-
Notifications
You must be signed in to change notification settings - Fork 1
/
grpc.go
66 lines (55 loc) · 1.8 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
60
61
62
63
64
65
66
package grpc
import (
"context"
"fmt"
"github.com/alexfalkowski/go-service/transport"
"github.com/alexfalkowski/go-service/transport/grpc"
"github.com/alexfalkowski/go-service/transport/grpc/metrics/prometheus"
"github.com/alexfalkowski/go-service/transport/grpc/trace/opentracing"
"github.com/alexfalkowski/go-service/transport/http"
v1 "github.com/alexfalkowski/konfig/api/konfig/v1"
"github.com/alexfalkowski/konfig/server/config"
source "github.com/alexfalkowski/konfig/source/configurator"
"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
TransportConfig *transport.Config
Logger *zap.Logger
Tracer opentracing.Tracer
Configurator source.Configurator
Transformer *config.Transformer
Metrics *prometheus.ClientMetrics
}
// Register server.
func Register(params RegisterParams) error {
ctx := context.Background()
server := NewServer(ServerParams{Configurator: params.Configurator, Transformer: params.Transformer})
v1.RegisterServiceServer(params.GRPCServer.Server, server)
conn, err := grpc.NewClient(
grpc.ClientParams{Context: ctx, Host: fmt.Sprintf("127.0.0.1:%s", params.TransportConfig.Port), Config: params.GRPCConfig},
grpc.WithClientLogger(params.Logger), grpc.WithClientTracer(params.Tracer), grpc.WithClientMetrics(params.Metrics),
)
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
}