-
-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathmain.go
59 lines (46 loc) · 1.18 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
package main
import (
"os"
"os/signal"
"github.com/datarhei/core/v16/app/api"
"github.com/datarhei/core/v16/config/store"
"github.com/datarhei/core/v16/log"
_ "github.com/joho/godotenv/autoload"
)
func main() {
logger := log.New("Core").WithOutput(log.NewConsoleWriter(os.Stderr, log.Lwarn, true))
configfile := store.Location(os.Getenv("CORE_CONFIGFILE"))
app, err := api.New(configfile, os.Stderr)
if err != nil {
logger.Error().WithError(err).Log("Failed to create new API")
os.Exit(1)
}
go func() {
defer func() {
if proc, err := os.FindProcess(os.Getpid()); err == nil {
proc.Signal(os.Interrupt)
}
}()
for {
if err := app.Start(); err != api.ErrConfigReload {
if err != nil {
logger.Error().WithError(err).Log("Failed to start API")
}
break
} else {
logger.Warn().WithError(err).Log("Config reload requested")
}
app.Stop()
if err := app.Reload(); err != nil {
logger.Error().WithError(err).Log("Failed to reload config")
break
}
}
}()
// Wait for interrupt signal to gracefully shutdown the app
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt)
<-quit
// Stop the app
app.Destroy()
}