-
Notifications
You must be signed in to change notification settings - Fork 37
Build and run an example SummatorService using clay Server
Nick K edited this page Jun 9, 2018
·
4 revisions
See example server.
This example uses sample SummatorService API w/ YAML-annotated HTTP endpoints and bundled server utrack/clay.Server
.
First, generate your Go code from protofile using protoc:
cd doc/example/pb
protoc -I/usr/local/include -I. \
-I$GOPATH/src \
-I$GOPATH/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis \
--go_out=plugins=grpc:. --goclay_out=grpc_api_configuration=sum.yaml:. ./sum.proto
- This command generates HTTP endpoints based on YAML description. See Describing HTTP endpoints for info on YAML and inline HTTP endpoint definitons.
Then finish your gRPC service implementation as usual:
// SumImpl is an implementation of SummatorService.
type SumImpl struct{}
// Sum implements SummatorServer.Sum.
func (s *SumImpl) Sum(ctx context.Context, r *pb.SumRequest) (*pb.SumResponse, error) {
if r.GetA() == 0 {
return nil, errors.New("a is zero")
}
if r.GetB() == 65536 {
panic(errors.New("we've got a problem!"))
}
sum := r.GetA() + r.GetB()
return &pb.SumResponse{
Sum: sum,
}, nil
}
Add helper method to the implementation, so it would implement the "github.com/utrack/clay/transport".Service
:
// GetDescription is a simple alias to the ServiceDesc constructor.
// It makes it possible to register the service implementation @ the server.
func (s *SumImpl) GetDescription() transport.ServiceDesc {
return pb.NewSummatorServiceDesc(s)
}
Now, you can run the server.
HTTP and gRPC traffic will be served at port 12345
with Swagger definition @ /swagger.json
.