Skip to content

Commit c11b186

Browse files
Merge pull request #14 from commitdev/fix-file-pathing
updated templates & pathing
2 parents 908bbde + 2745101 commit c11b186

File tree

9 files changed

+85
-30
lines changed

9 files changed

+85
-30
lines changed

generate/proto/generate.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ package proto
33
import (
44
"fmt"
55
"bytes"
6-
"github.com/commitdev/sprout/util"
76

7+
"github.com/commitdev/sprout/util"
88
"github.com/commitdev/sprout/config"
99
"github.com/commitdev/sprout/templator"
1010
"log"
@@ -66,9 +66,9 @@ func GenerateProtoHealth(templator *templator.Templator, config *config.SproutCo
6666
templator.ProtoHealthTemplate.Execute(f, config)
6767
}
6868

69-
func GenerateServiceProtobufFiles(templator *templator.Templator, config *config.SproutConfig) {
70-
protoPath := fmt.Sprintf("%s-idl/proto", config.Name)
71-
for _, s := range config.Services {
69+
func GenerateServiceProtobufFiles(templator *templator.Templator, cfg *config.SproutConfig) {
70+
protoPath := fmt.Sprintf("%s-idl/proto", cfg.Name)
71+
for _, s := range cfg.Services {
7272
serviceProtoDir := fmt.Sprintf("%s/%s", protoPath, s.Name)
7373
err := os.Mkdir(serviceProtoDir, os.ModePerm)
7474
if os.IsExist(err) {
@@ -80,9 +80,12 @@ func GenerateServiceProtobufFiles(templator *templator.Templator, config *config
8080

8181
f, err := os.Create(serviceProtoFilePath)
8282

83-
data := map[string]string{
84-
"Organization": config.Organization,
85-
"ServiceName": s.Name,
83+
data:= struct {
84+
*config.SproutConfig
85+
ServiceName string
86+
} {
87+
cfg,
88+
s.Name,
8689
}
8790

8891
templator.ProtoServiceTemplate.Execute(f, data)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghf
2626
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
2727
github.com/commitdev/sprout/example/hello-world v0.0.0-20190827182108-cfad4c3d94cc h1:zBLjQxkC2LT1e9eMs0I2k26NXXZVT/XGrnuqdAsFt3A=
2828
github.com/commitdev/sprout/example/hello-world v0.0.0-20190827183525-9eeb1651f4f4 h1:pQw2XR8va971sW7QIX18I7FWft54TjLdQex5MHCcVtg=
29+
github.com/commitdev/sprout/example/hello-world v0.0.0-20191006174419-73168768419f h1:zfmpI6udrSYU1Ly4YYqEvE3WyoGrgzqLgevS6FHWlsw=
2930
github.com/commitdev/sprout/example/hello-world-idl v0.0.0-20190910021125-8ac64211bb19 h1:ULmWBQ848cHxodGxtQWas0lWPAh/ZntpV1QsobbasMA=
31+
github.com/commitdev/sprout/example/hello-world/hello-world-idl v0.0.0-20191006174419-73168768419f h1:yT3SIhGDFr+Pf7uW0wllWwp+HFeRvObO2s++6upn8g4=
3032
github.com/coreos/bbolt v1.3.2/go.mod h1:iRUV2dpdMOn7Bo10OQBFzIJO9kkE559Wcmn+qkEiiKk=
3133
github.com/coreos/bbolt v1.3.3/go.mod h1:iRUV2dpdMOn7Bo10OQBFzIJO9kkE559Wcmn+qkEiiKk=
3234
github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE=

templates/golang/go_mod.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module {{ .GitRepo }}/{{ .Name }}
22

33
go 1.12
44

5-
replace {{ .GitRepo }}/{{ .Name }}-idl => {{ .Name }}-idl
5+
replace {{ .GitRepo }}/{{ .Name }}-idl => ./{{ .Name }}-idl
66

77
require (
88
{{ .GitRepo }}/{{ .Name }}-idl v0.0.0

templates/golang/main.tmpl

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,15 @@ import (
33
"log"
44
"net"
55

6-
health "{{ .GitRepo }}/{{ .Name }}/server/health"
76
healthpb "{{ .GitRepo }}/{{ .Name }}-idl/gen/go/health"
8-
7+
{{- range .Services}}
8+
{{ .Name }}pb "{{ $.GitRepo }}/{{ $.Name }}-idl/gen/go/{{ .Name }}"
9+
{{- end}}
10+
11+
health "{{ .GitRepo }}/{{ .Name }}/server/health"
12+
{{- range .Services}}
13+
{{ .Name }} "{{ $.GitRepo }}/{{ $.Name }}/server/{{ .Name }}"
14+
{{- end}}
915

1016
"google.golang.org/grpc"
1117
)
@@ -16,11 +22,17 @@ func main() {
1622
if err != nil {
1723
log.Fatalf("failed to listen: %v", err)
1824
}
25+
1926
s := grpc.NewServer()
2027

21-
//TODO: Register your servers here
28+
//Server initialization & registration
2229
healthServer := health.NewHealthServer()
2330
healthpb.RegisterHealthServer(s, healthServer)
31+
{{- range .Services}}
32+
{{ .Name }}Server := {{ .Name }}.New{{ .Name | Title}}Server()
33+
{{ .Name }}pb.Register{{ .Name | Title}}Server(s, {{ .Name }}Server)
34+
{{- end}}
35+
2436

2537
log.Printf("Starting grpc server on %v...", grpcAddr)
2638

templates/golang/server.tmpl

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ package {{ .ServiceName }}
22

33
import (
44
"context"
5-
api "{{.GitRepo}}/{{ .ProjectName }}-idl/gen/go/{{ .ServiceName }}"
5+
health_api "github.com/yourrepo/hello-world-idl/gen/go/health"
6+
//api "{{.GitRepo}}/{{ .ProjectName }}-idl/gen/go/{{ .ServiceName }}"
67
)
78

89
type {{ .ServiceName | Title }}Server struct {
@@ -12,3 +13,10 @@ type {{ .ServiceName | Title }}Server struct {
1213
func New{{ .ServiceName | Title }}Server() *{{ .ServiceName | Title }}Server {
1314
return &{{ .ServiceName | Title }}Server{}
1415
}
16+
17+
func (s *{{ .ServiceName | Title }}Server) Check(ctx context.Context, req *health_api.HealthCheckRequest) (*health_api.HealthCheckResponse, error) {
18+
resp := &health_api.HealthCheckResponse{
19+
Status: health_api.HealthCheckResponse_SERVING,
20+
}
21+
return resp,nil
22+
}

templates/proto/health_proto.tmpl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
syntax = "proto3";
22

3-
package {{ .Organization }}.health;
3+
option go_package = "{{ .GitRepo }}/{{ .Name }}-idl/gen/go/health";
4+
5+
package health;
46

57
{{- if .Network.Http.Enabled }}
68
import "google/api/annotations.proto";
@@ -30,9 +32,11 @@ message HealthCheckResponse {
3032

3133
service Health {
3234
rpc Check(HealthCheckRequest) returns (HealthCheckResponse){
35+
{{- if .Network.Http.Enabled }}
3336
option (google.api.http) = {
3437
get: "/v1/health"
3538
};
39+
{{- end}}
3640
}
3741

3842
rpc Watch(HealthCheckRequest) returns (stream HealthCheckResponse);

templates/proto/makefile.tmpl

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,33 +35,33 @@ generate: generate-grpc {{ if .Network.Web.Enabled }}generate-web{{- end}} {{ if
3535

3636
generate-grpc:
3737
mkdir -p gen/{{ $language }}
38-
protoc ${PROTO_SOURCES} --{{ $language }}_out=plugins=grpc:./gen/{{ $language }} ./proto/health/*.proto
38+
protoc ${PROTO_SOURCES} --{{ $language }}_out=plugins=grpc,paths=source_relative:proto ./proto/health/*.proto
3939
{{- range .Services}}
40-
protoc ${PROTO_SOURCES} --{{ $language }}_out=plugins=grpc:./gen/{{ $language }} ./proto/{{ .Name }}/*.proto
40+
protoc ${PROTO_SOURCES} --{{ $language }}_out=plugins=grpc,paths=source_relative:proto ./proto/{{ .Name }}/*.proto
4141
{{- end }}
42-
cp -f -rv gen/go/proto/* gen/go
43-
rm -rf gen/go/proto
42+
cp -f -rv proto/proto/* gen/go
43+
rm -rf proto/proto
4444

4545
{{- if .Network.Web.Enabled }}
4646
generate-web:
4747
mkdir -p gen/web
48-
protoc ${PROTO_SOURCES} --grpc-web_out=import_style=typescript,mode=grpcwebtext:gen/web ./proto/health/*.proto
48+
protoc ${PROTO_SOURCES} --grpc-web_out=import_style=typescript,mode=grpcwebtext:proto ./proto/health/*.proto
4949
{{- range .Services}}
50-
protoc ${PROTO_SOURCES} --grpc-web_out=import_style=typescript,mode=grpcwebtext:gen/web ./proto/{{ .Name }}/*.proto
50+
protoc ${PROTO_SOURCES} --grpc-web_out=import_style=typescript,mode=grpcwebtext:proto ./proto/{{ .Name }}/*.proto
5151
{{- end }}
52-
cp -f -rv gen/web/proto/* gen/web
53-
rm -rf gen/web/Proto gen/web/proto
52+
cp -f -rv proto/proto/* gen/web
53+
cp -f -rv proto/Proto/* gen/web
54+
rm -rf proto/proto proto/Proto
5455
{{- end}}
5556

5657
{{- if .Network.Http.Enabled }}
5758
generate-http:
58-
mkdir -p gen/http gen/swagger
59-
protoc ${PROTO_SOURCES} --grpc-gateway_out=logtostderr=true:gen/http --swagger_out=logtostderr=true:gen/swagger ./proto/health/*.proto
59+
mkdir -p gen/http
60+
protoc ${PROTO_SOURCES} --grpc-gateway_out=logtostderr=true,paths=source_relative:proto --swagger_out=logtostderr=true:proto ./proto/health/*.proto
6061
{{- range .Services}}
61-
protoc ${PROTO_SOURCES} --grpc-gateway_out=logtostderr=true:gen/http --swagger_out=logtostderr=true:gen/swagger ./proto/{{ .Name }}/*.proto
62+
protoc ${PROTO_SOURCES} --grpc-gateway_out=logtostderr=true,paths=source_relative:proto --swagger_out=logtostderr=true:proto ./proto/{{ .Name }}/*.proto
6263
{{- end }}
63-
cp -f -rv gen/http/proto/* gen/http
64-
cp -f -rv gen/swagger/proto/* gen/swagger
65-
rm -rf gen/swagger/proto
64+
cp -f -rv proto/proto/* gen/http
65+
rm -rf proto/proto
6666
{{- end}}
6767

templates/proto/service_proto.tmpl

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,29 @@
11
syntax = "proto3";
22

3-
package {{ .Organization }}.{{ .ServiceName }};
3+
option go_package = "{{ .GitRepo }}/{{ .Name }}-idl/gen/go/{{ .ServiceName }}";
4+
5+
package {{ .ServiceName }};
6+
7+
import "proto/health/health.proto";
8+
9+
{{- if .Network.Http.Enabled}}
10+
import "google/api/annotations.proto";
11+
import "protoc-gen-swagger/options/annotations.proto";
12+
13+
option (grpc.gateway.protoc_gen_swagger.options.openapiv2_swagger) = {
14+
info: {
15+
title: "{{ .ServiceName | Title }}";
16+
version: "1.0";
17+
}
18+
};
19+
{{- end}}
20+
21+
service {{ .ServiceName | Title }} {
22+
rpc Check(health.HealthCheckRequest) returns (health.HealthCheckResponse){
23+
{{- if .Network.Http.Enabled }}
24+
option (google.api.http) = {
25+
get: "/v1/{{ .ServiceName }}/health"
26+
};
27+
{{- end}}
28+
}
29+
}

templator/templator.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func NewTemplator(box *packr.Box) *Templator {
3131
protoHealthTemplate, _ := template.New("ProtoHealthTemplate").Parse(protoHealthTemplateSource)
3232

3333
protoServiceTemplateSource, _ := box.FindString("proto/service_proto.tmpl")
34-
protoServiceTemplate, _ := template.New("ProtoServiceTemplate").Parse(protoServiceTemplateSource)
34+
protoServiceTemplate, _ := template.New("ProtoServiceTemplate").Funcs(util.FuncMap).Parse(protoServiceTemplateSource)
3535

3636
return &Templator{
3737
MakefileTemplate: makeFileTemplate,
@@ -57,7 +57,7 @@ func NewGoTemplator(box *packr.Box) *GoTemplator {
5757
goModIDLTemplate, _ := template.New("GoModTemplate").Parse(goModIDLTemplateSource)
5858

5959
goMainTemplateSource, _ := box.FindString("golang/main.tmpl")
60-
goMainTemplate, _ := template.New("GoMainTemplate").Parse(goMainTemplateSource)
60+
goMainTemplate, _ := template.New("GoMainTemplate").Funcs(util.FuncMap).Parse(goMainTemplateSource)
6161

6262
return &GoTemplator{
6363
GoMain: goMainTemplate,

0 commit comments

Comments
 (0)