-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
270 lines (242 loc) · 8.58 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package main
import (
"context"
"crypto/tls"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"regexp"
"strings"
"syscall"
"time"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"github.com/rs/cors"
"golang.org/x/net/http2"
"golang.org/x/net/http2/h2c"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/reflection"
"google.golang.org/protobuf/encoding/protojson"
grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
grpc_zap "github.com/grpc-ecosystem/go-grpc-middleware/logging/zap"
grpc_recovery "github.com/grpc-ecosystem/go-grpc-middleware/recovery"
"github.com/instill-ai/mgmt-backend/config"
"github.com/instill-ai/mgmt-backend/pkg/external"
"github.com/instill-ai/mgmt-backend/pkg/handler"
"github.com/instill-ai/mgmt-backend/pkg/logger"
"github.com/instill-ai/mgmt-backend/pkg/middleware"
"github.com/instill-ai/mgmt-backend/pkg/repository"
"github.com/instill-ai/mgmt-backend/pkg/service"
"github.com/instill-ai/mgmt-backend/pkg/usage"
database "github.com/instill-ai/mgmt-backend/pkg/db"
mgmtPB "github.com/instill-ai/protogen-go/vdp/mgmt/v1alpha"
)
func grpcHandlerFunc(grpcServer *grpc.Server, gwHandler http.Handler, CORSOrigins []string) http.Handler {
return h2c.NewHandler(
cors.New(cors.Options{
AllowedOrigins: CORSOrigins,
AllowCredentials: true,
AllowedMethods: []string{http.MethodHead, http.MethodGet, http.MethodPost, http.MethodPatch, http.MethodDelete},
Debug: false,
}).Handler(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.ProtoMajor == 2 && strings.Contains(r.Header.Get("Content-Type"), "application/grpc") {
grpcServer.ServeHTTP(w, r)
} else {
gwHandler.ServeHTTP(w, r)
}
})),
&http2.Server{})
}
func main() {
if err := config.Init(); err != nil {
log.Fatal(err.Error())
}
logger.InitZapLogger(config.Config.Server.Debug)
logger, _ := logger.GetZapLogger()
defer func() {
// can't handle the error due to https://github.com/uber-go/zap/issues/880
_ = logger.Sync()
}()
grpc_zap.ReplaceGrpcLoggerV2(logger)
db := database.GetConnection(&config.Config.Database)
defer database.Close(db)
// Shared options for the logger, with a custom gRPC code to log level functions.
opts := []grpc_zap.Option{
grpc_zap.WithDecider(func(fullMethodName string, err error) bool {
// will not log gRPC calls if it was a call to liveness or readiness and no error was raised
if err == nil {
if match, _ := regexp.MatchString("vdp.mgmt.v1alpha.MgmtPrivateService/.*ness$", fullMethodName); match {
return false
}
if match, _ := regexp.MatchString("vdp.mgmt.v1alpha.MgmtPublicService/.*ness$", fullMethodName); match {
return false
}
}
// by default everything will be logged
return true
}),
}
grpcServerOpts := []grpc.ServerOption{
grpc.StreamInterceptor(grpc_middleware.ChainStreamServer(
middleware.StreamAppendMetadataInterceptor,
grpc_zap.StreamServerInterceptor(logger, opts...),
grpc_recovery.StreamServerInterceptor(middleware.RecoveryInterceptorOpt()),
)),
grpc.UnaryInterceptor(grpc_middleware.ChainUnaryServer(
middleware.UnaryAppendMetadataInterceptor,
grpc_zap.UnaryServerInterceptor(logger, opts...),
grpc_recovery.UnaryServerInterceptor(middleware.RecoveryInterceptorOpt()),
)),
}
// Create tls based credential
var creds credentials.TransportCredentials
var tlsConfig *tls.Config
var err error
if config.Config.Server.HTTPS.Cert != "" && config.Config.Server.HTTPS.Key != "" {
tlsConfig = &tls.Config{
ClientAuth: tls.RequireAndVerifyClientCert,
}
creds, err = credentials.NewServerTLSFromFile(config.Config.Server.HTTPS.Cert, config.Config.Server.HTTPS.Key)
if err != nil {
logger.Fatal(fmt.Sprintf("failed to create credentials: %v", err))
}
grpcServerOpts = append(grpcServerOpts, grpc.Creds(creds))
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
repository := repository.NewRepository(db)
service := service.NewService(repository)
// Start usage reporter
var usg usage.Usage
if !config.Config.Server.DisableUsage {
usageServiceClient, usageServiceClientConn := external.InitUsageServiceClient(&config.Config.UsageServer)
if usageServiceClientConn != nil {
defer usageServiceClientConn.Close()
logger.Info("try to start usage reporter")
go func() {
for {
usg = usage.NewUsage(ctx, repository, usageServiceClient, config.Config.Server.Edition)
if usg != nil {
usg.StartReporter(ctx)
logger.Info("usage reporter started")
break
}
logger.Warn("retry to start usage reporter after 5 minutes")
time.Sleep(5 * time.Minute)
}
}()
}
}
privateGrpcS := grpc.NewServer(grpcServerOpts...)
reflection.Register(privateGrpcS)
publicGrpcS := grpc.NewServer(grpcServerOpts...)
reflection.Register(publicGrpcS)
mgmtPB.RegisterMgmtPrivateServiceServer(
privateGrpcS,
handler.NewPrivateHandler(service),
)
mgmtPB.RegisterMgmtPublicServiceServer(
publicGrpcS,
handler.NewPublicHandler(service, usg, config.Config.Server.DisableUsage),
)
privateServeMux := runtime.NewServeMux(
runtime.WithForwardResponseOption(middleware.HttpResponseModifier),
runtime.WithIncomingHeaderMatcher(middleware.CustomMatcher),
runtime.WithErrorHandler(middleware.ErrorHandler),
runtime.WithMarshalerOption(runtime.MIMEWildcard, &runtime.JSONPb{
MarshalOptions: protojson.MarshalOptions{
UseProtoNames: true,
EmitUnpopulated: true,
UseEnumNumbers: false,
},
UnmarshalOptions: protojson.UnmarshalOptions{
DiscardUnknown: true,
},
}),
)
publicServeMux := runtime.NewServeMux(
runtime.WithIncomingHeaderMatcher(middleware.CustomMatcher),
runtime.WithForwardResponseOption(middleware.HttpResponseModifier),
runtime.WithErrorHandler(middleware.ErrorHandler),
runtime.WithMarshalerOption(runtime.MIMEWildcard, &runtime.JSONPb{
MarshalOptions: protojson.MarshalOptions{
UseProtoNames: true,
EmitUnpopulated: true,
UseEnumNumbers: false,
},
UnmarshalOptions: protojson.UnmarshalOptions{
DiscardUnknown: true,
},
}),
)
// Start gRPC server
var dialOpts []grpc.DialOption
if config.Config.Server.HTTPS.Cert != "" && config.Config.Server.HTTPS.Key != "" {
dialOpts = []grpc.DialOption{grpc.WithTransportCredentials(creds)}
} else {
dialOpts = []grpc.DialOption{grpc.WithTransportCredentials(insecure.NewCredentials())}
}
if err := mgmtPB.RegisterMgmtPrivateServiceHandlerFromEndpoint(ctx, privateServeMux, fmt.Sprintf(":%v", config.Config.Server.PrivatePort), dialOpts); err != nil {
logger.Fatal(err.Error())
}
if err := mgmtPB.RegisterMgmtPublicServiceHandlerFromEndpoint(ctx, publicServeMux, fmt.Sprintf(":%v", config.Config.Server.PublicPort), dialOpts); err != nil {
logger.Fatal(err.Error())
}
privateHTTPServer := &http.Server{
Addr: fmt.Sprintf(":%v", config.Config.Server.PrivatePort),
Handler: grpcHandlerFunc(privateGrpcS, privateServeMux, config.Config.Server.CORSOrigins),
TLSConfig: tlsConfig,
}
publicHTTPServer := &http.Server{
Addr: fmt.Sprintf(":%v", config.Config.Server.PublicPort),
Handler: grpcHandlerFunc(publicGrpcS, publicServeMux, config.Config.Server.CORSOrigins),
TLSConfig: tlsConfig,
}
// Wait for interrupt signal to gracefully shutdown the server with a timeout of 5 seconds.
quitSig := make(chan os.Signal, 1)
errSig := make(chan error)
if config.Config.Server.HTTPS.Cert != "" && config.Config.Server.HTTPS.Key != "" {
go func() {
if err := privateHTTPServer.ListenAndServeTLS(config.Config.Server.HTTPS.Cert, config.Config.Server.HTTPS.Key); err != nil {
errSig <- err
}
}()
go func() {
if err := publicHTTPServer.ListenAndServeTLS(config.Config.Server.HTTPS.Cert, config.Config.Server.HTTPS.Key); err != nil {
errSig <- err
}
}()
} else {
go func() {
if err := privateHTTPServer.ListenAndServe(); err != nil {
errSig <- err
}
}()
go func() {
if err := publicHTTPServer.ListenAndServe(); err != nil {
errSig <- err
}
}()
}
logger.Info("gRPC servers are running.")
// kill (no param) default send syscall.SIGTERM
// kill -2 is syscall.SIGINT
// kill -9 is syscall.SIGKILL but can't be catch, so don't need add it
signal.Notify(quitSig, syscall.SIGINT, syscall.SIGTERM)
select {
case err := <-errSig:
logger.Error(fmt.Sprintf("Fatal error: %v\n", err))
case <-quitSig:
// send out the usage report at exit
if !config.Config.Server.DisableUsage && usg != nil {
usg.TriggerSingleReporter(ctx)
}
logger.Info("Shutting down server...")
privateGrpcS.GracefulStop()
publicGrpcS.GracefulStop()
}
}