-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
85 lines (64 loc) · 1.49 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
package main
import (
"context"
"flag"
"os"
"os/signal"
"time"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
)
type (
Error string
)
func (e Error) Error() string {
return string(e)
}
func init() {
logrus.SetLevel(logrus.InfoLevel)
logrus.SetFormatter(&logrus.TextFormatter{})
logrus.SetOutput(os.Stderr)
}
func main() {
stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt)
file := flag.String("config", "config.yaml", "config file")
flag.Parse()
v, err := LoadConfiguration(*file)
if err != nil {
logrus.WithField("context", "load_configuration").Fatal(err)
}
l := LoadLogger(v)
app, err := NewApp(l, v)
if err != nil {
l.WithField("context", "init_app").Fatal(err)
}
app.RegisterHandler()
go func() {
if err := app.Listen(); err != nil {
l.WithField("context", "listen").Fatal(err)
}
}()
<-stop
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := app.Close(ctx); err != nil {
l.WithField("context", "close").Fatal(err)
}
}
func LoadConfiguration(filename string) (*viper.Viper, error) {
v := viper.New()
v.SetConfigFile(filename)
if err := v.ReadInConfig(); err != nil {
return nil, err
}
logrus.WithField("filename", v.ConfigFileUsed()).Info("reading config file")
return v, nil
}
func LoadLogger(v *viper.Viper) logrus.FieldLogger {
l := logrus.New()
l.SetLevel(logrus.Level(v.GetInt("app.log_level")))
l.SetFormatter(&logrus.TextFormatter{})
l.SetOutput(os.Stderr)
return l
}