/
grpc.go
55 lines (45 loc) · 1.38 KB
/
grpc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package grpc
import (
"context"
"fmt"
sgrpc "github.com/alexfalkowski/go-service/transport/grpc"
shttp "github.com/alexfalkowski/go-service/transport/http"
v2 "github.com/alexfalkowski/standort/api/standort/v2"
"github.com/alexfalkowski/standort/location"
"github.com/opentracing/opentracing-go"
"go.uber.org/fx"
"go.uber.org/zap"
"google.golang.org/grpc"
)
// RegisterParams for gRPC.
type RegisterParams struct {
fx.In
GRPCServer *grpc.Server
HTTPServer *shttp.Server
GRPCConfig *sgrpc.Config
Logger *zap.Logger
Tracer opentracing.Tracer
Location *location.Location
}
// Register server.
func Register(lc fx.Lifecycle, params RegisterParams) {
server := NewServer(params.Location)
v2.RegisterServiceServer(params.GRPCServer, server)
var conn *grpc.ClientConn
lc.Append(fx.Hook{
OnStart: func(ctx context.Context) error {
conn, _ = sgrpc.NewClient(ctx, fmt.Sprintf("127.0.0.1:%s", params.GRPCConfig.Port),
sgrpc.WithClientConfig(params.GRPCConfig), sgrpc.WithClientLogger(params.Logger),
sgrpc.WithClientTracer(params.Tracer), sgrpc.WithClientDialOption(grpc.WithBlock()),
)
return v2.RegisterServiceHandler(ctx, params.HTTPServer.Mux, conn)
},
OnStop: func(ctx context.Context) error {
return conn.Close()
},
})
}
// NewServer for gRPC.
func NewServer(location *location.Location) v2.ServiceServer {
return &Server{location: location}
}