-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
88 lines (74 loc) · 1.79 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
package main
import (
"context"
"fmt"
"net/http"
"os"
"os/signal"
"syscall"
"github.com/Scalingo/go-utils/logger"
"github.com/curzolapierre/hook-manager/config"
redisCtr "github.com/curzolapierre/hook-manager/redis"
"github.com/curzolapierre/hook-manager/webserver"
"github.com/sirupsen/logrus"
)
func logLevel() logrus.Level {
switch os.Getenv("LOGGER_LEVEL") {
case "panic":
return logrus.PanicLevel
case "fatal":
return logrus.FatalLevel
case "warn":
return logrus.WarnLevel
case "info":
return logrus.InfoLevel
case "debug":
return logrus.DebugLevel
default:
return logrus.InfoLevel
}
}
func initLogger() logrus.FieldLogger {
logger := logrus.New()
logger.SetLevel(logLevel())
logger.Formatter = &logrus.TextFormatter{
TimestampFormat: "2006-01-02T15:04:05.000",
FullTimestamp: true,
}
var fieldLogger logrus.FieldLogger = logger
return fieldLogger
}
func main() {
log := initLogger()
ctx := logger.ToCtx(context.Background(), log)
config, err := config.Lookup()
if err != nil {
log.WithError(err).Panic("Fail to load environment")
return
}
_, err = redisCtr.Client(config)
if err != nil {
log.WithError(err).Panic("fail to init redis client")
return
}
httpListenAddr := fmt.Sprintf("%s:%s", config.HttpHost, config.HttpPort)
log.Infof("Starting the web server on %v", httpListenAddr)
// Define routers
router := webserver.NewRouter(ctx, config)
go func() {
err := http.ListenAndServe(httpListenAddr, router)
if err != nil {
log.WithError(err).Error("Fail to start web server")
os.Exit(-1)
}
}()
signals := make(chan os.Signal, 1)
signal.Notify(signals, syscall.SIGTERM, syscall.SIGINT, syscall.SIGQUIT)
for range signals {
log.Info("Stopping the server")
// for _, stopper := range stoppers {
// stopper()
// }
os.Exit(0)
}
}