-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
159 lines (117 loc) · 3.1 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
package main
import (
"log/slog"
"os"
"os/signal"
"sync"
"syscall"
)
var version = "dev"
type ctxKey string
const loggerKey ctxKey = "logging"
func loadConfig(ctx *Context) *Config {
cfg, err := NewConfigFile()
inititalizeLogger(ctx, cfg)
logger := ctx.Value(loggerKey).(*slog.Logger)
if err != nil {
logger.Error("Error loading config", "error", err)
return nil
}
return cfg
}
func inititalizeLogger(ctx *Context, cfg *Config) {
handlerOpts := &slog.HandlerOptions{
AddSource: false,
Level: slog.LevelInfo,
}
if cfg != nil {
switch cfg.Server.Logging.Level {
case "debug":
handlerOpts.Level = slog.LevelDebug
handlerOpts.AddSource = true
case "error":
handlerOpts.Level = slog.LevelError
default:
handlerOpts.Level = slog.LevelInfo
}
if cfg.Server.Logging.JSON {
ctx.Set(loggerKey, slog.New(slog.NewJSONHandler(os.Stdout, handlerOpts)))
return
}
}
ctx.Set(loggerKey, slog.New(slog.NewTextHandler(os.Stdout, handlerOpts)))
}
func handleSignals(server GenericServer) {
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, os.Interrupt, syscall.SIGTERM)
logger := server.GetContext().Value(loggerKey).(*slog.Logger)
go func() {
sig := <-signalChan
logger.Debug("Received signal", slog.String("signal", sig.String()))
if server != nil {
server.Stop()
}
os.Exit(0)
}()
}
func newNetStringServerInstance(instance Listen, ctx *Context, cfg *Config, wg *sync.WaitGroup) {
defer wg.Done()
logger := ctx.Value(loggerKey).(*slog.Logger)
server := NewMultiServer(ctx, cfg)
handleSignals(server)
err := server.Start(instance, server.HandleNetStringConnection)
if err != nil {
logger.Error("Server error", slog.String("error", err.Error()))
return
}
}
func newPolicyServiceInstance(instance Listen, ctx *Context, cfg *Config, wg *sync.WaitGroup) {
defer wg.Done()
logger := ctx.Value(loggerKey).(*slog.Logger)
server := NewMultiServer(ctx, cfg)
handleSignals(server)
err := server.Start(instance, server.HandlePolicyServiceConnection)
if err != nil {
logger.Error("Server error", slog.String("error", err.Error()))
return
}
}
func runServer(ctx *Context, cfg *Config) {
var wg sync.WaitGroup
taskCount := 0
logger := ctx.Value(loggerKey).(*slog.Logger)
logger.Info("Starting server", slog.String("version", version))
for _, instance := range cfg.Server.Listen {
if instance.Kind == "socket_map" {
wg.Add(1)
taskCount++
go newNetStringServerInstance(instance, ctx, cfg, &wg)
} else if instance.Kind == "policy_service" {
if instance.Name != "" {
wg.Add(1)
taskCount++
go newPolicyServiceInstance(instance, ctx, cfg, &wg)
} else {
logger.Error("Policy service requires a name")
return
}
} else {
logger.Error("Invalid listen instance", slog.String("instance", instance.Kind))
return
}
}
if taskCount > 0 {
wg.Wait()
logger.Info("Server stopped", slog.String("version", version))
} else {
logger.Error("No listen instances configured")
}
}
func main() {
ctx := NewContext()
cfg := loadConfig(ctx)
if cfg != nil {
InitializeHttpClient(cfg)
runServer(ctx, cfg)
}
}