diff --git a/generate/proto/generate.go b/generate/proto/generate.go index a2b12b607..89ae2817b 100644 --- a/generate/proto/generate.go +++ b/generate/proto/generate.go @@ -3,8 +3,8 @@ package proto import ( "fmt" "bytes" - "github.com/commitdev/sprout/util" + "github.com/commitdev/sprout/util" "github.com/commitdev/sprout/config" "github.com/commitdev/sprout/templator" "log" @@ -66,9 +66,9 @@ func GenerateProtoHealth(templator *templator.Templator, config *config.SproutCo templator.ProtoHealthTemplate.Execute(f, config) } -func GenerateServiceProtobufFiles(templator *templator.Templator, config *config.SproutConfig) { - protoPath := fmt.Sprintf("%s-idl/proto", config.Name) - for _, s := range config.Services { +func GenerateServiceProtobufFiles(templator *templator.Templator, cfg *config.SproutConfig) { + protoPath := fmt.Sprintf("%s-idl/proto", cfg.Name) + for _, s := range cfg.Services { serviceProtoDir := fmt.Sprintf("%s/%s", protoPath, s.Name) err := os.Mkdir(serviceProtoDir, os.ModePerm) if os.IsExist(err) { @@ -80,9 +80,12 @@ func GenerateServiceProtobufFiles(templator *templator.Templator, config *config f, err := os.Create(serviceProtoFilePath) - data := map[string]string{ - "Organization": config.Organization, - "ServiceName": s.Name, + data:= struct { + *config.SproutConfig + ServiceName string + } { + cfg, + s.Name, } templator.ProtoServiceTemplate.Execute(f, data) diff --git a/go.sum b/go.sum index 3baec019a..b67ea5014 100644 --- a/go.sum +++ b/go.sum @@ -26,7 +26,9 @@ github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghf github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= github.com/commitdev/sprout/example/hello-world v0.0.0-20190827182108-cfad4c3d94cc h1:zBLjQxkC2LT1e9eMs0I2k26NXXZVT/XGrnuqdAsFt3A= github.com/commitdev/sprout/example/hello-world v0.0.0-20190827183525-9eeb1651f4f4 h1:pQw2XR8va971sW7QIX18I7FWft54TjLdQex5MHCcVtg= +github.com/commitdev/sprout/example/hello-world v0.0.0-20191006174419-73168768419f h1:zfmpI6udrSYU1Ly4YYqEvE3WyoGrgzqLgevS6FHWlsw= github.com/commitdev/sprout/example/hello-world-idl v0.0.0-20190910021125-8ac64211bb19 h1:ULmWBQ848cHxodGxtQWas0lWPAh/ZntpV1QsobbasMA= +github.com/commitdev/sprout/example/hello-world/hello-world-idl v0.0.0-20191006174419-73168768419f h1:yT3SIhGDFr+Pf7uW0wllWwp+HFeRvObO2s++6upn8g4= github.com/coreos/bbolt v1.3.2/go.mod h1:iRUV2dpdMOn7Bo10OQBFzIJO9kkE559Wcmn+qkEiiKk= github.com/coreos/bbolt v1.3.3/go.mod h1:iRUV2dpdMOn7Bo10OQBFzIJO9kkE559Wcmn+qkEiiKk= github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE= diff --git a/templates/golang/go_mod.tmpl b/templates/golang/go_mod.tmpl index 9def1532a..6a1725817 100644 --- a/templates/golang/go_mod.tmpl +++ b/templates/golang/go_mod.tmpl @@ -2,7 +2,7 @@ module {{ .GitRepo }}/{{ .Name }} go 1.12 -replace {{ .GitRepo }}/{{ .Name }}-idl => {{ .Name }}-idl +replace {{ .GitRepo }}/{{ .Name }}-idl => ./{{ .Name }}-idl require ( {{ .GitRepo }}/{{ .Name }}-idl v0.0.0 diff --git a/templates/golang/main.tmpl b/templates/golang/main.tmpl index 00b1bb472..bf2156c31 100644 --- a/templates/golang/main.tmpl +++ b/templates/golang/main.tmpl @@ -3,9 +3,15 @@ import ( "log" "net" - health "{{ .GitRepo }}/{{ .Name }}/server/health" healthpb "{{ .GitRepo }}/{{ .Name }}-idl/gen/go/health" - + {{- range .Services}} + {{ .Name }}pb "{{ $.GitRepo }}/{{ $.Name }}-idl/gen/go/{{ .Name }}" + {{- end}} + + health "{{ .GitRepo }}/{{ .Name }}/server/health" + {{- range .Services}} + {{ .Name }} "{{ $.GitRepo }}/{{ $.Name }}/server/{{ .Name }}" + {{- end}} "google.golang.org/grpc" ) @@ -16,11 +22,17 @@ func main() { if err != nil { log.Fatalf("failed to listen: %v", err) } + s := grpc.NewServer() - //TODO: Register your servers here + //Server initialization & registration healthServer := health.NewHealthServer() healthpb.RegisterHealthServer(s, healthServer) + {{- range .Services}} + {{ .Name }}Server := {{ .Name }}.New{{ .Name | Title}}Server() + {{ .Name }}pb.Register{{ .Name | Title}}Server(s, {{ .Name }}Server) + {{- end}} + log.Printf("Starting grpc server on %v...", grpcAddr) diff --git a/templates/golang/server.tmpl b/templates/golang/server.tmpl index a5d604c77..aa9aa7940 100644 --- a/templates/golang/server.tmpl +++ b/templates/golang/server.tmpl @@ -2,7 +2,8 @@ package {{ .ServiceName }} import ( "context" - api "{{.GitRepo}}/{{ .ProjectName }}-idl/gen/go/{{ .ServiceName }}" + health_api "github.com/yourrepo/hello-world-idl/gen/go/health" + //api "{{.GitRepo}}/{{ .ProjectName }}-idl/gen/go/{{ .ServiceName }}" ) type {{ .ServiceName | Title }}Server struct { @@ -12,3 +13,10 @@ type {{ .ServiceName | Title }}Server struct { func New{{ .ServiceName | Title }}Server() *{{ .ServiceName | Title }}Server { return &{{ .ServiceName | Title }}Server{} } + +func (s *{{ .ServiceName | Title }}Server) Check(ctx context.Context, req *health_api.HealthCheckRequest) (*health_api.HealthCheckResponse, error) { + resp := &health_api.HealthCheckResponse{ + Status: health_api.HealthCheckResponse_SERVING, + } + return resp,nil +} diff --git a/templates/proto/health_proto.tmpl b/templates/proto/health_proto.tmpl index 86149f0f2..b5658e76b 100644 --- a/templates/proto/health_proto.tmpl +++ b/templates/proto/health_proto.tmpl @@ -1,6 +1,8 @@ syntax = "proto3"; -package {{ .Organization }}.health; +option go_package = "{{ .GitRepo }}/{{ .Name }}-idl/gen/go/health"; + +package health; {{- if .Network.Http.Enabled }} import "google/api/annotations.proto"; @@ -30,9 +32,11 @@ message HealthCheckResponse { service Health { rpc Check(HealthCheckRequest) returns (HealthCheckResponse){ + {{- if .Network.Http.Enabled }} option (google.api.http) = { get: "/v1/health" }; + {{- end}} } rpc Watch(HealthCheckRequest) returns (stream HealthCheckResponse); diff --git a/templates/proto/makefile.tmpl b/templates/proto/makefile.tmpl index 50f3362ab..417d2b6ec 100644 --- a/templates/proto/makefile.tmpl +++ b/templates/proto/makefile.tmpl @@ -35,33 +35,33 @@ generate: generate-grpc {{ if .Network.Web.Enabled }}generate-web{{- end}} {{ if generate-grpc: mkdir -p gen/{{ $language }} - protoc ${PROTO_SOURCES} --{{ $language }}_out=plugins=grpc:./gen/{{ $language }} ./proto/health/*.proto + protoc ${PROTO_SOURCES} --{{ $language }}_out=plugins=grpc,paths=source_relative:proto ./proto/health/*.proto {{- range .Services}} - protoc ${PROTO_SOURCES} --{{ $language }}_out=plugins=grpc:./gen/{{ $language }} ./proto/{{ .Name }}/*.proto + protoc ${PROTO_SOURCES} --{{ $language }}_out=plugins=grpc,paths=source_relative:proto ./proto/{{ .Name }}/*.proto {{- end }} - cp -f -rv gen/go/proto/* gen/go - rm -rf gen/go/proto + cp -f -rv proto/proto/* gen/go + rm -rf proto/proto {{- if .Network.Web.Enabled }} generate-web: mkdir -p gen/web - protoc ${PROTO_SOURCES} --grpc-web_out=import_style=typescript,mode=grpcwebtext:gen/web ./proto/health/*.proto + protoc ${PROTO_SOURCES} --grpc-web_out=import_style=typescript,mode=grpcwebtext:proto ./proto/health/*.proto {{- range .Services}} - protoc ${PROTO_SOURCES} --grpc-web_out=import_style=typescript,mode=grpcwebtext:gen/web ./proto/{{ .Name }}/*.proto + protoc ${PROTO_SOURCES} --grpc-web_out=import_style=typescript,mode=grpcwebtext:proto ./proto/{{ .Name }}/*.proto {{- end }} - cp -f -rv gen/web/proto/* gen/web - rm -rf gen/web/Proto gen/web/proto + cp -f -rv proto/proto/* gen/web + cp -f -rv proto/Proto/* gen/web + rm -rf proto/proto proto/Proto {{- end}} {{- if .Network.Http.Enabled }} generate-http: - mkdir -p gen/http gen/swagger - protoc ${PROTO_SOURCES} --grpc-gateway_out=logtostderr=true:gen/http --swagger_out=logtostderr=true:gen/swagger ./proto/health/*.proto + mkdir -p gen/http + protoc ${PROTO_SOURCES} --grpc-gateway_out=logtostderr=true,paths=source_relative:proto --swagger_out=logtostderr=true:proto ./proto/health/*.proto {{- range .Services}} - protoc ${PROTO_SOURCES} --grpc-gateway_out=logtostderr=true:gen/http --swagger_out=logtostderr=true:gen/swagger ./proto/{{ .Name }}/*.proto + protoc ${PROTO_SOURCES} --grpc-gateway_out=logtostderr=true,paths=source_relative:proto --swagger_out=logtostderr=true:proto ./proto/{{ .Name }}/*.proto {{- end }} - cp -f -rv gen/http/proto/* gen/http - cp -f -rv gen/swagger/proto/* gen/swagger - rm -rf gen/swagger/proto + cp -f -rv proto/proto/* gen/http + rm -rf proto/proto {{- end}} diff --git a/templates/proto/service_proto.tmpl b/templates/proto/service_proto.tmpl index 5b7e2e75f..95300561c 100644 --- a/templates/proto/service_proto.tmpl +++ b/templates/proto/service_proto.tmpl @@ -1,3 +1,29 @@ syntax = "proto3"; -package {{ .Organization }}.{{ .ServiceName }}; +option go_package = "{{ .GitRepo }}/{{ .Name }}-idl/gen/go/{{ .ServiceName }}"; + +package {{ .ServiceName }}; + +import "proto/health/health.proto"; + +{{- if .Network.Http.Enabled}} +import "google/api/annotations.proto"; +import "protoc-gen-swagger/options/annotations.proto"; + +option (grpc.gateway.protoc_gen_swagger.options.openapiv2_swagger) = { + info: { + title: "{{ .ServiceName | Title }}"; + version: "1.0"; + } +}; +{{- end}} + +service {{ .ServiceName | Title }} { + rpc Check(health.HealthCheckRequest) returns (health.HealthCheckResponse){ + {{- if .Network.Http.Enabled }} + option (google.api.http) = { + get: "/v1/{{ .ServiceName }}/health" + }; + {{- end}} + } +} diff --git a/templator/templator.go b/templator/templator.go index 6667e375e..830b4ca81 100644 --- a/templator/templator.go +++ b/templator/templator.go @@ -31,7 +31,7 @@ func NewTemplator(box *packr.Box) *Templator { protoHealthTemplate, _ := template.New("ProtoHealthTemplate").Parse(protoHealthTemplateSource) protoServiceTemplateSource, _ := box.FindString("proto/service_proto.tmpl") - protoServiceTemplate, _ := template.New("ProtoServiceTemplate").Parse(protoServiceTemplateSource) + protoServiceTemplate, _ := template.New("ProtoServiceTemplate").Funcs(util.FuncMap).Parse(protoServiceTemplateSource) return &Templator{ MakefileTemplate: makeFileTemplate, @@ -57,7 +57,7 @@ func NewGoTemplator(box *packr.Box) *GoTemplator { goModIDLTemplate, _ := template.New("GoModTemplate").Parse(goModIDLTemplateSource) goMainTemplateSource, _ := box.FindString("golang/main.tmpl") - goMainTemplate, _ := template.New("GoMainTemplate").Parse(goMainTemplateSource) + goMainTemplate, _ := template.New("GoMainTemplate").Funcs(util.FuncMap).Parse(goMainTemplateSource) return &GoTemplator{ GoMain: goMainTemplate,