From 9da682760bd60a69f36b9cc8fb4e9a61b08e84d6 Mon Sep 17 00:00:00 2001 From: Manfred Touron Date: Thu, 16 Mar 2017 16:16:45 +0100 Subject: [PATCH] Fix Go-Kit test (#25) --- .travis.yml | 3 +- examples/go-kit/.gitignore | 1 + examples/go-kit/Makefile | 2 +- examples/go-kit/glide.lock | 48 +++++++++++++++++++ examples/go-kit/glide.yaml | 20 ++++++++ examples/go-kit/main.go | 22 ++++----- .../session/gen/transports/grpc/grpc.go | 3 +- .../session/gen/transports/http/http.go | 7 ++- .../sprint/gen/transports/grpc/grpc.go | 5 +- .../sprint/gen/transports/http/http.go | 17 +++---- .../services/user/gen/transports/grpc/grpc.go | 4 +- .../services/user/gen/transports/http/http.go | 12 ++--- .../gen/transports/grpc/grpc.go.tmpl | 3 +- .../gen/transports/http/http.go.tmpl | 7 ++- 14 files changed, 104 insertions(+), 50 deletions(-) create mode 100644 examples/go-kit/glide.lock create mode 100644 examples/go-kit/glide.yaml diff --git a/.travis.yml b/.travis.yml index 4a93fed..7381ecb 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,8 +1,9 @@ language: go go: 1.7.x install: +- mkdir -p $GOPATH/bin && curl https://glide.sh/get | sh - wget https://raw.githubusercontent.com/grpc-ecosystem/grpc-gateway/master/.travis/install-protoc.sh && chmod +x install-protoc.sh && ./install-protoc.sh 3.1.0 -- go get github.com/gogo/protobuf/protoc-gen-gogo +- go get -u github.com/golang/protobuf/protoc-gen-go script: - make install - make test diff --git a/examples/go-kit/.gitignore b/examples/go-kit/.gitignore index 06845b1..dc01103 100644 --- a/examples/go-kit/.gitignore +++ b/examples/go-kit/.gitignore @@ -1 +1,2 @@ +/vendor/ /server diff --git a/examples/go-kit/Makefile b/examples/go-kit/Makefile index b39252f..409f4f4 100644 --- a/examples/go-kit/Makefile +++ b/examples/go-kit/Makefile @@ -9,7 +9,7 @@ service_name = $(word 2,$(subst /, ,$1)) build: server server: $(TARGETS_GO) $(TARGETS_TMPL) - go get ./... + glide install go build -o server . $(TARGETS_GO): %_go: diff --git a/examples/go-kit/glide.lock b/examples/go-kit/glide.lock new file mode 100644 index 0000000..9b26bce --- /dev/null +++ b/examples/go-kit/glide.lock @@ -0,0 +1,48 @@ +hash: e225ab17b49f8d6601b2bd813d43ad34ee04380d29c278c11919efd454c7b2d7 +updated: 2017-03-16T17:03:34.437968115+01:00 +imports: +- name: github.com/dgrijalva/jwt-go + version: 2268707a8f0843315e2004ee4f1d021dc08baedf +- name: github.com/go-kit/kit + version: fadad6fffe0466b19df9efd9acde5c9a52df5fa4 + subpackages: + - auth/jwt + - endpoint + - log + - transport/grpc + - transport/http +- name: github.com/go-logfmt/logfmt + version: 390ab7935ee28ec6b286364bba9b4dd6410cb3d5 +- name: github.com/go-stack/stack + version: 100eb0c0a9c5b306ca2fb4f165df21d80ada4b82 +- name: github.com/golang/protobuf + version: c9c7427a2a70d2eb3bafa0ab2dc163e45f143317 + subpackages: + - proto +- name: github.com/gorilla/handlers + version: 3a5767ca75ece5f7f1440b1d16975247f8d8b221 +- name: github.com/kr/logfmt + version: b84e30acd515aadc4b783ad4ff83aff3299bdfe0 +- name: golang.org/x/net + version: a6577fac2d73be281a500b310739095313165611 + subpackages: + - context + - context/ctxhttp + - http2 + - http2/hpack + - idna + - internal/timeseries + - lex/httplex + - trace +- name: google.golang.org/grpc + version: 777daa17ff9b5daef1cfdf915088a2ada3332bf0 + subpackages: + - codes + - credentials + - grpclog + - internal + - metadata + - naming + - peer + - transport +testImports: [] diff --git a/examples/go-kit/glide.yaml b/examples/go-kit/glide.yaml new file mode 100644 index 0000000..807973a --- /dev/null +++ b/examples/go-kit/glide.yaml @@ -0,0 +1,20 @@ +package: github.com/moul/protoc-gen-gotemplate/examples/go-kit +import: +- package: github.com/go-kit/kit + subpackages: + - auth/jwt + - endpoint + - log + - transport/grpc + - transport/http +- package: github.com/golang/protobuf + subpackages: + - proto + version: c9c7427a2a70d2eb3bafa0ab2dc163e45f143317 +- package: github.com/gorilla/handlers +- package: golang.org/x/net + subpackages: + - context + - http2 + version: a6577fac2d73be281a500b310739095313165611 +- package: google.golang.org/grpc diff --git a/examples/go-kit/main.go b/examples/go-kit/main.go index 32c03a6..e544e3c 100644 --- a/examples/go-kit/main.go +++ b/examples/go-kit/main.go @@ -1,7 +1,6 @@ package main import ( - "context" "fmt" "net" "net/http" @@ -34,37 +33,36 @@ import ( func main() { mux := http.NewServeMux() - ctx := context.Background() errc := make(chan error) s := grpc.NewServer() var logger log.Logger { logger = log.NewLogfmtLogger(os.Stdout) - logger = log.NewContext(logger).With("ts", log.DefaultTimestampUTC) - logger = log.NewContext(logger).With("caller", log.DefaultCaller) + logger = log.With(logger, "ts", log.DefaultTimestampUTC) + logger = log.With(logger, "caller", log.DefaultCaller) } // initialize services { svc := session_svc.New() endpoints := session_endpoints.MakeEndpoints(svc) - srv := session_grpctransport.MakeGRPCServer(ctx, endpoints) + srv := session_grpctransport.MakeGRPCServer(endpoints) session_pb.RegisterSessionServiceServer(s, srv) - session_httptransport.RegisterHandlers(ctx, svc, mux, endpoints) + session_httptransport.RegisterHandlers(svc, mux, endpoints) } { svc := sprint_svc.New() endpoints := sprint_endpoints.MakeEndpoints(svc) - srv := sprint_grpctransport.MakeGRPCServer(ctx, endpoints) + srv := sprint_grpctransport.MakeGRPCServer(endpoints) sprint_pb.RegisterSprintServiceServer(s, srv) - sprint_httptransport.RegisterHandlers(ctx, svc, mux, endpoints) + sprint_httptransport.RegisterHandlers(svc, mux, endpoints) } { svc := user_svc.New() endpoints := user_endpoints.MakeEndpoints(svc) - srv := user_grpctransport.MakeGRPCServer(ctx, endpoints) + srv := user_grpctransport.MakeGRPCServer(endpoints) user_pb.RegisterUserServiceServer(s, srv) - user_httptransport.RegisterHandlers(ctx, svc, mux, endpoints) + user_httptransport.RegisterHandlers(svc, mux, endpoints) } // start servers @@ -75,13 +73,13 @@ func main() { }() go func() { - logger := log.NewContext(logger).With("transport", "HTTP") + logger := log.With(logger, "transport", "HTTP") logger.Log("addr", ":8000") errc <- http.ListenAndServe(":8000", handlers.LoggingHandler(os.Stderr, mux)) }() go func() { - logger := log.NewContext(logger).With("transport", "gRPC") + logger := log.With(logger, "transport", "gRPC") ln, err := net.Listen("tcp", ":9000") if err != nil { errc <- err diff --git a/examples/go-kit/services/session/gen/transports/grpc/grpc.go b/examples/go-kit/services/session/gen/transports/grpc/grpc.go index 70ab7dc..eccbb3d 100644 --- a/examples/go-kit/services/session/gen/transports/grpc/grpc.go +++ b/examples/go-kit/services/session/gen/transports/grpc/grpc.go @@ -14,13 +14,12 @@ import ( // avoid import errors var _ = fmt.Errorf -func MakeGRPCServer(ctx context.Context, endpoints endpoints.Endpoints) pb.SessionServiceServer { +func MakeGRPCServer(endpoints endpoints.Endpoints) pb.SessionServiceServer { var options []grpctransport.ServerOption _ = options return &grpcServer{ login: grpctransport.NewServer( - ctx, endpoints.LoginEndpoint, decodeRequest, encodeLoginResponse, diff --git a/examples/go-kit/services/session/gen/transports/http/http.go b/examples/go-kit/services/session/gen/transports/http/http.go index e0ea137..b608681 100644 --- a/examples/go-kit/services/session/gen/transports/http/http.go +++ b/examples/go-kit/services/session/gen/transports/http/http.go @@ -16,9 +16,8 @@ var _ = log.Printf var _ = gokit_endpoint.Chain var _ = httptransport.NewClient -func MakeLoginHandler(ctx context.Context, svc pb.SessionServiceServer, endpoint gokit_endpoint.Endpoint) *httptransport.Server { +func MakeLoginHandler(svc pb.SessionServiceServer, endpoint gokit_endpoint.Endpoint) *httptransport.Server { return httptransport.NewServer( - ctx, endpoint, decodeLoginRequest, encodeResponse, @@ -38,10 +37,10 @@ func encodeResponse(ctx context.Context, w http.ResponseWriter, response interfa return json.NewEncoder(w).Encode(response) } -func RegisterHandlers(ctx context.Context, svc pb.SessionServiceServer, mux *http.ServeMux, endpoints endpoints.Endpoints) error { +func RegisterHandlers(svc pb.SessionServiceServer, mux *http.ServeMux, endpoints endpoints.Endpoints) error { log.Println("new HTTP endpoint: \"/Login\" (service=Session)") - mux.Handle("/Login", MakeLoginHandler(ctx, svc, endpoints.LoginEndpoint)) + mux.Handle("/Login", MakeLoginHandler(svc, endpoints.LoginEndpoint)) return nil } diff --git a/examples/go-kit/services/sprint/gen/transports/grpc/grpc.go b/examples/go-kit/services/sprint/gen/transports/grpc/grpc.go index 3247a88..5fd10c8 100644 --- a/examples/go-kit/services/sprint/gen/transports/grpc/grpc.go +++ b/examples/go-kit/services/sprint/gen/transports/grpc/grpc.go @@ -14,13 +14,12 @@ import ( // avoid import errors var _ = fmt.Errorf -func MakeGRPCServer(ctx context.Context, endpoints endpoints.Endpoints) pb.SprintServiceServer { +func MakeGRPCServer(endpoints endpoints.Endpoints) pb.SprintServiceServer { var options []grpctransport.ServerOption _ = options return &grpcServer{ addsprint: grpctransport.NewServer( - ctx, endpoints.AddSprintEndpoint, decodeRequest, encodeAddSprintResponse, @@ -28,7 +27,6 @@ func MakeGRPCServer(ctx context.Context, endpoints endpoints.Endpoints) pb.Sprin ), closesprint: grpctransport.NewServer( - ctx, endpoints.CloseSprintEndpoint, decodeRequest, encodeCloseSprintResponse, @@ -36,7 +34,6 @@ func MakeGRPCServer(ctx context.Context, endpoints endpoints.Endpoints) pb.Sprin ), getsprint: grpctransport.NewServer( - ctx, endpoints.GetSprintEndpoint, decodeRequest, encodeGetSprintResponse, diff --git a/examples/go-kit/services/sprint/gen/transports/http/http.go b/examples/go-kit/services/sprint/gen/transports/http/http.go index 1ad3597..b6df66a 100644 --- a/examples/go-kit/services/sprint/gen/transports/http/http.go +++ b/examples/go-kit/services/sprint/gen/transports/http/http.go @@ -16,9 +16,8 @@ var _ = log.Printf var _ = gokit_endpoint.Chain var _ = httptransport.NewClient -func MakeAddSprintHandler(ctx context.Context, svc pb.SprintServiceServer, endpoint gokit_endpoint.Endpoint) *httptransport.Server { +func MakeAddSprintHandler(svc pb.SprintServiceServer, endpoint gokit_endpoint.Endpoint) *httptransport.Server { return httptransport.NewServer( - ctx, endpoint, decodeAddSprintRequest, encodeResponse, @@ -34,9 +33,8 @@ func decodeAddSprintRequest(ctx context.Context, r *http.Request) (interface{}, return &req, nil } -func MakeCloseSprintHandler(ctx context.Context, svc pb.SprintServiceServer, endpoint gokit_endpoint.Endpoint) *httptransport.Server { +func MakeCloseSprintHandler(svc pb.SprintServiceServer, endpoint gokit_endpoint.Endpoint) *httptransport.Server { return httptransport.NewServer( - ctx, endpoint, decodeCloseSprintRequest, encodeResponse, @@ -52,9 +50,8 @@ func decodeCloseSprintRequest(ctx context.Context, r *http.Request) (interface{} return &req, nil } -func MakeGetSprintHandler(ctx context.Context, svc pb.SprintServiceServer, endpoint gokit_endpoint.Endpoint) *httptransport.Server { +func MakeGetSprintHandler(svc pb.SprintServiceServer, endpoint gokit_endpoint.Endpoint) *httptransport.Server { return httptransport.NewServer( - ctx, endpoint, decodeGetSprintRequest, encodeResponse, @@ -74,16 +71,16 @@ func encodeResponse(ctx context.Context, w http.ResponseWriter, response interfa return json.NewEncoder(w).Encode(response) } -func RegisterHandlers(ctx context.Context, svc pb.SprintServiceServer, mux *http.ServeMux, endpoints endpoints.Endpoints) error { +func RegisterHandlers(svc pb.SprintServiceServer, mux *http.ServeMux, endpoints endpoints.Endpoints) error { log.Println("new HTTP endpoint: \"/AddSprint\" (service=Sprint)") - mux.Handle("/AddSprint", MakeAddSprintHandler(ctx, svc, endpoints.AddSprintEndpoint)) + mux.Handle("/AddSprint", MakeAddSprintHandler(svc, endpoints.AddSprintEndpoint)) log.Println("new HTTP endpoint: \"/CloseSprint\" (service=Sprint)") - mux.Handle("/CloseSprint", MakeCloseSprintHandler(ctx, svc, endpoints.CloseSprintEndpoint)) + mux.Handle("/CloseSprint", MakeCloseSprintHandler(svc, endpoints.CloseSprintEndpoint)) log.Println("new HTTP endpoint: \"/GetSprint\" (service=Sprint)") - mux.Handle("/GetSprint", MakeGetSprintHandler(ctx, svc, endpoints.GetSprintEndpoint)) + mux.Handle("/GetSprint", MakeGetSprintHandler(svc, endpoints.GetSprintEndpoint)) return nil } diff --git a/examples/go-kit/services/user/gen/transports/grpc/grpc.go b/examples/go-kit/services/user/gen/transports/grpc/grpc.go index f41fb29..636107f 100644 --- a/examples/go-kit/services/user/gen/transports/grpc/grpc.go +++ b/examples/go-kit/services/user/gen/transports/grpc/grpc.go @@ -14,13 +14,12 @@ import ( // avoid import errors var _ = fmt.Errorf -func MakeGRPCServer(ctx context.Context, endpoints endpoints.Endpoints) pb.UserServiceServer { +func MakeGRPCServer(endpoints endpoints.Endpoints) pb.UserServiceServer { var options []grpctransport.ServerOption _ = options return &grpcServer{ createuser: grpctransport.NewServer( - ctx, endpoints.CreateUserEndpoint, decodeRequest, encodeCreateUserResponse, @@ -28,7 +27,6 @@ func MakeGRPCServer(ctx context.Context, endpoints endpoints.Endpoints) pb.UserS ), getuser: grpctransport.NewServer( - ctx, endpoints.GetUserEndpoint, decodeRequest, encodeGetUserResponse, diff --git a/examples/go-kit/services/user/gen/transports/http/http.go b/examples/go-kit/services/user/gen/transports/http/http.go index 1a1f344..409ab6f 100644 --- a/examples/go-kit/services/user/gen/transports/http/http.go +++ b/examples/go-kit/services/user/gen/transports/http/http.go @@ -16,9 +16,8 @@ var _ = log.Printf var _ = gokit_endpoint.Chain var _ = httptransport.NewClient -func MakeCreateUserHandler(ctx context.Context, svc pb.UserServiceServer, endpoint gokit_endpoint.Endpoint) *httptransport.Server { +func MakeCreateUserHandler(svc pb.UserServiceServer, endpoint gokit_endpoint.Endpoint) *httptransport.Server { return httptransport.NewServer( - ctx, endpoint, decodeCreateUserRequest, encodeResponse, @@ -34,9 +33,8 @@ func decodeCreateUserRequest(ctx context.Context, r *http.Request) (interface{}, return &req, nil } -func MakeGetUserHandler(ctx context.Context, svc pb.UserServiceServer, endpoint gokit_endpoint.Endpoint) *httptransport.Server { +func MakeGetUserHandler(svc pb.UserServiceServer, endpoint gokit_endpoint.Endpoint) *httptransport.Server { return httptransport.NewServer( - ctx, endpoint, decodeGetUserRequest, encodeResponse, @@ -56,13 +54,13 @@ func encodeResponse(ctx context.Context, w http.ResponseWriter, response interfa return json.NewEncoder(w).Encode(response) } -func RegisterHandlers(ctx context.Context, svc pb.UserServiceServer, mux *http.ServeMux, endpoints endpoints.Endpoints) error { +func RegisterHandlers(svc pb.UserServiceServer, mux *http.ServeMux, endpoints endpoints.Endpoints) error { log.Println("new HTTP endpoint: \"/CreateUser\" (service=User)") - mux.Handle("/CreateUser", MakeCreateUserHandler(ctx, svc, endpoints.CreateUserEndpoint)) + mux.Handle("/CreateUser", MakeCreateUserHandler(svc, endpoints.CreateUserEndpoint)) log.Println("new HTTP endpoint: \"/GetUser\" (service=User)") - mux.Handle("/GetUser", MakeGetUserHandler(ctx, svc, endpoints.GetUserEndpoint)) + mux.Handle("/GetUser", MakeGetUserHandler(svc, endpoints.GetUserEndpoint)) return nil } diff --git a/examples/go-kit/templates/{{.File.Package}}/gen/transports/grpc/grpc.go.tmpl b/examples/go-kit/templates/{{.File.Package}}/gen/transports/grpc/grpc.go.tmpl index 0ddfe27..a10ee70 100644 --- a/examples/go-kit/templates/{{.File.Package}}/gen/transports/grpc/grpc.go.tmpl +++ b/examples/go-kit/templates/{{.File.Package}}/gen/transports/grpc/grpc.go.tmpl @@ -16,7 +16,7 @@ import ( // avoid import errors var _ = fmt.Errorf -func MakeGRPCServer(ctx context.Context, endpoints endpoints.Endpoints) pb.{{.File.Package | title}}ServiceServer { +func MakeGRPCServer(endpoints endpoints.Endpoints) pb.{{.File.Package | title}}ServiceServer { var options []grpctransport.ServerOption _ = options return &grpcServer{ @@ -27,7 +27,6 @@ func MakeGRPCServer(ctx context.Context, endpoints endpoints.Endpoints) pb.{{.Fi }, {{else}} {{.Name | lower}}: grpctransport.NewServer( - ctx, endpoints.{{.Name}}Endpoint, decodeRequest, encode{{.Name}}Response, diff --git a/examples/go-kit/templates/{{.File.Package}}/gen/transports/http/http.go.tmpl b/examples/go-kit/templates/{{.File.Package}}/gen/transports/http/http.go.tmpl index 40d04c0..a5b577d 100644 --- a/examples/go-kit/templates/{{.File.Package}}/gen/transports/http/http.go.tmpl +++ b/examples/go-kit/templates/{{.File.Package}}/gen/transports/http/http.go.tmpl @@ -21,9 +21,8 @@ var _ = httptransport.NewClient {{range .Service.Method}} {{if and (not .ServerStreaming) (not .ClientStreaming)}} - func Make{{.Name}}Handler(ctx context.Context, svc pb.{{$file.Package | title}}ServiceServer, endpoint gokit_endpoint.Endpoint) *httptransport.Server { + func Make{{.Name}}Handler(svc pb.{{$file.Package | title}}ServiceServer, endpoint gokit_endpoint.Endpoint) *httptransport.Server { return httptransport.NewServer( - ctx, endpoint, decode{{.Name}}Request, encodeResponse, @@ -45,11 +44,11 @@ func encodeResponse(ctx context.Context, w http.ResponseWriter, response interfa return json.NewEncoder(w).Encode(response) } -func RegisterHandlers(ctx context.Context, svc pb.{{$file.Package | title}}ServiceServer, mux *http.ServeMux, endpoints endpoints.Endpoints) error { +func RegisterHandlers(svc pb.{{$file.Package | title}}ServiceServer, mux *http.ServeMux, endpoints endpoints.Endpoints) error { {{range .Service.Method}} {{if and (not .ServerStreaming) (not .ClientStreaming)}} log.Println("new HTTP endpoint: \"/{{.Name}}\" (service={{$file.Package | title}})") - mux.Handle("/{{.Name}}", Make{{.Name}}Handler(ctx, svc, endpoints.{{.Name}}Endpoint)) + mux.Handle("/{{.Name}}", Make{{.Name}}Handler(svc, endpoints.{{.Name}}Endpoint)) {{end}} {{end}} return nil