-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
128 lines (106 loc) · 4.32 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
// Copyright (c) 2024 ESET
// See LICENSE file for redistribution.
package main
import (
"context"
"fmt"
logging "log/slog"
"os"
"os/signal"
"strings"
"syscall"
"time"
jErrors "github.com/juju/errors"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)
const (
defaultRequestTimeout = 5 * time.Second
defaultReadTimeout = 10 * time.Second
descriptorTimeout = time.Minute
maxRequestSize = 10024
httpServerAddr = "0.0.0.0:8080"
defaultDescriptorsFetchingType = "remote"
reflectionServiceName = "grpc.reflection.v1.ServerReflection/ServerReflectionInfo"
excludedDescriptors = "grpc.health.v1.Health,grpc.reflection.v1.ServerReflection"
grpcServerAddr = "0.0.0.0:50051"
tls = false
tlsSkipverify = false
defaultUseProtoNames = false
defaultEmitUnpopulated = false
defaultEmitDefaultValues = false
)
var (
Commit string
Version string
Build string
)
func main() {
conf := &Config{}
pflag.Uint("transport.http.maxRequestSizeKB", maxRequestSize, "maximum size of requests in KB")
pflag.Duration("transport.http.requestTimeout", defaultRequestTimeout, "request timeout")
pflag.String("transport.http.server.addr", httpServerAddr, "address and port of the HTTP server")
pflag.Duration("transport.http.server.gracefulTimeout", defaultRequestTimeout, "graceful timeout")
pflag.Duration("transport.http.server.readTimeout", defaultReadTimeout, "read timeout")
pflag.Duration("transport.http.server.readHeaderTimeout", defaultRequestTimeout, "read header timeout")
pflag.String("descriptors.kind", defaultDescriptorsFetchingType, "type of descriptors fetching")
pflag.Duration("descriptors.remote.timeout", descriptorTimeout, "request timeout for remote descriptors")
pflag.String("descriptors.remote.reflectionServiceName", reflectionServiceName, "reflection service name")
pflag.StringArray("descriptors.remote.exclude", strings.Split(excludedDescriptors, ","), "remote descriptors to exclude")
pflag.Duration("gateways.grpc.requestTimeout", defaultRequestTimeout, "client request timeout")
pflag.String("gateways.grpc.client.targetAddr", grpcServerAddr, "address and port of the gRPC server")
pflag.Duration("gateways.grpc.client.requestTimeout", defaultRequestTimeout, "requests timeout")
pflag.Bool("gateways.grpc.client.tls", tls, "use TLS for gRPC connection")
pflag.Bool("gateways.grpc.client.tlsSkipverify", tlsSkipverify, "skip TLS verification")
pflag.Bool("service.jsonencoder.useProtoNames", defaultUseProtoNames, "use proto names in JSON response (instead of camel case)")
pflag.Bool("service.jsonencoder.emitUnpopulated", defaultEmitUnpopulated, "emit unpopulated fields in JSON response for empty gRPC values")
pflag.Bool("service.jsonencoder.emitDefaultValues", defaultEmitDefaultValues, "include default values in JSON response for empty gRPC values") //nolint:lll
pflag.BoolP("version", "v", false, "print version")
configFile := pflag.StringP("config", "c", "", "path to config file")
pflag.Parse()
err := viper.BindPFlags(pflag.CommandLine)
if err != nil {
logging.Error(jErrors.Details(jErrors.Trace(err)))
os.Exit(1)
}
if viper.GetBool("version") {
fmt.Println("Version:\t", Version)
fmt.Println("Build: \t", Build)
fmt.Println("Commit: \t", Commit)
os.Exit(0)
}
err = viper.Unmarshal(&conf)
if err != nil {
logging.Error(jErrors.Details(jErrors.Annotate(err, "decode configuration to struct")))
os.Exit(1)
}
if configFile != nil && *configFile != "" {
err := loadConfigFromFile(*configFile, conf)
if err != nil {
logging.Error(jErrors.Details(jErrors.Trace(err)))
os.Exit(1)
}
}
err = conf.validate()
if err != nil {
logging.Error(jErrors.Details(jErrors.Annotate(err, "configuration validation error")))
os.Exit(1)
}
os.Exit(run(conf))
}
func run(conf *Config) int {
logging.SetDefault(logging.New(logging.NewJSONHandler(os.Stderr, &logging.HandlerOptions{Level: logging.LevelDebug})))
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer cancel()
app, err := New(ctx, conf)
if err != nil {
logging.Error(jErrors.Details(jErrors.Trace(err)))
return 1
}
err = app.Run(ctx)
if err != nil {
logging.Error(jErrors.Details(jErrors.Trace(err)))
return 1
}
return 0
}