|
1 | 1 | //go:build grpc |
2 | 2 |
|
3 | | -// This file is the real gRPC server wiring. It is excluded from normal builds |
4 | | -// via the "grpc" build tag because eyrie does not yet depend on |
5 | | -// google.golang.org/grpc. It is intentionally a documented stub: it compiles |
6 | | -// under `go build -tags grpc ./...` without pulling in grpc, and marks exactly |
7 | | -// where the generated code and server registration belong. |
8 | | -// |
9 | | -// To activate (see README.md for full steps): |
10 | | -// 1. go get google.golang.org/grpc google.golang.org/protobuf |
11 | | -// 2. generate eyriev1 stubs from proto/eyrie/v1/chat.proto |
12 | | -// 3. replace the body of Serve below with a real grpc.Server that registers |
13 | | -// a ChatServiceServer adapting conversation.Engine, and drop this note. |
14 | | - |
15 | 3 | package grpc |
16 | 4 |
|
17 | 5 | import ( |
18 | | - "errors" |
| 6 | + "context" |
| 7 | + "encoding/json" |
| 8 | + "fmt" |
19 | 9 | "net" |
| 10 | + |
| 11 | + googlegrpc "google.golang.org/grpc" |
| 12 | + "google.golang.org/grpc/encoding" |
20 | 13 | ) |
21 | 14 |
|
22 | | -// errGRPCNotWired signals that the gRPC dependency/codegen has not been added. |
23 | | -var errGRPCNotWired = errors.New("eyrie/grpc: gRPC server not wired up (run codegen; see internal/grpc/README.md)") |
| 15 | +// JSONCodec is the wire codec used by Eyrie's dependency-light gRPC surface. |
| 16 | +// Clients select it with grpc.CallContentSubtype("json"). |
| 17 | +type JSONCodec struct{} |
| 18 | + |
| 19 | +func (JSONCodec) Name() string { return "json" } |
| 20 | +func (JSONCodec) Marshal(v interface{}) ([]byte, error) { |
| 21 | + return json.Marshal(v) |
| 22 | +} |
| 23 | +func (JSONCodec) Unmarshal(data []byte, v interface{}) error { |
| 24 | + return json.Unmarshal(data, v) |
| 25 | +} |
| 26 | + |
| 27 | +func init() { |
| 28 | + encoding.RegisterCodec(JSONCodec{}) |
| 29 | +} |
| 30 | + |
| 31 | +// NewServer creates a gRPC server and registers the supplied ChatService. |
| 32 | +func NewServer(svc ChatService, opts ...googlegrpc.ServerOption) (*googlegrpc.Server, error) { |
| 33 | + if svc == nil { |
| 34 | + return nil, fmt.Errorf("eyrie/grpc: chat service is required") |
| 35 | + } |
| 36 | + server := googlegrpc.NewServer(opts...) |
| 37 | + server.RegisterService(&chatServiceDesc, svc) |
| 38 | + return server, nil |
| 39 | +} |
| 40 | + |
| 41 | +// Serve registers svc and serves requests until the listener is closed or the |
| 42 | +// server is stopped. |
| 43 | +func Serve(lis net.Listener, svc ChatService, opts ...googlegrpc.ServerOption) error { |
| 44 | + if lis == nil { |
| 45 | + return fmt.Errorf("eyrie/grpc: listener is required") |
| 46 | + } |
| 47 | + server, err := NewServer(svc, opts...) |
| 48 | + if err != nil { |
| 49 | + return err |
| 50 | + } |
| 51 | + return server.Serve(lis) |
| 52 | +} |
| 53 | + |
| 54 | +func chatHandler(srv interface{}, ctx context.Context, decode func(interface{}) error, interceptor googlegrpc.UnaryServerInterceptor) (interface{}, error) { |
| 55 | + req := new(ChatRequest) |
| 56 | + if err := decode(req); err != nil { |
| 57 | + return nil, err |
| 58 | + } |
| 59 | + if interceptor == nil { |
| 60 | + return srv.(ChatService).Chat(ctx, req) |
| 61 | + } |
| 62 | + info := &googlegrpc.UnaryServerInfo{Server: srv, FullMethod: "/eyrie.v1.ChatService/Chat"} |
| 63 | + handler := func(ctx context.Context, request interface{}) (interface{}, error) { |
| 64 | + return srv.(ChatService).Chat(ctx, request.(*ChatRequest)) |
| 65 | + } |
| 66 | + return interceptor(ctx, req, info, handler) |
| 67 | +} |
24 | 68 |
|
25 | | -// Serve will listen on lis and serve the ChatService over gRPC once the |
26 | | -// dependency and generated stubs are in place. Today it is a stub. |
27 | | -// |
28 | | -// TODO(grpc): construct grpc.NewServer(), register the generated |
29 | | -// ChatServiceServer backed by svc, and call grpcServer.Serve(lis). |
30 | | -func Serve(_ net.Listener, _ ChatService) error { |
31 | | - return errGRPCNotWired |
| 69 | +var chatServiceDesc = googlegrpc.ServiceDesc{ |
| 70 | + ServiceName: "eyrie.v1.ChatService", |
| 71 | + HandlerType: (*ChatService)(nil), |
| 72 | + Methods: []googlegrpc.MethodDesc{{ |
| 73 | + MethodName: "Chat", |
| 74 | + Handler: chatHandler, |
| 75 | + }}, |
| 76 | + Metadata: "eyrie/v1/chat.json", |
32 | 77 | } |
0 commit comments