diff --git a/cmd/init.go b/cmd/init.go index de6891a4..b0476261 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -11,6 +11,7 @@ import ( var modName string var module bool +var projectType string // initCmd initializes the service var initCmd = &cobra.Command{ @@ -32,7 +33,8 @@ var initCmd = &cobra.Command{ case "snake": fn = strcase.ToSnake } - options = append(options, svc.WithJsonCase(naming), svc.WithCaseConverter(fn), svc.WithProtoGenerator(v3.NewProtoGenerator(v3.WithFieldNamingFunc(fn), v3.WithProtocCmd(protocCmd)))) + options = append(options, svc.WithJsonCase(naming), svc.WithCaseConverter(fn), svc.WithProjectType(projectType), + svc.WithProtoGenerator(v3.NewProtoGenerator(v3.WithFieldNamingFunc(fn), v3.WithProtocCmd(protocCmd)))) s := svc.NewSvc(svcdir, options...) s.Init() }, @@ -46,4 +48,5 @@ func init() { initCmd.Flags().StringVarP(&docfile, "file", "f", "", `OpenAPI 3.0 or Swagger 2.0 spec json file path or download link`) initCmd.Flags().StringVar(&protocCmd, "grpc_gen_cmd", "protoc --proto_path=. --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative --go-json_out=. --go-json_opt=paths=source_relative", `command to generate grpc service and message code`) initCmd.Flags().StringVar(&naming, "case", "lowerCamel", `protobuf message field and json tag case, only support "lowerCamel" and "snake"`) + initCmd.Flags().StringVarP(&projectType, "type", "t", "grpc", `Indicate project type, accept values: grpc or rest`) } diff --git a/cmd/internal/svc/codegen/init.go b/cmd/internal/svc/codegen/init.go index 41c95800..89c54613 100644 --- a/cmd/internal/svc/codegen/init.go +++ b/cmd/internal/svc/codegen/init.go @@ -28,7 +28,11 @@ import ( "{{.DtoPackage}}" ) +{{ if eq .ProjectType "rest" }} +//go:generate go-doudou svc http --case {{ .JsonCase }} +{{ else }} //go:generate go-doudou svc grpc --http2grpc --case {{ .JsonCase }} +{{ end }} type {{.SvcName}} interface { // You can define your service methods as your need. Below is an example. @@ -179,6 +183,7 @@ type InitProjConfig struct { ProtoGenerator v3.ProtoGenerator JsonCase string DocPath string + ProjectType string } // InitProj inits a service project @@ -186,18 +191,19 @@ type InitProjConfig struct { // modName is module name func InitProj(conf InitProjConfig) { var ( - err error - svcName string - svcfile string - dtodir string - dtofile string - goVersion string - f *os.File - tpl *template.Template - envfile string - docPath string + err error + svcName string + svcfile string + dtodir string + dtofile string + goVersion string + f *os.File + tpl *template.Template + envfile string + docPath string + projectType string ) - dir, modName, runner, module, jsonCase, docPath := conf.Dir, conf.ModName, conf.Runner, conf.Module, conf.JsonCase, conf.DocPath + dir, modName, runner, module, jsonCase, docPath, projectType := conf.Dir, conf.ModName, conf.Runner, conf.Module, conf.JsonCase, conf.DocPath, conf.ProjectType if stringutils.IsEmpty(dir) { dir, _ = os.Getwd() } @@ -279,15 +285,17 @@ func InitProj(conf InitProjConfig) { tpl, _ = template.New(svcTmpl).Parse(svcTmpl) _ = tpl.Execute(f, struct { - DtoPackage string - SvcName string - Version string - JsonCase string + DtoPackage string + SvcName string + Version string + JsonCase string + ProjectType string }{ - DtoPackage: strings.ReplaceAll(filepath.Join(modName, "dto"), string(os.PathSeparator), "/"), - SvcName: svcName, - Version: version.Release, - JsonCase: jsonCase, + DtoPackage: strings.ReplaceAll(filepath.Join(modName, "dto"), string(os.PathSeparator), "/"), + SvcName: svcName, + Version: version.Release, + JsonCase: jsonCase, + ProjectType: projectType, }) } diff --git a/cmd/internal/svc/svc.go b/cmd/internal/svc/svc.go index 4ba3dc41..6f9a5dd3 100644 --- a/cmd/internal/svc/svc.go +++ b/cmd/internal/svc/svc.go @@ -95,7 +95,8 @@ type Svc struct { JsonCase string CaseConverter func(string) string - http2grpc bool + http2grpc bool + projectType string } type DbConfig struct { @@ -180,6 +181,7 @@ func (receiver *Svc) Init() { Module: receiver.module, ProtoGenerator: receiver.protoGenerator, JsonCase: receiver.JsonCase, + ProjectType: receiver.projectType, }) } @@ -268,6 +270,12 @@ func WithJsonCase(jsonCase string) SvcOption { } } +func WithProjectType(projectType string) SvcOption { + return func(svc *Svc) { + svc.projectType = projectType + } +} + func WithProtoGenerator(protoGenerator v3.ProtoGenerator) SvcOption { return func(svc *Svc) { svc.protoGenerator = protoGenerator