-
Notifications
You must be signed in to change notification settings - Fork 0
/
grpc.go
66 lines (55 loc) · 1.78 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/migrieren/api/migrieren/v1"
"github.com/alexfalkowski/migrieren/migrate"
"github.com/alexfalkowski/migrieren/migrate/migrator"
"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
Metrics *prometheus.ClientMetrics
MigrateConfig *migrate.Config
Migrator migrator.Migrator
}
// Register server.
func Register(params RegisterParams) error {
ctx := context.Background()
server := NewServer(ServerParams{Config: params.MigrateConfig, Migrator: params.Migrator})
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
}