diff --git a/examples/go-kit/.gitignore b/examples/go-kit/.gitignore new file mode 100644 index 0000000..06845b1 --- /dev/null +++ b/examples/go-kit/.gitignore @@ -0,0 +1 @@ +/server diff --git a/examples/go-kit/Makefile b/examples/go-kit/Makefile new file mode 100644 index 0000000..2391b58 --- /dev/null +++ b/examples/go-kit/Makefile @@ -0,0 +1,23 @@ +SOURCES := $(shell find . -name "*.proto" -not -path ./vendor/\*) + +TARGETS_GO := $(foreach source, $(SOURCES), $(source)_go) +TARGETS_TMPL := $(foreach source, $(SOURCES), $(source)_tmpl) + +service_name = $(word 2,$(subst /, ,$1)) + +.PHONY: build +build: server + +server: $(TARGETS_GO) $(TARGETS_TMPL) + go build -o server . + +$(TARGETS_GO): %_go: + protoc --gogo_out=plugins=grpc:. "$*" + @mkdir -p services/$(call service_name,$*)/gen/pb + @mv ./services/$(call service_name,$*)/$(call service_name,$*).pb.go ./services/$(call service_name,$*)/gen/pb/pb.go + +$(TARGETS_TMPL): %_tmpl: + @mkdir -p $(dir $*)gen + protoc -I. --gotemplate_out=template_dir=templates:services "$*" + @rm -rf services/services # need to investigate why this directory is created + gofmt -w $(dir $*)gen diff --git a/examples/go-kit/main.go b/examples/go-kit/main.go new file mode 100644 index 0000000..c46bd1f --- /dev/null +++ b/examples/go-kit/main.go @@ -0,0 +1,82 @@ +package main + +import ( + "context" + "fmt" + "net" + "net/http" + "os" + "os/signal" + "syscall" + + "github.com/go-kit/kit/log" + "github.com/gorilla/handlers" + "google.golang.org/grpc" + + session_svc "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/session" + session_endpoints "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/session/gen/endpoints" + session_pb "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/session/gen/pb" + session_grpctransport "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/session/gen/transports/grpc" + session_httptransport "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/session/gen/transports/http" + + sprint_svc "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/sprint" + sprint_endpoints "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/sprint/gen/endpoints" + sprint_pb "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/sprint/gen/pb" + sprint_grpctransport "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/sprint/gen/transports/grpc" + sprint_httptransport "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/sprint/gen/transports/http" +) + +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) + } + + // initialize services + { + svc := session_svc.New() + endpoints := session_endpoints.MakeEndpoints(svc) + srv := session_grpctransport.MakeGRPCServer(ctx, endpoints) + session_pb.RegisterSessionServiceServer(s, srv) + session_httptransport.RegisterHandlers(ctx, svc, mux, endpoints) + } + { + svc := sprint_svc.New() + endpoints := sprint_endpoints.MakeEndpoints(svc) + srv := sprint_grpctransport.MakeGRPCServer(ctx, endpoints) + sprint_pb.RegisterSprintServiceServer(s, srv) + sprint_httptransport.RegisterHandlers(ctx, svc, mux, endpoints) + } + + // start servers + go func() { + c := make(chan os.Signal, 1) + signal.Notify(c, syscall.SIGINT, syscall.SIGTERM) + errc <- fmt.Errorf("%s", <-c) + }() + + go func() { + logger := log.NewContext(logger).With("transport", "HTTP") + logger.Log("addr", ":8000") + errc <- http.ListenAndServe(":8000", handlers.LoggingHandler(os.Stderr, mux)) + }() + + go func() { + logger := log.NewContext(logger).With("transport", "gRPC") + ln, err := net.Listen("tcp", ":9000") + if err != nil { + errc <- err + return + } + logger.Log("addr", ":9000") + errc <- s.Serve(ln) + }() + + logger.Log("exit", <-errc) +} diff --git a/examples/go-kit/services/session/gen/README.md b/examples/go-kit/services/session/gen/README.md new file mode 100644 index 0000000..e69de29 diff --git a/examples/go-kit/services/session/gen/endpoints/endpoints.go b/examples/go-kit/services/session/gen/endpoints/endpoints.go new file mode 100644 index 0000000..cc032f6 --- /dev/null +++ b/examples/go-kit/services/session/gen/endpoints/endpoints.go @@ -0,0 +1,63 @@ +package session_endpoints + +import ( + "fmt" + "github.com/go-kit/kit/endpoint" + pb "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/session/gen/pb" + context "golang.org/x/net/context" +) + +var _ = fmt.Errorf + +type Endpoints struct { + LoginEndpoint endpoint.Endpoint + + LogoutEndpoint endpoint.Endpoint +} + +func (e *Endpoints) Login(ctx context.Context, in *pb.LoginRequest) (*pb.LoginResponse, error) { + out, err := e.LoginEndpoint(ctx, in) + if err != nil { + return &pb.LoginResponse{ErrMsg: err.Error()}, err + } + return out.(*pb.LoginResponse), err +} + +func (e *Endpoints) Logout(ctx context.Context, in *pb.LogoutRequest) (*pb.LogoutResponse, error) { + out, err := e.LogoutEndpoint(ctx, in) + if err != nil { + return &pb.LogoutResponse{ErrMsg: err.Error()}, err + } + return out.(*pb.LogoutResponse), err +} + +func MakeLoginEndpoint(svc pb.SessionServiceServer) endpoint.Endpoint { + return func(ctx context.Context, request interface{}) (interface{}, error) { + req := request.(*pb.LoginRequest) + rep, err := svc.Login(ctx, req) + if err != nil { + return &pb.LoginResponse{ErrMsg: err.Error()}, err + } + return rep, nil + } +} + +func MakeLogoutEndpoint(svc pb.SessionServiceServer) endpoint.Endpoint { + return func(ctx context.Context, request interface{}) (interface{}, error) { + req := request.(*pb.LogoutRequest) + rep, err := svc.Logout(ctx, req) + if err != nil { + return &pb.LogoutResponse{ErrMsg: err.Error()}, err + } + return rep, nil + } +} + +func MakeEndpoints(svc pb.SessionServiceServer) Endpoints { + return Endpoints{ + + LoginEndpoint: MakeLoginEndpoint(svc), + + LogoutEndpoint: MakeLogoutEndpoint(svc), + } +} diff --git a/examples/go-kit/services/session/gen/pb/pb.go b/examples/go-kit/services/session/gen/pb/pb.go new file mode 100644 index 0000000..375f7f5 --- /dev/null +++ b/examples/go-kit/services/session/gen/pb/pb.go @@ -0,0 +1,250 @@ +// Code generated by protoc-gen-gogo. +// source: services/session/session.proto +// DO NOT EDIT! + +/* +Package session is a generated protocol buffer package. + +It is generated from these files: + services/session/session.proto + +It has these top-level messages: + LoginRequest + LoginResponse + LogoutRequest + LogoutResponse +*/ +package session + +import proto "github.com/gogo/protobuf/proto" +import fmt "fmt" +import math "math" + +import ( + context "golang.org/x/net/context" + grpc "google.golang.org/grpc" +) + +// Reference imports to suppress errors if they are not otherwise used. +var _ = proto.Marshal +var _ = fmt.Errorf +var _ = math.Inf + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the proto package it is being compiled against. +// A compilation error at this line likely means your copy of the +// proto package needs to be updated. +const _ = proto.GoGoProtoPackageIsVersion2 // please upgrade the proto package + +type LoginRequest struct { + Username string `protobuf:"bytes,1,opt,name=username,proto3" json:"username,omitempty"` + Password string `protobuf:"bytes,2,opt,name=password,proto3" json:"password,omitempty"` +} + +func (m *LoginRequest) Reset() { *m = LoginRequest{} } +func (m *LoginRequest) String() string { return proto.CompactTextString(m) } +func (*LoginRequest) ProtoMessage() {} +func (*LoginRequest) Descriptor() ([]byte, []int) { return fileDescriptorSession, []int{0} } + +func (m *LoginRequest) GetUsername() string { + if m != nil { + return m.Username + } + return "" +} + +func (m *LoginRequest) GetPassword() string { + if m != nil { + return m.Password + } + return "" +} + +type LoginResponse struct { + Token string `protobuf:"bytes,1,opt,name=token,proto3" json:"token,omitempty"` + ErrMsg string `protobuf:"bytes,2,opt,name=err_msg,json=errMsg,proto3" json:"err_msg,omitempty"` +} + +func (m *LoginResponse) Reset() { *m = LoginResponse{} } +func (m *LoginResponse) String() string { return proto.CompactTextString(m) } +func (*LoginResponse) ProtoMessage() {} +func (*LoginResponse) Descriptor() ([]byte, []int) { return fileDescriptorSession, []int{1} } + +func (m *LoginResponse) GetToken() string { + if m != nil { + return m.Token + } + return "" +} + +func (m *LoginResponse) GetErrMsg() string { + if m != nil { + return m.ErrMsg + } + return "" +} + +type LogoutRequest struct { + Token string `protobuf:"bytes,1,opt,name=token,proto3" json:"token,omitempty"` +} + +func (m *LogoutRequest) Reset() { *m = LogoutRequest{} } +func (m *LogoutRequest) String() string { return proto.CompactTextString(m) } +func (*LogoutRequest) ProtoMessage() {} +func (*LogoutRequest) Descriptor() ([]byte, []int) { return fileDescriptorSession, []int{2} } + +func (m *LogoutRequest) GetToken() string { + if m != nil { + return m.Token + } + return "" +} + +type LogoutResponse struct { + ErrMsg string `protobuf:"bytes,1,opt,name=err_msg,json=errMsg,proto3" json:"err_msg,omitempty"` +} + +func (m *LogoutResponse) Reset() { *m = LogoutResponse{} } +func (m *LogoutResponse) String() string { return proto.CompactTextString(m) } +func (*LogoutResponse) ProtoMessage() {} +func (*LogoutResponse) Descriptor() ([]byte, []int) { return fileDescriptorSession, []int{3} } + +func (m *LogoutResponse) GetErrMsg() string { + if m != nil { + return m.ErrMsg + } + return "" +} + +func init() { + proto.RegisterType((*LoginRequest)(nil), "session.LoginRequest") + proto.RegisterType((*LoginResponse)(nil), "session.LoginResponse") + proto.RegisterType((*LogoutRequest)(nil), "session.LogoutRequest") + proto.RegisterType((*LogoutResponse)(nil), "session.LogoutResponse") +} + +// Reference imports to suppress errors if they are not otherwise used. +var _ context.Context +var _ grpc.ClientConn + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the grpc package it is being compiled against. +const _ = grpc.SupportPackageIsVersion4 + +// Client API for SessionService service + +type SessionServiceClient interface { + Login(ctx context.Context, in *LoginRequest, opts ...grpc.CallOption) (*LoginResponse, error) + Logout(ctx context.Context, in *LogoutRequest, opts ...grpc.CallOption) (*LogoutResponse, error) +} + +type sessionServiceClient struct { + cc *grpc.ClientConn +} + +func NewSessionServiceClient(cc *grpc.ClientConn) SessionServiceClient { + return &sessionServiceClient{cc} +} + +func (c *sessionServiceClient) Login(ctx context.Context, in *LoginRequest, opts ...grpc.CallOption) (*LoginResponse, error) { + out := new(LoginResponse) + err := grpc.Invoke(ctx, "/session.SessionService/Login", in, out, c.cc, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +func (c *sessionServiceClient) Logout(ctx context.Context, in *LogoutRequest, opts ...grpc.CallOption) (*LogoutResponse, error) { + out := new(LogoutResponse) + err := grpc.Invoke(ctx, "/session.SessionService/Logout", in, out, c.cc, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +// Server API for SessionService service + +type SessionServiceServer interface { + Login(context.Context, *LoginRequest) (*LoginResponse, error) + Logout(context.Context, *LogoutRequest) (*LogoutResponse, error) +} + +func RegisterSessionServiceServer(s *grpc.Server, srv SessionServiceServer) { + s.RegisterService(&_SessionService_serviceDesc, srv) +} + +func _SessionService_Login_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(LoginRequest) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(SessionServiceServer).Login(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: "/session.SessionService/Login", + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(SessionServiceServer).Login(ctx, req.(*LoginRequest)) + } + return interceptor(ctx, in, info, handler) +} + +func _SessionService_Logout_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(LogoutRequest) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(SessionServiceServer).Logout(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: "/session.SessionService/Logout", + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(SessionServiceServer).Logout(ctx, req.(*LogoutRequest)) + } + return interceptor(ctx, in, info, handler) +} + +var _SessionService_serviceDesc = grpc.ServiceDesc{ + ServiceName: "session.SessionService", + HandlerType: (*SessionServiceServer)(nil), + Methods: []grpc.MethodDesc{ + { + MethodName: "Login", + Handler: _SessionService_Login_Handler, + }, + { + MethodName: "Logout", + Handler: _SessionService_Logout_Handler, + }, + }, + Streams: []grpc.StreamDesc{}, + Metadata: "services/session/session.proto", +} + +func init() { proto.RegisterFile("services/session/session.proto", fileDescriptorSession) } + +var fileDescriptorSession = []byte{ + // 231 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0x6c, 0x50, 0xcb, 0x4a, 0xc5, 0x30, + 0x10, 0x35, 0xc2, 0xed, 0xd5, 0x41, 0xef, 0x62, 0x50, 0x6f, 0xe9, 0x42, 0x24, 0x20, 0xe8, 0xa6, + 0x82, 0x6e, 0x04, 0xc1, 0xa5, 0x2b, 0xdd, 0xb4, 0x1f, 0x20, 0x55, 0x87, 0x52, 0xa4, 0x99, 0x3a, + 0x93, 0xea, 0x27, 0xf8, 0xdb, 0x62, 0x93, 0x68, 0x7d, 0xac, 0xc2, 0xc9, 0xcc, 0x79, 0xcc, 0x81, + 0x43, 0x25, 0x79, 0xed, 0x1e, 0x49, 0xcf, 0x94, 0x54, 0x3b, 0x76, 0xe9, 0x2d, 0x07, 0x61, 0xcf, + 0xb8, 0x8c, 0xd0, 0xde, 0xc0, 0xce, 0x2d, 0xb7, 0x9d, 0xab, 0xe8, 0x65, 0x24, 0xf5, 0x58, 0xc0, + 0xd6, 0xa8, 0x24, 0xae, 0xe9, 0x29, 0x37, 0x47, 0xe6, 0x64, 0xbb, 0xfa, 0xc2, 0x9f, 0xb3, 0xa1, + 0x51, 0x7d, 0x63, 0x79, 0xca, 0x37, 0xc3, 0x2c, 0x61, 0x7b, 0x0d, 0xbb, 0x51, 0x47, 0x07, 0x76, + 0x4a, 0xb8, 0x07, 0x0b, 0xcf, 0xcf, 0xe4, 0xa2, 0x4a, 0x00, 0xb8, 0x86, 0x25, 0x89, 0xdc, 0xf7, + 0xda, 0x46, 0x85, 0x8c, 0x44, 0xee, 0xb4, 0xb5, 0xc7, 0x13, 0x9f, 0x47, 0x9f, 0x82, 0xfc, 0xcb, + 0xb7, 0xa7, 0xb0, 0x4a, 0x6b, 0xd1, 0x67, 0xa6, 0x68, 0xe6, 0x8a, 0xe7, 0xef, 0x06, 0x56, 0x75, + 0xb8, 0xb2, 0x0e, 0x65, 0xe0, 0x25, 0x2c, 0xa6, 0x90, 0xb8, 0x5f, 0xa6, 0x3a, 0xe6, 0xc7, 0x17, + 0x07, 0xbf, 0xbf, 0x83, 0x87, 0xdd, 0xc0, 0x2b, 0xc8, 0x82, 0x2f, 0xfe, 0xd8, 0xf9, 0xce, 0x5b, + 0xac, 0xff, 0xfc, 0x27, 0xf2, 0x43, 0x36, 0x75, 0x7e, 0xf1, 0x11, 0x00, 0x00, 0xff, 0xff, 0xc7, + 0x91, 0x50, 0x79, 0x95, 0x01, 0x00, 0x00, +} diff --git a/examples/go-kit/services/session/gen/transports/grpc/grpc.go b/examples/go-kit/services/session/gen/transports/grpc/grpc.go new file mode 100644 index 0000000..7053979 --- /dev/null +++ b/examples/go-kit/services/session/gen/transports/grpc/grpc.go @@ -0,0 +1,75 @@ +package session_grpctransport + +import ( + "fmt" + + grpctransport "github.com/go-kit/kit/transport/grpc" + endpoint "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/session/gen/endpoints" + pb "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/session/gen/pb" + context "golang.org/x/net/context" +) + +// avoid import errors +var _ = fmt.Errorf + +func MakeGRPCServer(ctx context.Context, endpoints endpoint.Endpoints) pb.SessionServiceServer { + options := []grpctransport.ServerOption{} + return &grpcServer{ + + login: grpctransport.NewServer( + ctx, + endpoints.LoginEndpoint, + decodeLoginRequest, + encodeLoginResponse, + options..., + ), + + logout: grpctransport.NewServer( + ctx, + endpoints.LogoutEndpoint, + decodeLogoutRequest, + encodeLogoutResponse, + options..., + ), + } +} + +type grpcServer struct { + login grpctransport.Handler + + logout grpctransport.Handler +} + +func (s *grpcServer) Login(ctx context.Context, req *pb.LoginRequest) (*pb.LoginResponse, error) { + _, rep, err := s.login.ServeGRPC(ctx, req) + if err != nil { + return nil, err + } + return rep.(*pb.LoginResponse), nil +} + +func decodeLoginRequest(ctx context.Context, grpcReq interface{}) (interface{}, error) { + return grpcReq, nil +} + +func encodeLoginResponse(ctx context.Context, response interface{}) (interface{}, error) { + resp := response.(*pb.LoginResponse) + return resp, nil +} + +func (s *grpcServer) Logout(ctx context.Context, req *pb.LogoutRequest) (*pb.LogoutResponse, error) { + _, rep, err := s.logout.ServeGRPC(ctx, req) + if err != nil { + return nil, err + } + return rep.(*pb.LogoutResponse), nil +} + +func decodeLogoutRequest(ctx context.Context, grpcReq interface{}) (interface{}, error) { + return grpcReq, nil +} + +func encodeLogoutResponse(ctx context.Context, response interface{}) (interface{}, error) { + resp := response.(*pb.LogoutResponse) + return resp, nil +} diff --git a/examples/go-kit/services/session/gen/transports/http/http.go b/examples/go-kit/services/session/gen/transports/http/http.go new file mode 100644 index 0000000..93aedb6 --- /dev/null +++ b/examples/go-kit/services/session/gen/transports/http/http.go @@ -0,0 +1,68 @@ +package session_httptransport + +import ( + "encoding/json" + context "golang.org/x/net/context" + "log" + "net/http" + + gokit_endpoint "github.com/go-kit/kit/endpoint" + httptransport "github.com/go-kit/kit/transport/http" + endpoints "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/session/gen/endpoints" + pb "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/session/gen/pb" +) + +func MakeLoginHandler(ctx context.Context, svc pb.SessionServiceServer, endpoint gokit_endpoint.Endpoint) *httptransport.Server { + return httptransport.NewServer( + ctx, + endpoint, + decodeLoginRequest, + encodeLoginResponse, + []httptransport.ServerOption{}..., + ) +} + +func decodeLoginRequest(ctx context.Context, r *http.Request) (interface{}, error) { + var req pb.LoginRequest + if err := json.NewDecoder(r.Body).Decode(&req); err != nil { + return nil, err + } + return &req, nil +} + +func encodeLoginResponse(ctx context.Context, w http.ResponseWriter, response interface{}) error { + return json.NewEncoder(w).Encode(response) +} + +func MakeLogoutHandler(ctx context.Context, svc pb.SessionServiceServer, endpoint gokit_endpoint.Endpoint) *httptransport.Server { + return httptransport.NewServer( + ctx, + endpoint, + decodeLogoutRequest, + encodeLogoutResponse, + []httptransport.ServerOption{}..., + ) +} + +func decodeLogoutRequest(ctx context.Context, r *http.Request) (interface{}, error) { + var req pb.LogoutRequest + if err := json.NewDecoder(r.Body).Decode(&req); err != nil { + return nil, err + } + return &req, nil +} + +func encodeLogoutResponse(ctx context.Context, w http.ResponseWriter, response interface{}) error { + return json.NewEncoder(w).Encode(response) +} + +func RegisterHandlers(ctx context.Context, 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)) + + log.Println("new HTTP endpoint: \"/Logout\" (service=Session)") + mux.Handle("/Logout", MakeLogoutHandler(ctx, svc, endpoints.LogoutEndpoint)) + + return nil +} diff --git a/examples/go-kit/services/session/service.go b/examples/go-kit/services/session/service.go new file mode 100644 index 0000000..9e5134f --- /dev/null +++ b/examples/go-kit/services/session/service.go @@ -0,0 +1,23 @@ +package sessionsvc + +import ( + "fmt" + + "golang.org/x/net/context" + + pb "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/session/gen/pb" +) + +type Service struct{} + +func New() pb.SessionServiceServer { + return &Service{} +} + +func (svc *Service) Login(ctx context.Context, in *pb.LoginRequest) (*pb.LoginResponse, error) { + return nil, fmt.Errorf("not implemented") +} + +func (svc *Service) Logout(ctx context.Context, in *pb.LogoutRequest) (*pb.LogoutResponse, error) { + return nil, fmt.Errorf("not implemented") +} diff --git a/examples/go-kit/services/session/session.proto b/examples/go-kit/services/session/session.proto new file mode 100644 index 0000000..55f78a9 --- /dev/null +++ b/examples/go-kit/services/session/session.proto @@ -0,0 +1,26 @@ +syntax = "proto3"; + +package session; + +service SessionService { + rpc Login(LoginRequest) returns (LoginResponse) {} + rpc Logout(LogoutRequest) returns (LogoutResponse) {} +} + +message LoginRequest { + string username = 1; + string password = 2; +} + +message LoginResponse { + string token = 1; + string err_msg = 2; +} + +message LogoutRequest { + string token = 1; +} + +message LogoutResponse { + string err_msg = 1; +} \ No newline at end of file diff --git a/examples/go-kit/services/sprint/gen/README.md b/examples/go-kit/services/sprint/gen/README.md new file mode 100644 index 0000000..e69de29 diff --git a/examples/go-kit/services/sprint/gen/endpoints/endpoints.go b/examples/go-kit/services/sprint/gen/endpoints/endpoints.go new file mode 100644 index 0000000..56a0fe5 --- /dev/null +++ b/examples/go-kit/services/sprint/gen/endpoints/endpoints.go @@ -0,0 +1,86 @@ +package sprint_endpoints + +import ( + "fmt" + "github.com/go-kit/kit/endpoint" + pb "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/sprint/gen/pb" + context "golang.org/x/net/context" +) + +var _ = fmt.Errorf + +type Endpoints struct { + AddSprintEndpoint endpoint.Endpoint + + CloseSprintEndpoint endpoint.Endpoint + + GetSprintEndpoint endpoint.Endpoint +} + +func (e *Endpoints) AddSprint(ctx context.Context, in *pb.AddSprintRequest) (*pb.AddSprintResponse, error) { + out, err := e.AddSprintEndpoint(ctx, in) + if err != nil { + return &pb.AddSprintResponse{ErrMsg: err.Error()}, err + } + return out.(*pb.AddSprintResponse), err +} + +func (e *Endpoints) CloseSprint(ctx context.Context, in *pb.CloseSprintRequest) (*pb.CloseSprintResponse, error) { + out, err := e.CloseSprintEndpoint(ctx, in) + if err != nil { + return &pb.CloseSprintResponse{ErrMsg: err.Error()}, err + } + return out.(*pb.CloseSprintResponse), err +} + +func (e *Endpoints) GetSprint(ctx context.Context, in *pb.GetSprintRequest) (*pb.GetSprintResponse, error) { + out, err := e.GetSprintEndpoint(ctx, in) + if err != nil { + return &pb.GetSprintResponse{ErrMsg: err.Error()}, err + } + return out.(*pb.GetSprintResponse), err +} + +func MakeAddSprintEndpoint(svc pb.SprintServiceServer) endpoint.Endpoint { + return func(ctx context.Context, request interface{}) (interface{}, error) { + req := request.(*pb.AddSprintRequest) + rep, err := svc.AddSprint(ctx, req) + if err != nil { + return &pb.AddSprintResponse{ErrMsg: err.Error()}, err + } + return rep, nil + } +} + +func MakeCloseSprintEndpoint(svc pb.SprintServiceServer) endpoint.Endpoint { + return func(ctx context.Context, request interface{}) (interface{}, error) { + req := request.(*pb.CloseSprintRequest) + rep, err := svc.CloseSprint(ctx, req) + if err != nil { + return &pb.CloseSprintResponse{ErrMsg: err.Error()}, err + } + return rep, nil + } +} + +func MakeGetSprintEndpoint(svc pb.SprintServiceServer) endpoint.Endpoint { + return func(ctx context.Context, request interface{}) (interface{}, error) { + req := request.(*pb.GetSprintRequest) + rep, err := svc.GetSprint(ctx, req) + if err != nil { + return &pb.GetSprintResponse{ErrMsg: err.Error()}, err + } + return rep, nil + } +} + +func MakeEndpoints(svc pb.SprintServiceServer) Endpoints { + return Endpoints{ + + AddSprintEndpoint: MakeAddSprintEndpoint(svc), + + CloseSprintEndpoint: MakeCloseSprintEndpoint(svc), + + GetSprintEndpoint: MakeGetSprintEndpoint(svc), + } +} diff --git a/examples/go-kit/services/sprint/gen/pb/pb.go b/examples/go-kit/services/sprint/gen/pb/pb.go new file mode 100644 index 0000000..d06ebf4 --- /dev/null +++ b/examples/go-kit/services/sprint/gen/pb/pb.go @@ -0,0 +1,357 @@ +// Code generated by protoc-gen-gogo. +// source: services/sprint/sprint.proto +// DO NOT EDIT! + +/* +Package sprint is a generated protocol buffer package. + +It is generated from these files: + services/sprint/sprint.proto + +It has these top-level messages: + AddSprintRequest + AddSprintResponse + CloseSprintRequest + CloseSprintResponse + GetSprintRequest + GetSprintResponse + Sprint +*/ +package sprint + +import proto "github.com/gogo/protobuf/proto" +import fmt "fmt" +import math "math" + +import ( + context "golang.org/x/net/context" + grpc "google.golang.org/grpc" +) + +// Reference imports to suppress errors if they are not otherwise used. +var _ = proto.Marshal +var _ = fmt.Errorf +var _ = math.Inf + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the proto package it is being compiled against. +// A compilation error at this line likely means your copy of the +// proto package needs to be updated. +const _ = proto.GoGoProtoPackageIsVersion2 // please upgrade the proto package + +type AddSprintRequest struct { + Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"` +} + +func (m *AddSprintRequest) Reset() { *m = AddSprintRequest{} } +func (m *AddSprintRequest) String() string { return proto.CompactTextString(m) } +func (*AddSprintRequest) ProtoMessage() {} +func (*AddSprintRequest) Descriptor() ([]byte, []int) { return fileDescriptorSprint, []int{0} } + +func (m *AddSprintRequest) GetName() string { + if m != nil { + return m.Name + } + return "" +} + +type AddSprintResponse struct { + Sprint *Sprint `protobuf:"bytes,1,opt,name=sprint" json:"sprint,omitempty"` + ErrMsg string `protobuf:"bytes,2,opt,name=err_msg,json=errMsg,proto3" json:"err_msg,omitempty"` +} + +func (m *AddSprintResponse) Reset() { *m = AddSprintResponse{} } +func (m *AddSprintResponse) String() string { return proto.CompactTextString(m) } +func (*AddSprintResponse) ProtoMessage() {} +func (*AddSprintResponse) Descriptor() ([]byte, []int) { return fileDescriptorSprint, []int{1} } + +func (m *AddSprintResponse) GetSprint() *Sprint { + if m != nil { + return m.Sprint + } + return nil +} + +func (m *AddSprintResponse) GetErrMsg() string { + if m != nil { + return m.ErrMsg + } + return "" +} + +type CloseSprintRequest struct { + Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"` +} + +func (m *CloseSprintRequest) Reset() { *m = CloseSprintRequest{} } +func (m *CloseSprintRequest) String() string { return proto.CompactTextString(m) } +func (*CloseSprintRequest) ProtoMessage() {} +func (*CloseSprintRequest) Descriptor() ([]byte, []int) { return fileDescriptorSprint, []int{2} } + +func (m *CloseSprintRequest) GetId() string { + if m != nil { + return m.Id + } + return "" +} + +type CloseSprintResponse struct { + ErrMsg string `protobuf:"bytes,1,opt,name=err_msg,json=errMsg,proto3" json:"err_msg,omitempty"` +} + +func (m *CloseSprintResponse) Reset() { *m = CloseSprintResponse{} } +func (m *CloseSprintResponse) String() string { return proto.CompactTextString(m) } +func (*CloseSprintResponse) ProtoMessage() {} +func (*CloseSprintResponse) Descriptor() ([]byte, []int) { return fileDescriptorSprint, []int{3} } + +func (m *CloseSprintResponse) GetErrMsg() string { + if m != nil { + return m.ErrMsg + } + return "" +} + +type GetSprintRequest struct { + Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"` +} + +func (m *GetSprintRequest) Reset() { *m = GetSprintRequest{} } +func (m *GetSprintRequest) String() string { return proto.CompactTextString(m) } +func (*GetSprintRequest) ProtoMessage() {} +func (*GetSprintRequest) Descriptor() ([]byte, []int) { return fileDescriptorSprint, []int{4} } + +func (m *GetSprintRequest) GetId() string { + if m != nil { + return m.Id + } + return "" +} + +type GetSprintResponse struct { + Sprint *Sprint `protobuf:"bytes,1,opt,name=sprint" json:"sprint,omitempty"` + ErrMsg string `protobuf:"bytes,2,opt,name=err_msg,json=errMsg,proto3" json:"err_msg,omitempty"` +} + +func (m *GetSprintResponse) Reset() { *m = GetSprintResponse{} } +func (m *GetSprintResponse) String() string { return proto.CompactTextString(m) } +func (*GetSprintResponse) ProtoMessage() {} +func (*GetSprintResponse) Descriptor() ([]byte, []int) { return fileDescriptorSprint, []int{5} } + +func (m *GetSprintResponse) GetSprint() *Sprint { + if m != nil { + return m.Sprint + } + return nil +} + +func (m *GetSprintResponse) GetErrMsg() string { + if m != nil { + return m.ErrMsg + } + return "" +} + +type Sprint struct { + Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"` + CreatedAt uint32 `protobuf:"varint,2,opt,name=created_at,json=createdAt,proto3" json:"created_at,omitempty"` + Name string `protobuf:"bytes,3,opt,name=name,proto3" json:"name,omitempty"` +} + +func (m *Sprint) Reset() { *m = Sprint{} } +func (m *Sprint) String() string { return proto.CompactTextString(m) } +func (*Sprint) ProtoMessage() {} +func (*Sprint) Descriptor() ([]byte, []int) { return fileDescriptorSprint, []int{6} } + +func (m *Sprint) GetId() string { + if m != nil { + return m.Id + } + return "" +} + +func (m *Sprint) GetCreatedAt() uint32 { + if m != nil { + return m.CreatedAt + } + return 0 +} + +func (m *Sprint) GetName() string { + if m != nil { + return m.Name + } + return "" +} + +func init() { + proto.RegisterType((*AddSprintRequest)(nil), "sprint.AddSprintRequest") + proto.RegisterType((*AddSprintResponse)(nil), "sprint.AddSprintResponse") + proto.RegisterType((*CloseSprintRequest)(nil), "sprint.CloseSprintRequest") + proto.RegisterType((*CloseSprintResponse)(nil), "sprint.CloseSprintResponse") + proto.RegisterType((*GetSprintRequest)(nil), "sprint.GetSprintRequest") + proto.RegisterType((*GetSprintResponse)(nil), "sprint.GetSprintResponse") + proto.RegisterType((*Sprint)(nil), "sprint.Sprint") +} + +// Reference imports to suppress errors if they are not otherwise used. +var _ context.Context +var _ grpc.ClientConn + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the grpc package it is being compiled against. +const _ = grpc.SupportPackageIsVersion4 + +// Client API for SprintService service + +type SprintServiceClient interface { + AddSprint(ctx context.Context, in *AddSprintRequest, opts ...grpc.CallOption) (*AddSprintResponse, error) + CloseSprint(ctx context.Context, in *CloseSprintRequest, opts ...grpc.CallOption) (*CloseSprintResponse, error) + GetSprint(ctx context.Context, in *GetSprintRequest, opts ...grpc.CallOption) (*GetSprintResponse, error) +} + +type sprintServiceClient struct { + cc *grpc.ClientConn +} + +func NewSprintServiceClient(cc *grpc.ClientConn) SprintServiceClient { + return &sprintServiceClient{cc} +} + +func (c *sprintServiceClient) AddSprint(ctx context.Context, in *AddSprintRequest, opts ...grpc.CallOption) (*AddSprintResponse, error) { + out := new(AddSprintResponse) + err := grpc.Invoke(ctx, "/sprint.SprintService/AddSprint", in, out, c.cc, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +func (c *sprintServiceClient) CloseSprint(ctx context.Context, in *CloseSprintRequest, opts ...grpc.CallOption) (*CloseSprintResponse, error) { + out := new(CloseSprintResponse) + err := grpc.Invoke(ctx, "/sprint.SprintService/CloseSprint", in, out, c.cc, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +func (c *sprintServiceClient) GetSprint(ctx context.Context, in *GetSprintRequest, opts ...grpc.CallOption) (*GetSprintResponse, error) { + out := new(GetSprintResponse) + err := grpc.Invoke(ctx, "/sprint.SprintService/GetSprint", in, out, c.cc, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +// Server API for SprintService service + +type SprintServiceServer interface { + AddSprint(context.Context, *AddSprintRequest) (*AddSprintResponse, error) + CloseSprint(context.Context, *CloseSprintRequest) (*CloseSprintResponse, error) + GetSprint(context.Context, *GetSprintRequest) (*GetSprintResponse, error) +} + +func RegisterSprintServiceServer(s *grpc.Server, srv SprintServiceServer) { + s.RegisterService(&_SprintService_serviceDesc, srv) +} + +func _SprintService_AddSprint_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(AddSprintRequest) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(SprintServiceServer).AddSprint(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: "/sprint.SprintService/AddSprint", + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(SprintServiceServer).AddSprint(ctx, req.(*AddSprintRequest)) + } + return interceptor(ctx, in, info, handler) +} + +func _SprintService_CloseSprint_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(CloseSprintRequest) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(SprintServiceServer).CloseSprint(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: "/sprint.SprintService/CloseSprint", + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(SprintServiceServer).CloseSprint(ctx, req.(*CloseSprintRequest)) + } + return interceptor(ctx, in, info, handler) +} + +func _SprintService_GetSprint_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(GetSprintRequest) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(SprintServiceServer).GetSprint(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: "/sprint.SprintService/GetSprint", + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(SprintServiceServer).GetSprint(ctx, req.(*GetSprintRequest)) + } + return interceptor(ctx, in, info, handler) +} + +var _SprintService_serviceDesc = grpc.ServiceDesc{ + ServiceName: "sprint.SprintService", + HandlerType: (*SprintServiceServer)(nil), + Methods: []grpc.MethodDesc{ + { + MethodName: "AddSprint", + Handler: _SprintService_AddSprint_Handler, + }, + { + MethodName: "CloseSprint", + Handler: _SprintService_CloseSprint_Handler, + }, + { + MethodName: "GetSprint", + Handler: _SprintService_GetSprint_Handler, + }, + }, + Streams: []grpc.StreamDesc{}, + Metadata: "services/sprint/sprint.proto", +} + +func init() { proto.RegisterFile("services/sprint/sprint.proto", fileDescriptorSprint) } + +var fileDescriptorSprint = []byte{ + // 290 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0xac, 0x52, 0x4d, 0x4b, 0xc3, 0x40, + 0x10, 0x6d, 0xaa, 0x44, 0x32, 0xa5, 0xa5, 0x1d, 0x0f, 0xc6, 0xa8, 0x20, 0x8b, 0x14, 0x4f, 0x11, + 0xea, 0x2f, 0x68, 0x3d, 0x28, 0x88, 0x97, 0xb4, 0xf7, 0x12, 0xbb, 0x43, 0x09, 0xd8, 0x24, 0xee, + 0xac, 0xfe, 0x5f, 0xff, 0x89, 0xb0, 0xbb, 0xcd, 0x57, 0x8b, 0x27, 0x4f, 0xc9, 0xec, 0xbc, 0x7d, + 0x6f, 0xe6, 0xbd, 0x85, 0x6b, 0x26, 0xf5, 0x9d, 0x6d, 0x88, 0x1f, 0xb8, 0x54, 0x59, 0xae, 0xdd, + 0x27, 0x2e, 0x55, 0xa1, 0x0b, 0xf4, 0x6d, 0x25, 0xa6, 0x30, 0x9e, 0x4b, 0xb9, 0x34, 0x45, 0x42, + 0x9f, 0x5f, 0xc4, 0x1a, 0x11, 0x4e, 0xf3, 0x74, 0x47, 0xa1, 0x77, 0xeb, 0xdd, 0x07, 0x89, 0xf9, + 0x17, 0x2b, 0x98, 0x34, 0x70, 0x5c, 0x16, 0x39, 0x13, 0x4e, 0xc1, 0xd1, 0x18, 0xe8, 0x60, 0x36, + 0x8a, 0x9d, 0x86, 0xc3, 0xb9, 0x2e, 0x5e, 0xc0, 0x19, 0x29, 0xb5, 0xde, 0xf1, 0x36, 0xec, 0x1b, + 0x4e, 0x9f, 0x94, 0x7a, 0xe3, 0xad, 0xb8, 0x03, 0x7c, 0xfa, 0x28, 0x98, 0xda, 0xfa, 0x23, 0xe8, + 0x67, 0xd2, 0xa9, 0xf7, 0x33, 0x29, 0x62, 0x38, 0x6f, 0xa1, 0x9c, 0x7a, 0x83, 0xd5, 0x6b, 0xb1, + 0x0a, 0x18, 0x3f, 0x93, 0xfe, 0x9b, 0x73, 0x05, 0x93, 0x06, 0xe6, 0xbf, 0xf6, 0x79, 0x05, 0xdf, + 0x42, 0xbb, 0x7a, 0x78, 0x03, 0xb0, 0x51, 0x94, 0x6a, 0x92, 0xeb, 0x54, 0x9b, 0x5b, 0xc3, 0x24, + 0x70, 0x27, 0xf3, 0xda, 0xf2, 0x93, 0xda, 0xf2, 0xd9, 0x8f, 0x07, 0x43, 0xcb, 0xb6, 0xb4, 0x49, + 0xe2, 0x02, 0x82, 0x2a, 0x04, 0x0c, 0xf7, 0xc3, 0x75, 0xf3, 0x8b, 0x2e, 0x8f, 0x74, 0xec, 0x86, + 0xa2, 0x87, 0x2f, 0x30, 0x68, 0x98, 0x89, 0xd1, 0x1e, 0x7b, 0x98, 0x43, 0x74, 0x75, 0xb4, 0x57, + 0x31, 0x2d, 0x20, 0xa8, 0x2c, 0xac, 0xa7, 0xe9, 0x3a, 0x5f, 0x4f, 0x73, 0xe0, 0xb7, 0xe8, 0xbd, + 0xfb, 0xe6, 0x35, 0x3e, 0xfe, 0x06, 0x00, 0x00, 0xff, 0xff, 0xf0, 0x9e, 0xb2, 0x1e, 0xad, 0x02, + 0x00, 0x00, +} diff --git a/examples/go-kit/services/sprint/gen/transports/grpc/grpc.go b/examples/go-kit/services/sprint/gen/transports/grpc/grpc.go new file mode 100644 index 0000000..ce3c15f --- /dev/null +++ b/examples/go-kit/services/sprint/gen/transports/grpc/grpc.go @@ -0,0 +1,102 @@ +package sprint_grpctransport + +import ( + "fmt" + + grpctransport "github.com/go-kit/kit/transport/grpc" + endpoint "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/sprint/gen/endpoints" + pb "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/sprint/gen/pb" + context "golang.org/x/net/context" +) + +// avoid import errors +var _ = fmt.Errorf + +func MakeGRPCServer(ctx context.Context, endpoints endpoint.Endpoints) pb.SprintServiceServer { + options := []grpctransport.ServerOption{} + return &grpcServer{ + + addsprint: grpctransport.NewServer( + ctx, + endpoints.AddSprintEndpoint, + decodeAddSprintRequest, + encodeAddSprintResponse, + options..., + ), + + closesprint: grpctransport.NewServer( + ctx, + endpoints.CloseSprintEndpoint, + decodeCloseSprintRequest, + encodeCloseSprintResponse, + options..., + ), + + getsprint: grpctransport.NewServer( + ctx, + endpoints.GetSprintEndpoint, + decodeGetSprintRequest, + encodeGetSprintResponse, + options..., + ), + } +} + +type grpcServer struct { + addsprint grpctransport.Handler + + closesprint grpctransport.Handler + + getsprint grpctransport.Handler +} + +func (s *grpcServer) AddSprint(ctx context.Context, req *pb.AddSprintRequest) (*pb.AddSprintResponse, error) { + _, rep, err := s.addsprint.ServeGRPC(ctx, req) + if err != nil { + return nil, err + } + return rep.(*pb.AddSprintResponse), nil +} + +func decodeAddSprintRequest(ctx context.Context, grpcReq interface{}) (interface{}, error) { + return grpcReq, nil +} + +func encodeAddSprintResponse(ctx context.Context, response interface{}) (interface{}, error) { + resp := response.(*pb.AddSprintResponse) + return resp, nil +} + +func (s *grpcServer) CloseSprint(ctx context.Context, req *pb.CloseSprintRequest) (*pb.CloseSprintResponse, error) { + _, rep, err := s.closesprint.ServeGRPC(ctx, req) + if err != nil { + return nil, err + } + return rep.(*pb.CloseSprintResponse), nil +} + +func decodeCloseSprintRequest(ctx context.Context, grpcReq interface{}) (interface{}, error) { + return grpcReq, nil +} + +func encodeCloseSprintResponse(ctx context.Context, response interface{}) (interface{}, error) { + resp := response.(*pb.CloseSprintResponse) + return resp, nil +} + +func (s *grpcServer) GetSprint(ctx context.Context, req *pb.GetSprintRequest) (*pb.GetSprintResponse, error) { + _, rep, err := s.getsprint.ServeGRPC(ctx, req) + if err != nil { + return nil, err + } + return rep.(*pb.GetSprintResponse), nil +} + +func decodeGetSprintRequest(ctx context.Context, grpcReq interface{}) (interface{}, error) { + return grpcReq, nil +} + +func encodeGetSprintResponse(ctx context.Context, response interface{}) (interface{}, error) { + resp := response.(*pb.GetSprintResponse) + return resp, nil +} diff --git a/examples/go-kit/services/sprint/gen/transports/http/http.go b/examples/go-kit/services/sprint/gen/transports/http/http.go new file mode 100644 index 0000000..5d76962 --- /dev/null +++ b/examples/go-kit/services/sprint/gen/transports/http/http.go @@ -0,0 +1,93 @@ +package sprint_httptransport + +import ( + "encoding/json" + context "golang.org/x/net/context" + "log" + "net/http" + + gokit_endpoint "github.com/go-kit/kit/endpoint" + httptransport "github.com/go-kit/kit/transport/http" + endpoints "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/sprint/gen/endpoints" + pb "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/sprint/gen/pb" +) + +func MakeAddSprintHandler(ctx context.Context, svc pb.SprintServiceServer, endpoint gokit_endpoint.Endpoint) *httptransport.Server { + return httptransport.NewServer( + ctx, + endpoint, + decodeAddSprintRequest, + encodeAddSprintResponse, + []httptransport.ServerOption{}..., + ) +} + +func decodeAddSprintRequest(ctx context.Context, r *http.Request) (interface{}, error) { + var req pb.AddSprintRequest + if err := json.NewDecoder(r.Body).Decode(&req); err != nil { + return nil, err + } + return &req, nil +} + +func encodeAddSprintResponse(ctx context.Context, w http.ResponseWriter, response interface{}) error { + return json.NewEncoder(w).Encode(response) +} + +func MakeCloseSprintHandler(ctx context.Context, svc pb.SprintServiceServer, endpoint gokit_endpoint.Endpoint) *httptransport.Server { + return httptransport.NewServer( + ctx, + endpoint, + decodeCloseSprintRequest, + encodeCloseSprintResponse, + []httptransport.ServerOption{}..., + ) +} + +func decodeCloseSprintRequest(ctx context.Context, r *http.Request) (interface{}, error) { + var req pb.CloseSprintRequest + if err := json.NewDecoder(r.Body).Decode(&req); err != nil { + return nil, err + } + return &req, nil +} + +func encodeCloseSprintResponse(ctx context.Context, w http.ResponseWriter, response interface{}) error { + return json.NewEncoder(w).Encode(response) +} + +func MakeGetSprintHandler(ctx context.Context, svc pb.SprintServiceServer, endpoint gokit_endpoint.Endpoint) *httptransport.Server { + return httptransport.NewServer( + ctx, + endpoint, + decodeGetSprintRequest, + encodeGetSprintResponse, + []httptransport.ServerOption{}..., + ) +} + +func decodeGetSprintRequest(ctx context.Context, r *http.Request) (interface{}, error) { + var req pb.GetSprintRequest + if err := json.NewDecoder(r.Body).Decode(&req); err != nil { + return nil, err + } + return &req, nil +} + +func encodeGetSprintResponse(ctx context.Context, w http.ResponseWriter, response interface{}) error { + return json.NewEncoder(w).Encode(response) +} + +func RegisterHandlers(ctx context.Context, 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)) + + log.Println("new HTTP endpoint: \"/CloseSprint\" (service=Sprint)") + mux.Handle("/CloseSprint", MakeCloseSprintHandler(ctx, svc, endpoints.CloseSprintEndpoint)) + + log.Println("new HTTP endpoint: \"/GetSprint\" (service=Sprint)") + mux.Handle("/GetSprint", MakeGetSprintHandler(ctx, svc, endpoints.GetSprintEndpoint)) + + return nil +} diff --git a/examples/go-kit/services/sprint/service.go b/examples/go-kit/services/sprint/service.go new file mode 100644 index 0000000..6b26f7c --- /dev/null +++ b/examples/go-kit/services/sprint/service.go @@ -0,0 +1,27 @@ +package sprintsvc + +import ( + "fmt" + + "golang.org/x/net/context" + + pb "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/sprint/gen/pb" +) + +type Service struct{} + +func New() pb.SprintServiceServer { + return &Service{} +} + +func (svc *Service) AddSprint(ctx context.Context, in *pb.AddSprintRequest) (*pb.AddSprintResponse, error) { + return nil, fmt.Errorf("not implemented") +} + +func (svc *Service) CloseSprint(ctx context.Context, in *pb.CloseSprintRequest) (*pb.CloseSprintResponse, error) { + return nil, fmt.Errorf("not implemented") +} + +func (svc *Service) GetSprint(ctx context.Context, in *pb.GetSprintRequest) (*pb.GetSprintResponse, error) { + return nil, fmt.Errorf("not implemented") +} diff --git a/examples/go-kit/services/sprint/sprint.proto b/examples/go-kit/services/sprint/sprint.proto new file mode 100644 index 0000000..dfb6a33 --- /dev/null +++ b/examples/go-kit/services/sprint/sprint.proto @@ -0,0 +1,38 @@ +syntax = "proto3"; + +package sprint; + +service SprintService { + rpc AddSprint(AddSprintRequest) returns (AddSprintResponse) {} + rpc CloseSprint(CloseSprintRequest) returns (CloseSprintResponse) {} + rpc GetSprint(GetSprintRequest) returns (GetSprintResponse) {} +} + +message AddSprintRequest { + string name = 1; +} +message AddSprintResponse { + Sprint sprint = 1; + string err_msg = 2; +} + +message CloseSprintRequest { + string id = 1; +} +message CloseSprintResponse { + string err_msg = 1; +} + +message GetSprintRequest { + string id = 1; +} +message GetSprintResponse { + Sprint sprint = 1; + string err_msg = 2; +} + +message Sprint { + string id = 1; + uint32 created_at = 2; + string name = 3; +} \ No newline at end of file diff --git a/examples/go-kit/templates/{{.File.Package}}/gen/README.md.tmpl b/examples/go-kit/templates/{{.File.Package}}/gen/README.md.tmpl new file mode 100644 index 0000000..e69de29 diff --git a/examples/go-kit/templates/{{.File.Package}}/gen/endpoints/endpoints.go.tmpl b/examples/go-kit/templates/{{.File.Package}}/gen/endpoints/endpoints.go.tmpl new file mode 100644 index 0000000..557ef4a --- /dev/null +++ b/examples/go-kit/templates/{{.File.Package}}/gen/endpoints/endpoints.go.tmpl @@ -0,0 +1,49 @@ +package {{.File.Package}}_endpoints + +{{$file := .File}} + +import ( + "fmt" + context "golang.org/x/net/context" + pb "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/{{.File.Package}}/gen/pb" + "github.com/go-kit/kit/endpoint" +) + +var _ = fmt.Errorf + +type Endpoints struct { + {{range .Service.Method}} + {{.Name}}Endpoint endpoint.Endpoint + {{end}} +} + +{{range .Service.Method}} +func (e *Endpoints){{.Name}}(ctx context.Context, in *pb.{{.Name}}Request) (*pb.{{.Name}}Response, error) { + out, err := e.{{.Name}}Endpoint(ctx, in) + if err != nil { + return &pb.{{.Name}}Response{ErrMsg: err.Error()}, err + } + return out.(*pb.{{.Name}}Response), err +} +{{end}} + +{{range .Service.Method}} +func Make{{.Name}}Endpoint(svc pb.{{$file.Package | title}}ServiceServer) endpoint.Endpoint { + return func(ctx context.Context, request interface{}) (interface{}, error) { + req := request.(*pb.{{.Name}}Request) + rep, err := svc.{{.Name}}(ctx, req) + if err != nil { + return &pb.{{.Name}}Response{ErrMsg: err.Error()}, err + } + return rep, nil + } +} +{{end}} + +func MakeEndpoints(svc pb.{{.File.Package | title}}ServiceServer) Endpoints { + return Endpoints{ + {{range .Service.Method}} + {{.Name}}Endpoint: Make{{.Name}}Endpoint(svc), + {{end}} + } +} 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 new file mode 100644 index 0000000..41dc7d4 --- /dev/null +++ b/examples/go-kit/templates/{{.File.Package}}/gen/transports/grpc/grpc.go.tmpl @@ -0,0 +1,59 @@ +package {{.File.Package}}_grpctransport + +{{$file := .File}} + +import ( + "fmt" + + context "golang.org/x/net/context" + pb "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/{{.File.Package}}/gen/pb" + grpctransport "github.com/go-kit/kit/transport/grpc" + endpoint "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/{{.File.Package}}/gen/endpoints" +) + +// avoid import errors +var _ = fmt.Errorf + +func MakeGRPCServer(ctx context.Context, endpoints endpoint.Endpoints) pb.{{.File.Package | title}}ServiceServer { + options := []grpctransport.ServerOption{} + return &grpcServer{ + {{range .Service.Method}} + {{if not .ServerStreaming}} + {{if not .ClientStreaming}} + {{.Name | lower}}: grpctransport.NewServer( + ctx, + endpoints.{{.Name}}Endpoint, + decode{{.Name}}Request, + encode{{.Name}}Response, + options..., + ), + {{end}} + {{end}} + {{end}} + } +} + +type grpcServer struct { + {{range .Service.Method}} + {{.Name | lower}} grpctransport.Handler + {{end}} +} + +{{range .Service.Method}} +func (s *grpcServer) {{.Name}}(ctx context.Context, req *pb.{{.Name}}Request) (*pb.{{.Name}}Response, error) { + _, rep, err := s.{{.Name | lower}}.ServeGRPC(ctx, req) + if err != nil { + return nil, err + } + return rep.(*pb.{{.Name}}Response), nil +} + +func decode{{.Name}}Request(ctx context.Context, grpcReq interface{}) (interface{}, error) { + return grpcReq, nil +} + +func encode{{.Name}}Response(ctx context.Context, response interface{}) (interface{}, error) { + resp := response.(*pb.{{.Name}}Response) + return resp, nil +} +{{end}} 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 new file mode 100644 index 0000000..8adc0f3 --- /dev/null +++ b/examples/go-kit/templates/{{.File.Package}}/gen/transports/http/http.go.tmpl @@ -0,0 +1,47 @@ +package {{.File.Package}}_httptransport + +{{$file := .File}} + +import ( + "log" + "net/http" + "encoding/json" + context "golang.org/x/net/context" + + pb "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/{{.File.Package}}/gen/pb" + gokit_endpoint "github.com/go-kit/kit/endpoint" + httptransport "github.com/go-kit/kit/transport/http" + endpoints "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/{{.File.Package}}/gen/endpoints" +) + +{{range .Service.Method}} +func Make{{.Name}}Handler(ctx context.Context, svc pb.{{$file.Package | title}}ServiceServer, endpoint gokit_endpoint.Endpoint) *httptransport.Server { + return httptransport.NewServer( + ctx, + endpoint, + decode{{.Name}}Request, + encode{{.Name}}Response, + []httptransport.ServerOption{}..., + ) +} + +func decode{{.Name}}Request(ctx context.Context, r *http.Request) (interface{}, error) { + var req pb.{{.Name}}Request + if err := json.NewDecoder(r.Body).Decode(&req); err != nil { + return nil, err + } + return &req, nil +} + +func encode{{.Name}}Response(ctx context.Context, w http.ResponseWriter, response interface{}) error { + return json.NewEncoder(w).Encode(response) +} +{{end}} + +func RegisterHandlers(ctx context.Context, svc pb.{{$file.Package | title}}ServiceServer, mux *http.ServeMux, endpoints endpoints.Endpoints) error { + {{range .Service.Method}} + log.Println("new HTTP endpoint: \"/{{.Name}}\" (service={{$file.Package | title}})") + mux.Handle("/{{.Name}}", Make{{.Name}}Handler(ctx, svc, endpoints.{{.Name}}Endpoint)) + {{end}} + return nil +}