-
Notifications
You must be signed in to change notification settings - Fork 1
/
grpc.go
59 lines (48 loc) · 1.33 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
56
57
58
59
package grpc
import (
"context"
"fmt"
"github.com/alexfalkowski/go-service/transport/grpc"
"github.com/alexfalkowski/go-service/transport/grpc/telemetry/tracer"
"github.com/alexfalkowski/go-service/transport/http"
v1 "github.com/alexfalkowski/konfig/api/konfig/v1"
"go.opentelemetry.io/otel/metric"
"go.uber.org/fx"
"go.uber.org/zap"
)
// RegisterParams for gRPC.
type RegisterParams struct {
fx.In
Lifecycle fx.Lifecycle
GRPCServer *grpc.Server
HTTPServer *http.Server
GRPCConfig *grpc.Config
Logger *zap.Logger
Tracer tracer.Tracer
Meter metric.Meter
Server v1.ServiceServer
}
// Register server.
func Register(params RegisterParams) error {
ctx := context.Background()
v1.RegisterServiceServer(params.GRPCServer.Server, params.Server)
conn, err := grpc.NewClient(ctx, fmt.Sprintf("127.0.0.1:%s", params.GRPCConfig.Port), params.GRPCConfig,
grpc.WithClientLogger(params.Logger), grpc.WithClientTracer(params.Tracer), grpc.WithClientMetrics(params.Meter),
)
if err != nil {
return err
}
if err := v1.RegisterServiceHandler(ctx, params.HTTPServer.Mux, conn); err != nil {
return err
}
params.Lifecycle.Append(fx.Hook{
OnStart: func(ctx context.Context) error {
conn.ResetConnectBackoff()
return nil
},
OnStop: func(ctx context.Context) error {
return conn.Close()
},
})
return nil
}