forked from metaverse/truss
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from polatouche6/tdl_dev
modify endpoint.gotemplate to implementing service discovery and load balancing on the client side
- Loading branch information
Showing
9 changed files
with
744 additions
and
196 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// Code generated by truss. DO NOT EDIT. | ||
// Rerunning truss will overwrite this file. | ||
// Version: {{.Version}} | ||
// Version Date: {{.VersionDate}} | ||
|
||
package svc | ||
|
||
// This file provides server-side bindings for the gRPC transport. | ||
// It utilizes the transport/grpc.Server. | ||
|
||
import ( | ||
"net/http" | ||
"context" | ||
|
||
"google.golang.org/grpc/metadata" | ||
|
||
grpctransport "github.com/go-kit/kit/transport/grpc" | ||
|
||
// This Service | ||
pb "{{.PBImportPath -}}" | ||
) | ||
|
||
// MakeGRPCServer makes a set of endpoints available as a gRPC {{.Service.Name}}Server. | ||
func MakeGRPCServer(endpoints Endpoints, options ...grpctransport.ServerOption) pb.{{.Service.Name}}Server { | ||
serverOptions := []grpctransport.ServerOption{ | ||
grpctransport.ServerBefore(metadataToContext), | ||
} | ||
serverOptions = append(serverOptions, options...) | ||
return &grpcServer{ | ||
// {{ ToLower .Service.Name }} | ||
{{range $i := .Service.Methods}} | ||
{{ToLower $i.Name}}: grpctransport.NewServer( | ||
endpoints.{{$i.Name}}Endpoint, | ||
DecodeGRPC{{$i.Name}}Request, | ||
EncodeGRPC{{$i.Name}}Response, | ||
serverOptions..., | ||
), | ||
{{- end}} | ||
} | ||
} | ||
|
||
// grpcServer implements the {{GoName .Service.Name}}Server interface | ||
type grpcServer struct { | ||
{{range $i := .Service.Methods}} | ||
{{ToLower $i.Name}} grpctransport.Handler | ||
{{- end}} | ||
} | ||
|
||
// Methods for grpcServer to implement {{GoName .Service.Name}}Server interface | ||
{{range $i := .Service.Methods}} | ||
func (s *grpcServer) {{GoName $i.Name}}(ctx context.Context, req *pb.{{GoName $i.RequestType.Name}}) (*pb.{{GoName $i.ResponseType.Name}}, error) { | ||
_, rep, err := s.{{ToLower $i.Name}}.ServeGRPC(ctx, req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return rep.(*pb.{{GoName $i.ResponseType.Name}}), nil | ||
} | ||
{{end}} | ||
|
||
// Server Decode | ||
{{range $i := .Service.Methods}} | ||
// DecodeGRPC{{$i.Name}}Request is a transport/grpc.DecodeRequestFunc that converts a | ||
// gRPC {{ToLower $i.Name}} request to a user-domain {{ToLower $i.Name}} request. Primarily useful in a server. | ||
func DecodeGRPC{{$i.Name}}Request(_ context.Context, grpcReq interface{}) (interface{}, error) { | ||
req := grpcReq.(*pb.{{GoName $i.RequestType.Name}}) | ||
return req, nil | ||
} | ||
{{end}} | ||
|
||
// Server Encode | ||
{{range $i := .Service.Methods}} | ||
// EncodeGRPC{{$i.Name}}Response is a transport/grpc.EncodeResponseFunc that converts a | ||
// user-domain {{ToLower $i.Name}} response to a gRPC {{ToLower $i.Name}} reply. Primarily useful in a server. | ||
func EncodeGRPC{{$i.Name}}Response(_ context.Context, response interface{}) (interface{}, error) { | ||
resp := response.(*pb.{{GoName $i.ResponseType.Name}}) | ||
return resp, nil | ||
} | ||
{{end}} | ||
|
||
// Helpers | ||
|
||
func metadataToContext(ctx context.Context, md metadata.MD) context.Context { | ||
for k, v := range md { | ||
if v != nil { | ||
// The key is added both in metadata format (k) which is all lower | ||
// and the http.CanonicalHeaderKey of the key so that it can be | ||
// accessed in either format | ||
ctx = context.WithValue(ctx, k, v[0]) | ||
ctx = context.WithValue(ctx, http.CanonicalHeaderKey(k), v[0]) | ||
} | ||
} | ||
|
||
return ctx | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.