Generate endpoints + transports
This commit is contained in:
parent
bffc245a82
commit
f62940ac4c
@ -1 +0,0 @@
|
||||
package session_client
|
@ -1 +0,0 @@
|
||||
package main
|
@ -1 +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"
|
||||
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.LoginReply), 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.LogoutReply), 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.LoginReply{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.LogoutReply{ErrMsg: err.Error()}, err
|
||||
}
|
||||
return rep, nil
|
||||
}
|
||||
}
|
||||
|
||||
func MakeEndpoints(svc pb.SessionServiceServer) Endpoints {
|
||||
return Endpoints{
|
||||
|
||||
LoginEndpoint: MakeLoginEndpoint(svc),
|
||||
|
||||
LogoutEndpoint: MakeLogoutEndpoint(svc),
|
||||
}
|
||||
}
|
||||
|
@ -1 +0,0 @@
|
||||
package session_init
|
@ -1 +1,75 @@
|
||||
package session_transportgrpc
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
grpctransport "github.com/go-kit/kit/transport/grpc"
|
||||
pb "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/session"
|
||||
endpoint "github.com/moul/protoc-gen-gotemplate/examples/go-kit/session/gen/endpoints"
|
||||
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.LoginReply, error) {
|
||||
_, rep, err := s.login.ServeGRPC(ctx, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return rep.(*pb.LoginReply), 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.LoginReply)
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (s *grpcServer) Logout(ctx context.Context, req *pb.LogoutRequest) (*pb.LogoutReply, error) {
|
||||
_, rep, err := s.logout.ServeGRPC(ctx, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return rep.(*pb.LogoutReply), 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.LogoutReply)
|
||||
return resp, nil
|
||||
}
|
||||
|
@ -1 +1,68 @@
|
||||
package session_transporthttp
|
||||
|
||||
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"
|
||||
pb "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/session"
|
||||
endpoints "github.com/moul/protoc-gen-gotemplate/examples/go-kit/session/gen/endpoints"
|
||||
)
|
||||
|
||||
func MakeLoginHandler(ctx context.Context, svc pb.SessionServiceServer, endpoint gokit_endpoint.Endpoint) *httptransport.Server {
|
||||
return httptransport.NewServer(
|
||||
ctx,
|
||||
endpoint,
|
||||
decodeLoginRequest,
|
||||
encodeLoginResponse,
|
||||
append([]httptransport.ServerOption{}, httptransport.ServerBefore(jwt.ToHTTPContext()))...,
|
||||
)
|
||||
}
|
||||
|
||||
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,
|
||||
append([]httptransport.ServerOption{}, httptransport.ServerBefore(jwt.ToHTTPContext()))...,
|
||||
)
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
@ -1 +0,0 @@
|
||||
package sprint_client
|
@ -1 +0,0 @@
|
||||
package main
|
@ -1 +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"
|
||||
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.AddSprintReply), 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.CloseSprintReply), 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.GetSprintReply), 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.AddSprintReply{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.CloseSprintReply{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.GetSprintReply{ErrMsg: err.Error()}, err
|
||||
}
|
||||
return rep, nil
|
||||
}
|
||||
}
|
||||
|
||||
func MakeEndpoints(svc pb.SprintServiceServer) Endpoints {
|
||||
return Endpoints{
|
||||
|
||||
AddSprintEndpoint: MakeAddSprintEndpoint(svc),
|
||||
|
||||
CloseSprintEndpoint: MakeCloseSprintEndpoint(svc),
|
||||
|
||||
GetSprintEndpoint: MakeGetSprintEndpoint(svc),
|
||||
}
|
||||
}
|
||||
|
@ -1 +0,0 @@
|
||||
package sprint_init
|
@ -1 +1,102 @@
|
||||
package sprint_transportgrpc
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
grpctransport "github.com/go-kit/kit/transport/grpc"
|
||||
pb "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/sprint"
|
||||
endpoint "github.com/moul/protoc-gen-gotemplate/examples/go-kit/sprint/gen/endpoints"
|
||||
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.AddSprintReply, error) {
|
||||
_, rep, err := s.addsprint.ServeGRPC(ctx, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return rep.(*pb.AddSprintReply), 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.AddSprintReply)
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (s *grpcServer) CloseSprint(ctx context.Context, req *pb.CloseSprintRequest) (*pb.CloseSprintReply, error) {
|
||||
_, rep, err := s.closesprint.ServeGRPC(ctx, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return rep.(*pb.CloseSprintReply), 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.CloseSprintReply)
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (s *grpcServer) GetSprint(ctx context.Context, req *pb.GetSprintRequest) (*pb.GetSprintReply, error) {
|
||||
_, rep, err := s.getsprint.ServeGRPC(ctx, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return rep.(*pb.GetSprintReply), 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.GetSprintReply)
|
||||
return resp, nil
|
||||
}
|
||||
|
@ -1 +1,93 @@
|
||||
package sprint_transporthttp
|
||||
|
||||
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"
|
||||
pb "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/sprint"
|
||||
endpoints "github.com/moul/protoc-gen-gotemplate/examples/go-kit/sprint/gen/endpoints"
|
||||
)
|
||||
|
||||
func MakeAddSprintHandler(ctx context.Context, svc pb.SprintServiceServer, endpoint gokit_endpoint.Endpoint) *httptransport.Server {
|
||||
return httptransport.NewServer(
|
||||
ctx,
|
||||
endpoint,
|
||||
decodeAddSprintRequest,
|
||||
encodeAddSprintResponse,
|
||||
append([]httptransport.ServerOption{}, httptransport.ServerBefore(jwt.ToHTTPContext()))...,
|
||||
)
|
||||
}
|
||||
|
||||
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,
|
||||
append([]httptransport.ServerOption{}, httptransport.ServerBefore(jwt.ToHTTPContext()))...,
|
||||
)
|
||||
}
|
||||
|
||||
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,
|
||||
append([]httptransport.ServerOption{}, httptransport.ServerBefore(jwt.ToHTTPContext()))...,
|
||||
)
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
@ -1 +0,0 @@
|
||||
package {{.File.Package}}_client
|
@ -1 +0,0 @@
|
||||
package main
|
@ -1 +1,49 @@
|
||||
package {{.File.Package}}_endpoints
|
||||
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}}"
|
||||
"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}}Reply), 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}}Reply{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}}
|
||||
}
|
||||
}
|
||||
|
@ -1 +0,0 @@
|
||||
package {{.File.Package}}_init
|
@ -1 +1,59 @@
|
||||
package {{.File.Package}}_transportgrpc
|
||||
package {{.File.Package}}_transportgrpc
|
||||
|
||||
{{$file := .File}}
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
context "golang.org/x/net/context"
|
||||
pb "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/{{.File.Package}}"
|
||||
grpctransport "github.com/go-kit/kit/transport/grpc"
|
||||
endpoint "github.com/moul/protoc-gen-gotemplate/examples/go-kit/{{.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}}Reply, error) {
|
||||
_, rep, err := s.{{.Name | lower}}.ServeGRPC(ctx, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return rep.(*pb.{{.Name}}Reply), 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}}Reply)
|
||||
return resp, nil
|
||||
}
|
||||
{{end}}
|
||||
|
@ -1 +1,47 @@
|
||||
package {{.File.Package}}_transporthttp
|
||||
package {{.File.Package}}_transporthttp
|
||||
|
||||
{{$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}}"
|
||||
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/{{.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,
|
||||
append([]httptransport.ServerOption{}, httptransport.ServerBefore(jwt.ToHTTPContext()))...,
|
||||
)
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user