-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
77 lines (65 loc) · 2.53 KB
/
main.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
70
71
72
73
74
75
76
77
// Server main program.
package main
import (
"context"
"log"
grpclogsettable "github.com/grpc-ecosystem/go-grpc-middleware/logging/settable"
grpczap "github.com/grpc-ecosystem/go-grpc-middleware/logging/zap"
grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
"go.opentelemetry.io/otel"
"go.uber.org/zap"
_ "google.golang.org/genproto/googleapis/rpc/errdetails"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"github.com/indrasaputra/arjuna/gateway/config"
"github.com/indrasaputra/arjuna/gateway/server"
"github.com/indrasaputra/arjuna/pkg/sdk/trace"
apiv1 "github.com/indrasaputra/arjuna/proto/api/v1"
)
func main() {
ctx := context.Background()
cfg, err := config.NewConfig(".env")
checkError(err)
_, err = trace.NewProvider(ctx, cfg.Tracer)
checkError(err)
gatewayServer := server.NewGrpcGateway(cfg.Port)
options := defaultGrpcServerOptions()
registerGrpcGatewayService(context.Background(), gatewayServer, cfg, options...)
log.Println("running grpc gateway server...")
_ = gatewayServer.Serve()
}
func registerGrpcGatewayService(ctx context.Context, gatewayServer *server.GrpcGateway, cfg *config.Config, options ...grpc.DialOption) {
gatewayServer.AttachService(func(server *runtime.ServeMux) error {
if err := apiv1.RegisterUserCommandServiceHandlerFromEndpoint(ctx, server, cfg.UserServiceAddress, options); err != nil {
return err
}
if err := apiv1.RegisterUserQueryServiceHandlerFromEndpoint(ctx, server, cfg.UserServiceAddress, options); err != nil {
return err
}
return apiv1.RegisterAuthServiceHandlerFromEndpoint(ctx, server, cfg.AuthServiceAddress, options)
})
}
func defaultGrpcServerOptions() []grpc.DialOption {
logger, _ := zap.NewProduction() // error is impossible, hence ignored.
grpczap.SetGrpcLoggerV2(grpclogsettable.ReplaceGrpcLoggerV2(), logger)
return []grpc.DialOption{
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithChainUnaryInterceptor(
otelgrpc.UnaryClientInterceptor(otelgrpc.WithTracerProvider(otel.GetTracerProvider())),
grpczap.UnaryClientInterceptor(logger),
grpc_prometheus.UnaryClientInterceptor,
),
grpc.WithChainStreamInterceptor(
otelgrpc.StreamClientInterceptor(otelgrpc.WithTracerProvider(otel.GetTracerProvider())),
grpczap.StreamClientInterceptor(logger),
grpc_prometheus.StreamClientInterceptor,
),
}
}
func checkError(err error) {
if err != nil {
panic(err)
}
}