-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
143 lines (125 loc) · 4.48 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
package main
import (
"context"
"flag"
"log"
"os"
"github.com/gatewayd-io/gatewayd-plugin-cache/plugin"
sdkConfig "github.com/gatewayd-io/gatewayd-plugin-sdk/config"
"github.com/gatewayd-io/gatewayd-plugin-sdk/logging"
"github.com/gatewayd-io/gatewayd-plugin-sdk/metrics"
p "github.com/gatewayd-io/gatewayd-plugin-sdk/plugin"
v1 "github.com/gatewayd-io/gatewayd-plugin-sdk/plugin/v1"
apiV1 "github.com/gatewayd-io/gatewayd/api/v1"
"github.com/getsentry/sentry-go"
"github.com/go-redis/redis/v8"
"github.com/hashicorp/go-hclog"
goplugin "github.com/hashicorp/go-plugin"
"github.com/spf13/cast"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
func main() {
sentryDSN := sdkConfig.GetEnv("SENTRY_DSN", "")
// Initialize Sentry SDK
err := sentry.Init(sentry.ClientOptions{
Dsn: sentryDSN,
TracesSampleRate: 1.0,
})
if err != nil {
log.Fatalf("Failed to initialize Sentry SDK: %s", err.Error())
}
// Parse command line flags, passed by GatewayD via the plugin config
logLevel := flag.String("log-level", "info", "Log level")
flag.Parse()
logger := hclog.New(&hclog.LoggerOptions{
Level: logging.GetLogLevel(*logLevel),
Output: os.Stderr,
JSONFormat: true,
Color: hclog.ColorOff,
})
pluginInstance := plugin.NewCachePlugin(plugin.Plugin{
Logger: logger,
})
//nolint:nestif
if cfg := cast.ToStringMap(plugin.PluginConfig["config"]); cfg != nil {
metricsConfig := metrics.NewMetricsConfig(cfg)
if metricsConfig != nil && metricsConfig.Enabled {
go metrics.ExposeMetrics(metricsConfig, logger)
}
// Initialize the API client to get proxies from GatewayD.
apiGRPCAddress := cast.ToString(cfg["apiGRPCAddress"])
apiClientConn, err := grpc.NewClient(
apiGRPCAddress, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil || apiClientConn == nil {
logger.Error("Failed to initialize API client", "error", err)
if pluginInstance.Impl.ExitOnStartupError {
logger.Info("Exiting due to startup error")
if apiClientConn != nil {
apiClientConn.Close()
}
os.Exit(1)
}
}
defer apiClientConn.Close()
pluginInstance.Impl.APIClient = apiV1.NewGatewayDAdminAPIServiceClient(apiClientConn)
cacheBufferSize := cast.ToUint(cfg["cacheBufferSize"])
if cacheBufferSize <= 0 {
cacheBufferSize = 100 // default value
}
pluginInstance.Impl.UpdateCacheChannel = make(chan *v1.Struct, cacheBufferSize)
go pluginInstance.Impl.UpdateCache(context.Background())
pluginInstance.Impl.RedisURL = cast.ToString(cfg["redisURL"])
pluginInstance.Impl.Expiry = cast.ToDuration(cfg["expiry"])
pluginInstance.Impl.DefaultDBName = cast.ToString(cfg["defaultDBName"])
pluginInstance.Impl.ScanCount = cast.ToInt64(cfg["scanCount"])
pluginInstance.Impl.ExitOnStartupError = cast.ToBool(cfg["exitOnStartupError"])
redisConfig, err := redis.ParseURL(pluginInstance.Impl.RedisURL)
if err != nil {
logger.Error("Failed to parse Redis URL", "error", err)
if pluginInstance.Impl.ExitOnStartupError {
logger.Info("Exiting due to startup error")
if apiClientConn != nil {
apiClientConn.Close()
}
os.Exit(1) //nolint:gocritic
}
}
pluginInstance.Impl.RedisClient = redis.NewClient(redisConfig)
// Ping the Redis server to check if it is available.
_, err = pluginInstance.Impl.RedisClient.Ping(context.Background()).Result()
if err != nil {
logger.Error("Failed to ping Redis server", "error", err)
if pluginInstance.Impl.ExitOnStartupError {
logger.Info("Exiting due to startup error")
if apiClientConn != nil {
apiClientConn.Close()
}
os.Exit(1)
}
}
pluginInstance.Impl.PeriodicInvalidatorEnabled = cast.ToBool(
cfg["periodicInvalidatorEnabled"])
pluginInstance.Impl.PeriodicInvalidatorStartDelay = cast.ToDuration(
cfg["periodicInvalidatorStartDelay"])
pluginInstance.Impl.PeriodicInvalidatorInterval = cast.ToDuration(
cfg["periodicInvalidatorInterval"])
// Start the periodic invalidator.
if pluginInstance.Impl.PeriodicInvalidatorEnabled {
pluginInstance.Impl.PeriodicInvalidator()
}
}
defer close(pluginInstance.Impl.UpdateCacheChannel)
goplugin.Serve(&goplugin.ServeConfig{
HandshakeConfig: goplugin.HandshakeConfig{
ProtocolVersion: 1,
MagicCookieKey: sdkConfig.GetEnv("MAGIC_COOKIE_KEY", ""),
MagicCookieValue: sdkConfig.GetEnv("MAGIC_COOKIE_VALUE", ""),
},
Plugins: goplugin.PluginSet{
plugin.PluginID.GetName(): pluginInstance,
},
GRPCServer: p.DefaultGRPCServer,
Logger: logger,
})
}