-
Notifications
You must be signed in to change notification settings - Fork 0
/
launcher.go
260 lines (207 loc) · 6.42 KB
/
launcher.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
package launcher
import (
"flag"
"fmt"
"os"
"runtime"
"strings"
"time"
"github.com/alrusov/config"
"github.com/alrusov/jsonw"
"github.com/alrusov/log"
"github.com/alrusov/misc"
"github.com/alrusov/panic"
"github.com/alrusov/stdhttp"
_ "time/tzdata" // If the time package cannot find tzdata files on the system, it will use this embedded information
authbasic "github.com/alrusov/auth-basic"
authjwt "github.com/alrusov/auth-jwt"
authkeycloak "github.com/alrusov/auth-keycloak"
authkrb5 "github.com/alrusov/auth-krb5"
authurl "github.com/alrusov/auth-url"
)
//----------------------------------------------------------------------------------------------------------------------------//
// Application --
type Application interface {
CheckConfig() error
NewListener() (*stdhttp.HTTP, error)
}
var (
cfgFile string
)
//----------------------------------------------------------------------------------------------------------------------------//
// Go --
func Go(a Application, cfg any) {
defaultConfig, _ := misc.AbsPath(fmt.Sprintf("%s/%s.toml", misc.AppExecPath(), misc.AppName()))
flagEnvFiles := flag.String("env", "", "Environment files to use")
flagConfigFile := flag.String("config", defaultConfig, "Configuration file to use")
flagVersion := flag.Bool("version", false, "Daemon version")
flagDumpPanicIDs := flag.Bool("dump-panic-ids", false, "Dump panic IDs to log with ALERT level")
flagDisableConsoleLog := flag.Bool("disable-console-log", false, "Disable console log")
flagDebug := flag.Bool("debug", false, "Debug mode")
flag.Parse()
if *flagDebug {
panic.Disable()
}
if *flagDumpPanicIDs {
panic.SetDumpStack(true)
}
panicID := panic.ID()
defer panic.SaveStackToLogEx(panicID)
misc.Logger = log.StdLogger
if *flagDisableConsoleLog {
log.SetConsoleWriter(nil)
}
if *flagVersion {
ts := misc.BuildTime()
if ts != "" {
ts = " [" + ts + "Z]"
}
tags := misc.AppTags()
if tags != "" {
tags = " " + tags
}
fmt.Fprintf(os.Stderr, "%s %s%s%s, %s/%s\n%s\n", misc.AppName(), misc.AppVersion(), tags, ts, runtime.GOOS, runtime.GOARCH, misc.Copyright())
os.Exit(misc.ExVersion)
return // formally for validators
}
var err error
cfgFile, err = misc.AbsPath(*flagConfigFile)
if err != nil {
log.Message(log.ALERT, "Incorrect config file name: %s", err)
misc.StopApp(misc.ExIncorrectConfigFile)
misc.Exit()
return // formally for validators
}
envFiles := []string{".env", ".env-local"}
stdEnv := true
if len(*flagEnvFiles) != 0 {
envFiles = strings.Split(*flagEnvFiles, ",")
stdEnv = false
}
for _, fn := range envFiles {
fn = strings.TrimSpace(fn)
if fn == "" {
continue
}
if err := misc.LoadEnv(fn); err != nil && !stdEnv {
log.Message(log.ALERT, "Incorrect environment file: %s", err)
misc.StopApp(misc.ExIncorrectConfigFile)
misc.Exit()
return // formally for validators
}
}
if err := config.LoadFile(cfgFile, cfg); err != nil {
log.Message(log.ALERT, "Incorrect config file: %s", err)
misc.StopApp(misc.ExIncorrectConfigFile)
misc.Exit()
return // formally for validators
}
cc := config.GetCommon()
if cc == nil {
log.Message(log.ALERT, "Config has an incorrect structure\n")
misc.StopApp(misc.ExConfigIncorrect)
misc.Exit()
return // formally for validators
}
jsonw.UseStd(cc.UseStdJSON)
log.MaxLen(cc.LogMaxStringLen)
log.SetFile(cc.LogDir, "", cc.LogLocalTime, cc.LogBufferSize, cc.LogBufferDelay.D())
log.SetLogLevels(cc.LogLevel, cc.LogLevels, log.FuncNameModeNone)
log.Message(log.DEBUG, "Config file:\n>>>\n%s\n<<<", string(config.GetSecuredText()))
if err := a.CheckConfig(); err != nil {
log.Message(log.ALERT, "Config errors: %s", err.Error())
misc.StopApp(misc.ExConfigErrors)
misc.Exit()
return // formally for validators
}
if cc.GoMaxProcs > 0 {
runtime.GOMAXPROCS(cc.GoMaxProcs)
}
if cc.DeepProfiling {
runtime.SetBlockProfileRate(1)
runtime.SetMutexProfileFraction(1)
}
start(a, cc)
misc.Exit()
}
//----------------------------------------------------------------------------------------------------------------------------//
func processor(a Application, cc *config.Common) {
stdhttp.SetMinSizeForGzip(cc.MinSizeForGzip)
listener, err := a.NewListener()
if err != nil {
log.Message(log.CRIT, "Create listener error: %s", err.Error())
misc.StopApp(misc.ExCreateListenerError)
return
}
listener.SetName(cc.Name, cc.Description)
err = addAuth(listener)
if err != nil {
log.Message(log.CRIT, "Auth initialization: %s", err.Error())
misc.StopApp(misc.ExCreateListenerError)
return
}
go memStats(cc)
go func() {
panicID := panic.ID()
defer panic.SaveStackToLogEx(panicID)
misc.WaitingForStop()
log.Message(log.INFO, "Stopping...")
listener.Stop()
}()
log.Message(log.INFO, "Press Ctrl+C for exit")
if err := listener.Start(); err != nil {
log.Message(log.CRIT, "Start listener error: %s", err.Error())
misc.StopApp(misc.ExStartListenerError)
return
}
}
//----------------------------------------------------------------------------------------------------------------------------//
func addAuth(listener *stdhttp.HTTP) (err error) {
err = authbasic.Add(listener)
if err != nil {
return err
}
err = authjwt.Add(listener)
if err != nil {
return err
}
err = authkrb5.Add(listener)
if err != nil {
return err
}
err = authkeycloak.Add(listener)
if err != nil {
return err
}
err = authurl.Add(listener)
if err != nil {
return err
}
return
}
//----------------------------------------------------------------------------------------------------------------------------//
func memStats(cc *config.Common) {
panicID := panic.ID()
defer panic.SaveStackToLogEx(panicID)
if cc.MemStatsPeriod > 0 {
level, ok := log.Str2Level(cc.MemStatsLevel)
if !ok {
level = log.DEBUG
}
if level <= log.CurrentLogLevel() {
delay := cc.MemStatsPeriod
var mem runtime.MemStats
for {
//debug.FreeOSMemory()
misc.Sleep(1 * time.Second)
runtime.ReadMemStats(&mem)
log.Message(level, "AllocSys %d, HeapSys %d, HeapInuse: %d, HeapObjects %d, StackSys: %d, StackInuse: %d; NumCPU: %d; GoMaxProcs: %d; NumGoroutine: %d",
mem.Sys, mem.HeapSys, mem.HeapInuse, mem.HeapObjects, mem.StackSys, mem.StackInuse, runtime.NumCPU(), runtime.GOMAXPROCS(-1), runtime.NumGoroutine())
if !misc.Sleep(delay.D()) {
break
}
}
}
}
}
//----------------------------------------------------------------------------------------------------------------------------//