-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathmain.go
96 lines (81 loc) · 2.44 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
package main
import (
"fmt"
"log/slog"
"os"
"time"
metrics "code.cloudfoundry.org/go-metric-registry"
"code.cloudfoundry.org/tlsconfig"
"net/http"
//nolint: gosec
_ "net/http/pprof"
. "code.cloudfoundry.org/log-cache/internal/gateway"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"
)
func init() {
slog.SetDefault(slog.New(slog.NewTextHandler(os.Stderr, nil)))
}
func main() {
slog.Info("Starting Log Cache Gateway...")
defer slog.Info("Log Cache Gateway stopped.")
cfg, err := LoadConfig()
if err != nil {
panic(err)
}
metricServerOption := metrics.WithTLSServer(
int(cfg.MetricsServer.Port),
cfg.MetricsServer.CertFile,
cfg.MetricsServer.KeyFile,
cfg.MetricsServer.CAFile,
)
if cfg.MetricsServer.CAFile == "" {
metricServerOption = metrics.WithPublicServer(int(cfg.MetricsServer.Port))
}
m := metrics.NewRegistry(
slog.NewLogLogger(slog.Default().Handler(), slog.LevelInfo),
metricServerOption,
)
if cfg.MetricsServer.DebugMetrics {
m.RegisterDebugMetrics()
pprofServer := &http.Server{
Addr: fmt.Sprintf("127.0.0.1:%d", cfg.MetricsServer.PprofPort),
Handler: http.DefaultServeMux,
ReadHeaderTimeout: 2 * time.Second,
}
go func() { slog.Info("pprof server stopped", "error", pprofServer.ListenAndServe().Error()) }()
}
gatewayOptions := []GatewayOption{
WithGatewayVersion(cfg.Version),
WithGatewayBlock(),
}
if cfg.ProxyCertPath != "" || cfg.ProxyKeyPath != "" {
gatewayOptions = append(gatewayOptions, WithGatewayTLSServer(cfg.ProxyCertPath, cfg.ProxyKeyPath))
}
if cfg.TLS.HasAnyCredential() {
tlsConfig, err := tlsconfig.Build(
tlsconfig.WithInternalServiceDefaults(),
tlsconfig.WithIdentityFromFile(cfg.TLS.CertPath, cfg.TLS.KeyPath),
).Client(
tlsconfig.WithAuthorityFromFile(cfg.TLS.CAPath),
tlsconfig.WithServerName("log-cache"),
)
if err != nil {
panic(err)
}
gatewayOptions = append(gatewayOptions, WithGatewayLogCacheDialOpts(
grpc.WithTransportCredentials(credentials.NewTLS(tlsConfig)),
grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(50*1024*1024)),
),
)
} else {
gatewayOptions = append(gatewayOptions, WithGatewayLogCacheDialOpts(
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(50*1024*1024)),
),
)
}
gateway := NewGateway(cfg.LogCacheAddr, cfg.Addr, gatewayOptions...)
gateway.Start()
}