Skip to content

Commit

Permalink
updated templates & pathing
Browse files Browse the repository at this point in the history
  • Loading branch information
Pritesh-Patel committed Oct 6, 2019
1 parent ec9ea7d commit 2745101
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 30 deletions.
17 changes: 10 additions & 7 deletions generate/proto/generate.go
Expand Up @@ -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"
Expand Down Expand Up @@ -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) {
Expand All @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Expand Up @@ -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=
Expand Down
2 changes: 1 addition & 1 deletion templates/golang/go_mod.tmpl
Expand Up @@ -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
Expand Down
18 changes: 15 additions & 3 deletions templates/golang/main.tmpl
Expand Up @@ -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"
)
Expand All @@ -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)

Expand Down
10 changes: 9 additions & 1 deletion templates/golang/server.tmpl
Expand Up @@ -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 {
Expand All @@ -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
}
6 changes: 5 additions & 1 deletion 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";
Expand Down Expand Up @@ -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);
Expand Down
28 changes: 14 additions & 14 deletions templates/proto/makefile.tmpl
Expand Up @@ -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}}

28 changes: 27 additions & 1 deletion 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}}
}
}
4 changes: 2 additions & 2 deletions templator/templator.go
Expand Up @@ -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,
Expand All @@ -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,
Expand Down

0 comments on commit 2745101

Please sign in to comment.