Merge pull request #17 from moul/dev/moul/add-user-service
Add user service
This commit is contained in:
commit
0336dc5217
@ -24,6 +24,12 @@ import (
|
||||
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"
|
||||
|
||||
user_svc "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/user"
|
||||
user_endpoints "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/user/gen/endpoints"
|
||||
user_pb "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/user/gen/pb"
|
||||
user_grpctransport "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/user/gen/transports/grpc"
|
||||
user_httptransport "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/user/gen/transports/http"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@ -53,6 +59,13 @@ func main() {
|
||||
sprint_pb.RegisterSprintServiceServer(s, srv)
|
||||
sprint_httptransport.RegisterHandlers(ctx, svc, mux, endpoints)
|
||||
}
|
||||
{
|
||||
svc := user_svc.New()
|
||||
endpoints := user_endpoints.MakeEndpoints(svc)
|
||||
srv := user_grpctransport.MakeGRPCServer(ctx, endpoints)
|
||||
user_pb.RegisterUserServiceServer(s, srv)
|
||||
user_httptransport.RegisterHandlers(ctx, svc, mux, endpoints)
|
||||
}
|
||||
|
||||
// start servers
|
||||
go func() {
|
||||
|
0
examples/go-kit/services/user/gen/README.md
Normal file
0
examples/go-kit/services/user/gen/README.md
Normal file
63
examples/go-kit/services/user/gen/endpoints/endpoints.go
Normal file
63
examples/go-kit/services/user/gen/endpoints/endpoints.go
Normal file
@ -0,0 +1,63 @@
|
||||
package user_endpoints
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/go-kit/kit/endpoint"
|
||||
pb "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/user/gen/pb"
|
||||
context "golang.org/x/net/context"
|
||||
)
|
||||
|
||||
var _ = fmt.Errorf
|
||||
|
||||
type Endpoints struct {
|
||||
CreateUserEndpoint endpoint.Endpoint
|
||||
|
||||
GetUserEndpoint endpoint.Endpoint
|
||||
}
|
||||
|
||||
func (e *Endpoints) CreateUser(ctx context.Context, in *pb.CreateUserRequest) (*pb.CreateUserResponse, error) {
|
||||
out, err := e.CreateUserEndpoint(ctx, in)
|
||||
if err != nil {
|
||||
return &pb.CreateUserResponse{ErrMsg: err.Error()}, err
|
||||
}
|
||||
return out.(*pb.CreateUserResponse), err
|
||||
}
|
||||
|
||||
func (e *Endpoints) GetUser(ctx context.Context, in *pb.GetUserRequest) (*pb.GetUserResponse, error) {
|
||||
out, err := e.GetUserEndpoint(ctx, in)
|
||||
if err != nil {
|
||||
return &pb.GetUserResponse{ErrMsg: err.Error()}, err
|
||||
}
|
||||
return out.(*pb.GetUserResponse), err
|
||||
}
|
||||
|
||||
func MakeCreateUserEndpoint(svc pb.UserServiceServer) endpoint.Endpoint {
|
||||
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
||||
req := request.(*pb.CreateUserRequest)
|
||||
rep, err := svc.CreateUser(ctx, req)
|
||||
if err != nil {
|
||||
return &pb.CreateUserResponse{ErrMsg: err.Error()}, err
|
||||
}
|
||||
return rep, nil
|
||||
}
|
||||
}
|
||||
|
||||
func MakeGetUserEndpoint(svc pb.UserServiceServer) endpoint.Endpoint {
|
||||
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
||||
req := request.(*pb.GetUserRequest)
|
||||
rep, err := svc.GetUser(ctx, req)
|
||||
if err != nil {
|
||||
return &pb.GetUserResponse{ErrMsg: err.Error()}, err
|
||||
}
|
||||
return rep, nil
|
||||
}
|
||||
}
|
||||
|
||||
func MakeEndpoints(svc pb.UserServiceServer) Endpoints {
|
||||
return Endpoints{
|
||||
|
||||
CreateUserEndpoint: MakeCreateUserEndpoint(svc),
|
||||
|
||||
GetUserEndpoint: MakeGetUserEndpoint(svc),
|
||||
}
|
||||
}
|
276
examples/go-kit/services/user/gen/pb/pb.go
Normal file
276
examples/go-kit/services/user/gen/pb/pb.go
Normal file
@ -0,0 +1,276 @@
|
||||
// Code generated by protoc-gen-gogo.
|
||||
// source: services/user/user.proto
|
||||
// DO NOT EDIT!
|
||||
|
||||
/*
|
||||
Package user is a generated protocol buffer package.
|
||||
|
||||
It is generated from these files:
|
||||
services/user/user.proto
|
||||
|
||||
It has these top-level messages:
|
||||
CreateUserRequest
|
||||
CreateUserResponse
|
||||
GetUserRequest
|
||||
GetUserResponse
|
||||
User
|
||||
*/
|
||||
package user
|
||||
|
||||
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 CreateUserRequest struct {
|
||||
Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"`
|
||||
}
|
||||
|
||||
func (m *CreateUserRequest) Reset() { *m = CreateUserRequest{} }
|
||||
func (m *CreateUserRequest) String() string { return proto.CompactTextString(m) }
|
||||
func (*CreateUserRequest) ProtoMessage() {}
|
||||
func (*CreateUserRequest) Descriptor() ([]byte, []int) { return fileDescriptorUser, []int{0} }
|
||||
|
||||
func (m *CreateUserRequest) GetName() string {
|
||||
if m != nil {
|
||||
return m.Name
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
type CreateUserResponse struct {
|
||||
User *User `protobuf:"bytes,1,opt,name=user" json:"user,omitempty"`
|
||||
ErrMsg string `protobuf:"bytes,2,opt,name=err_msg,json=errMsg,proto3" json:"err_msg,omitempty"`
|
||||
}
|
||||
|
||||
func (m *CreateUserResponse) Reset() { *m = CreateUserResponse{} }
|
||||
func (m *CreateUserResponse) String() string { return proto.CompactTextString(m) }
|
||||
func (*CreateUserResponse) ProtoMessage() {}
|
||||
func (*CreateUserResponse) Descriptor() ([]byte, []int) { return fileDescriptorUser, []int{1} }
|
||||
|
||||
func (m *CreateUserResponse) GetUser() *User {
|
||||
if m != nil {
|
||||
return m.User
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *CreateUserResponse) GetErrMsg() string {
|
||||
if m != nil {
|
||||
return m.ErrMsg
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
type GetUserRequest struct {
|
||||
Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"`
|
||||
}
|
||||
|
||||
func (m *GetUserRequest) Reset() { *m = GetUserRequest{} }
|
||||
func (m *GetUserRequest) String() string { return proto.CompactTextString(m) }
|
||||
func (*GetUserRequest) ProtoMessage() {}
|
||||
func (*GetUserRequest) Descriptor() ([]byte, []int) { return fileDescriptorUser, []int{2} }
|
||||
|
||||
func (m *GetUserRequest) GetId() string {
|
||||
if m != nil {
|
||||
return m.Id
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
type GetUserResponse struct {
|
||||
User *User `protobuf:"bytes,1,opt,name=user" json:"user,omitempty"`
|
||||
ErrMsg string `protobuf:"bytes,2,opt,name=err_msg,json=errMsg,proto3" json:"err_msg,omitempty"`
|
||||
}
|
||||
|
||||
func (m *GetUserResponse) Reset() { *m = GetUserResponse{} }
|
||||
func (m *GetUserResponse) String() string { return proto.CompactTextString(m) }
|
||||
func (*GetUserResponse) ProtoMessage() {}
|
||||
func (*GetUserResponse) Descriptor() ([]byte, []int) { return fileDescriptorUser, []int{3} }
|
||||
|
||||
func (m *GetUserResponse) GetUser() *User {
|
||||
if m != nil {
|
||||
return m.User
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *GetUserResponse) GetErrMsg() string {
|
||||
if m != nil {
|
||||
return m.ErrMsg
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
type User struct {
|
||||
Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"`
|
||||
Name string `protobuf:"bytes,2,opt,name=name,proto3" json:"name,omitempty"`
|
||||
}
|
||||
|
||||
func (m *User) Reset() { *m = User{} }
|
||||
func (m *User) String() string { return proto.CompactTextString(m) }
|
||||
func (*User) ProtoMessage() {}
|
||||
func (*User) Descriptor() ([]byte, []int) { return fileDescriptorUser, []int{4} }
|
||||
|
||||
func (m *User) GetId() string {
|
||||
if m != nil {
|
||||
return m.Id
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (m *User) GetName() string {
|
||||
if m != nil {
|
||||
return m.Name
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func init() {
|
||||
proto.RegisterType((*CreateUserRequest)(nil), "user.CreateUserRequest")
|
||||
proto.RegisterType((*CreateUserResponse)(nil), "user.CreateUserResponse")
|
||||
proto.RegisterType((*GetUserRequest)(nil), "user.GetUserRequest")
|
||||
proto.RegisterType((*GetUserResponse)(nil), "user.GetUserResponse")
|
||||
proto.RegisterType((*User)(nil), "user.User")
|
||||
}
|
||||
|
||||
// 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 UserService service
|
||||
|
||||
type UserServiceClient interface {
|
||||
CreateUser(ctx context.Context, in *CreateUserRequest, opts ...grpc.CallOption) (*CreateUserResponse, error)
|
||||
GetUser(ctx context.Context, in *GetUserRequest, opts ...grpc.CallOption) (*GetUserResponse, error)
|
||||
}
|
||||
|
||||
type userServiceClient struct {
|
||||
cc *grpc.ClientConn
|
||||
}
|
||||
|
||||
func NewUserServiceClient(cc *grpc.ClientConn) UserServiceClient {
|
||||
return &userServiceClient{cc}
|
||||
}
|
||||
|
||||
func (c *userServiceClient) CreateUser(ctx context.Context, in *CreateUserRequest, opts ...grpc.CallOption) (*CreateUserResponse, error) {
|
||||
out := new(CreateUserResponse)
|
||||
err := grpc.Invoke(ctx, "/user.UserService/CreateUser", in, out, c.cc, opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *userServiceClient) GetUser(ctx context.Context, in *GetUserRequest, opts ...grpc.CallOption) (*GetUserResponse, error) {
|
||||
out := new(GetUserResponse)
|
||||
err := grpc.Invoke(ctx, "/user.UserService/GetUser", in, out, c.cc, opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// Server API for UserService service
|
||||
|
||||
type UserServiceServer interface {
|
||||
CreateUser(context.Context, *CreateUserRequest) (*CreateUserResponse, error)
|
||||
GetUser(context.Context, *GetUserRequest) (*GetUserResponse, error)
|
||||
}
|
||||
|
||||
func RegisterUserServiceServer(s *grpc.Server, srv UserServiceServer) {
|
||||
s.RegisterService(&_UserService_serviceDesc, srv)
|
||||
}
|
||||
|
||||
func _UserService_CreateUser_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(CreateUserRequest)
|
||||
if err := dec(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if interceptor == nil {
|
||||
return srv.(UserServiceServer).CreateUser(ctx, in)
|
||||
}
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: "/user.UserService/CreateUser",
|
||||
}
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return srv.(UserServiceServer).CreateUser(ctx, req.(*CreateUserRequest))
|
||||
}
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
func _UserService_GetUser_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(GetUserRequest)
|
||||
if err := dec(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if interceptor == nil {
|
||||
return srv.(UserServiceServer).GetUser(ctx, in)
|
||||
}
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: "/user.UserService/GetUser",
|
||||
}
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return srv.(UserServiceServer).GetUser(ctx, req.(*GetUserRequest))
|
||||
}
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
var _UserService_serviceDesc = grpc.ServiceDesc{
|
||||
ServiceName: "user.UserService",
|
||||
HandlerType: (*UserServiceServer)(nil),
|
||||
Methods: []grpc.MethodDesc{
|
||||
{
|
||||
MethodName: "CreateUser",
|
||||
Handler: _UserService_CreateUser_Handler,
|
||||
},
|
||||
{
|
||||
MethodName: "GetUser",
|
||||
Handler: _UserService_GetUser_Handler,
|
||||
},
|
||||
},
|
||||
Streams: []grpc.StreamDesc{},
|
||||
Metadata: "services/user/user.proto",
|
||||
}
|
||||
|
||||
func init() { proto.RegisterFile("services/user/user.proto", fileDescriptorUser) }
|
||||
|
||||
var fileDescriptorUser = []byte{
|
||||
// 236 bytes of a gzipped FileDescriptorProto
|
||||
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0xe2, 0x92, 0x28, 0x4e, 0x2d, 0x2a,
|
||||
0xcb, 0x4c, 0x4e, 0x2d, 0xd6, 0x2f, 0x2d, 0x4e, 0x2d, 0x02, 0x13, 0x7a, 0x05, 0x45, 0xf9, 0x25,
|
||||
0xf9, 0x42, 0x2c, 0x20, 0xb6, 0x92, 0x3a, 0x97, 0xa0, 0x73, 0x51, 0x6a, 0x62, 0x49, 0x6a, 0x68,
|
||||
0x71, 0x6a, 0x51, 0x50, 0x6a, 0x61, 0x69, 0x6a, 0x71, 0x89, 0x90, 0x10, 0x17, 0x4b, 0x5e, 0x62,
|
||||
0x6e, 0xaa, 0x04, 0xa3, 0x02, 0xa3, 0x06, 0x67, 0x10, 0x98, 0xad, 0xe4, 0xcb, 0x25, 0x84, 0xac,
|
||||
0xb0, 0xb8, 0x20, 0x3f, 0xaf, 0x38, 0x55, 0x48, 0x8e, 0x0b, 0x6c, 0x0c, 0x58, 0x25, 0xb7, 0x11,
|
||||
0x97, 0x1e, 0xd8, 0x7c, 0xb0, 0x0a, 0xb0, 0xb8, 0x90, 0x38, 0x17, 0x7b, 0x6a, 0x51, 0x51, 0x7c,
|
||||
0x6e, 0x71, 0xba, 0x04, 0x13, 0xd8, 0x30, 0xb6, 0xd4, 0xa2, 0x22, 0xdf, 0xe2, 0x74, 0x25, 0x05,
|
||||
0x2e, 0x3e, 0xf7, 0xd4, 0x12, 0x64, 0x4b, 0xf9, 0xb8, 0x98, 0x32, 0x53, 0xa0, 0x56, 0x32, 0x65,
|
||||
0xa6, 0x28, 0x79, 0x71, 0xf1, 0xc3, 0x55, 0x50, 0x6a, 0x9b, 0x16, 0x17, 0x0b, 0x48, 0x19, 0xba,
|
||||
0x1d, 0x70, 0x8f, 0x32, 0x21, 0x3c, 0x6a, 0xd4, 0xc5, 0xc8, 0xc5, 0x0d, 0x52, 0x1c, 0x0c, 0x09,
|
||||
0x38, 0x21, 0x47, 0x2e, 0x2e, 0x84, 0xc7, 0x85, 0xc4, 0x21, 0x96, 0x62, 0x84, 0x99, 0x94, 0x04,
|
||||
0xa6, 0x04, 0xc4, 0xd5, 0x4a, 0x0c, 0x42, 0x16, 0x5c, 0xec, 0x50, 0xaf, 0x08, 0x89, 0x40, 0x94,
|
||||
0xa1, 0xfa, 0x5d, 0x4a, 0x14, 0x4d, 0x14, 0xa6, 0x33, 0x89, 0x0d, 0x1c, 0x57, 0xc6, 0x80, 0x00,
|
||||
0x00, 0x00, 0xff, 0xff, 0xce, 0xde, 0xa3, 0x2e, 0xc7, 0x01, 0x00, 0x00,
|
||||
}
|
75
examples/go-kit/services/user/gen/transports/grpc/grpc.go
Normal file
75
examples/go-kit/services/user/gen/transports/grpc/grpc.go
Normal file
@ -0,0 +1,75 @@
|
||||
package user_grpctransport
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
grpctransport "github.com/go-kit/kit/transport/grpc"
|
||||
endpoint "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/user/gen/endpoints"
|
||||
pb "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/user/gen/pb"
|
||||
context "golang.org/x/net/context"
|
||||
)
|
||||
|
||||
// avoid import errors
|
||||
var _ = fmt.Errorf
|
||||
|
||||
func MakeGRPCServer(ctx context.Context, endpoints endpoint.Endpoints) pb.UserServiceServer {
|
||||
options := []grpctransport.ServerOption{}
|
||||
return &grpcServer{
|
||||
|
||||
createuser: grpctransport.NewServer(
|
||||
ctx,
|
||||
endpoints.CreateUserEndpoint,
|
||||
decodeCreateUserRequest,
|
||||
encodeCreateUserResponse,
|
||||
options...,
|
||||
),
|
||||
|
||||
getuser: grpctransport.NewServer(
|
||||
ctx,
|
||||
endpoints.GetUserEndpoint,
|
||||
decodeGetUserRequest,
|
||||
encodeGetUserResponse,
|
||||
options...,
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
type grpcServer struct {
|
||||
createuser grpctransport.Handler
|
||||
|
||||
getuser grpctransport.Handler
|
||||
}
|
||||
|
||||
func (s *grpcServer) CreateUser(ctx context.Context, req *pb.CreateUserRequest) (*pb.CreateUserResponse, error) {
|
||||
_, rep, err := s.createuser.ServeGRPC(ctx, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return rep.(*pb.CreateUserResponse), nil
|
||||
}
|
||||
|
||||
func decodeCreateUserRequest(ctx context.Context, grpcReq interface{}) (interface{}, error) {
|
||||
return grpcReq, nil
|
||||
}
|
||||
|
||||
func encodeCreateUserResponse(ctx context.Context, response interface{}) (interface{}, error) {
|
||||
resp := response.(*pb.CreateUserResponse)
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (s *grpcServer) GetUser(ctx context.Context, req *pb.GetUserRequest) (*pb.GetUserResponse, error) {
|
||||
_, rep, err := s.getuser.ServeGRPC(ctx, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return rep.(*pb.GetUserResponse), nil
|
||||
}
|
||||
|
||||
func decodeGetUserRequest(ctx context.Context, grpcReq interface{}) (interface{}, error) {
|
||||
return grpcReq, nil
|
||||
}
|
||||
|
||||
func encodeGetUserResponse(ctx context.Context, response interface{}) (interface{}, error) {
|
||||
resp := response.(*pb.GetUserResponse)
|
||||
return resp, nil
|
||||
}
|
68
examples/go-kit/services/user/gen/transports/http/http.go
Normal file
68
examples/go-kit/services/user/gen/transports/http/http.go
Normal file
@ -0,0 +1,68 @@
|
||||
package user_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/user/gen/endpoints"
|
||||
pb "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/user/gen/pb"
|
||||
)
|
||||
|
||||
func MakeCreateUserHandler(ctx context.Context, svc pb.UserServiceServer, endpoint gokit_endpoint.Endpoint) *httptransport.Server {
|
||||
return httptransport.NewServer(
|
||||
ctx,
|
||||
endpoint,
|
||||
decodeCreateUserRequest,
|
||||
encodeCreateUserResponse,
|
||||
[]httptransport.ServerOption{}...,
|
||||
)
|
||||
}
|
||||
|
||||
func decodeCreateUserRequest(ctx context.Context, r *http.Request) (interface{}, error) {
|
||||
var req pb.CreateUserRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &req, nil
|
||||
}
|
||||
|
||||
func encodeCreateUserResponse(ctx context.Context, w http.ResponseWriter, response interface{}) error {
|
||||
return json.NewEncoder(w).Encode(response)
|
||||
}
|
||||
|
||||
func MakeGetUserHandler(ctx context.Context, svc pb.UserServiceServer, endpoint gokit_endpoint.Endpoint) *httptransport.Server {
|
||||
return httptransport.NewServer(
|
||||
ctx,
|
||||
endpoint,
|
||||
decodeGetUserRequest,
|
||||
encodeGetUserResponse,
|
||||
[]httptransport.ServerOption{}...,
|
||||
)
|
||||
}
|
||||
|
||||
func decodeGetUserRequest(ctx context.Context, r *http.Request) (interface{}, error) {
|
||||
var req pb.GetUserRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &req, nil
|
||||
}
|
||||
|
||||
func encodeGetUserResponse(ctx context.Context, w http.ResponseWriter, response interface{}) error {
|
||||
return json.NewEncoder(w).Encode(response)
|
||||
}
|
||||
|
||||
func RegisterHandlers(ctx context.Context, 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))
|
||||
|
||||
log.Println("new HTTP endpoint: \"/GetUser\" (service=User)")
|
||||
mux.Handle("/GetUser", MakeGetUserHandler(ctx, svc, endpoints.GetUserEndpoint))
|
||||
|
||||
return nil
|
||||
}
|
21
examples/go-kit/services/user/service.go
Normal file
21
examples/go-kit/services/user/service.go
Normal file
@ -0,0 +1,21 @@
|
||||
package usersvc
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"golang.org/x/net/context"
|
||||
|
||||
pb "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/user/gen/pb"
|
||||
)
|
||||
|
||||
type Service struct{}
|
||||
|
||||
func New() pb.UserServiceServer { return &Service{} }
|
||||
|
||||
func (svc *Service) CreateUser(ctx context.Context, in *pb.CreateUserRequest) (*pb.CreateUserResponse, error) {
|
||||
return nil, fmt.Errorf("not implemented")
|
||||
}
|
||||
|
||||
func (svc *Service) GetUser(ctx context.Context, in *pb.GetUserRequest) (*pb.GetUserResponse, error) {
|
||||
return nil, fmt.Errorf("not implemented")
|
||||
}
|
29
examples/go-kit/services/user/user.proto
Normal file
29
examples/go-kit/services/user/user.proto
Normal file
@ -0,0 +1,29 @@
|
||||
syntax = "proto3";
|
||||
|
||||
package user;
|
||||
|
||||
service UserService {
|
||||
rpc CreateUser(CreateUserRequest) returns (CreateUserResponse) {}
|
||||
rpc GetUser(GetUserRequest) returns (GetUserResponse) {}
|
||||
}
|
||||
|
||||
message CreateUserRequest {
|
||||
string name = 1;
|
||||
}
|
||||
message CreateUserResponse {
|
||||
User user = 1;
|
||||
string err_msg = 2;
|
||||
}
|
||||
|
||||
message GetUserRequest {
|
||||
string id = 1;
|
||||
}
|
||||
message GetUserResponse {
|
||||
User user = 1;
|
||||
string err_msg = 2;
|
||||
}
|
||||
|
||||
message User {
|
||||
string id = 1;
|
||||
string name = 2;
|
||||
}
|
Loading…
Reference in New Issue
Block a user