Fix Go-Kit test (#25)

This commit is contained in:
Manfred Touron 2017-03-16 16:16:45 +01:00
parent 97cfa60fc8
commit 9da682760b
14 changed files with 104 additions and 50 deletions

View File

@ -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

View File

@ -1 +1,2 @@
/vendor/
/server

View File

@ -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:

48
examples/go-kit/glide.lock generated Normal file
View File

@ -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: []

View File

@ -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

View File

@ -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

View File

@ -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,

View File

@ -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
}

View File

@ -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,

View File

@ -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
}

View File

@ -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,

View File

@ -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
}

View File

@ -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,

View File

@ -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