/
init.go
69 lines (58 loc) · 2.33 KB
/
init.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
67
68
69
package grpcHandler
import (
"fmt"
"github.com/alhamsya/boilerplate-go/domain/constants"
"github.com/alhamsya/boilerplate-go/infrastructure/middleware"
"github.com/alhamsya/boilerplate-go/lib/helpers/grace"
"github.com/alhamsya/boilerplate-go/service/inter/grpc/routers"
"google.golang.org/grpc"
"google.golang.org/grpc/health/grpc_health_v1"
"google.golang.org/grpc/keepalive"
"google.golang.org/grpc/reflection"
grpcMiddleware "github.com/grpc-ecosystem/go-grpc-middleware"
grpcRecovery "github.com/grpc-ecosystem/go-grpc-middleware/recovery"
grpcOpenTracing "github.com/grpc-ecosystem/go-grpc-middleware/tracing/opentracing"
grpcValidator "github.com/grpc-ecosystem/go-grpc-middleware/validator"
grpcPrometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
)
func New(this *Handler) *Handler {
srv := grpc.NewServer(
// MaxConnectionIdle is a duration for the amount of time after which an
// idle connection would be closed by sending a GoAway. Idleness duration is
// defined since the most recent time the number of outstanding RPCs became
// zero or the connection establishment
grpc.KeepaliveParams(keepalive.ServerParameters{
MaxConnectionIdle: constCommon.DefaultGRCPMaxConnectionIdle,
}),
grpc.UnaryInterceptor(
grpcMiddleware.ChainUnaryServer(
grpcOpenTracing.UnaryServerInterceptor(),
grpcValidator.UnaryServerInterceptor(),
grpcPrometheus.UnaryServerInterceptor,
grpcRecovery.UnaryServerInterceptor(),
middleware.GrpcLoggingInterceptor,
),
),
)
//enabling histogram to prometheus
grpcPrometheus.EnableHandlingTimeHistogram()
//healthy check gRPC grpc-health-probe
grpc_health_v1.RegisterHealthServer(srv, NewHealthChecker())
//gRPC Server Reflection provides information about publicly-accessible gRPC service on a server,
//and assists clients at runtime to construct RPC requests and responses without precompiled service information.
reflection.Register(srv)
routeHandler := &grpcRouters.GrpcServer{
Cfg: this.Cfg,
GrpcInteractor: this.Interactor,
App: srv,
}
grpcRouters.New(routeHandler).Register()
return &Handler{
Cfg: this.Cfg,
Interactor: this.Interactor,
GrpcServer: srv,
}
}
func (h *Handler) Run() error {
return grace.ServeGRPC(h.GrpcServer,fmt.Sprintf(":%s", h.Cfg.GrpcServer.Port), constCommon.DefaultServerGRPCGraceTimeout)
}