forked from api7/apisix-seed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
98 lines (84 loc) · 1.9 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
package main
import (
"os"
"os/signal"
"sync"
"syscall"
"github.com/api7/gopkg/pkg/log"
rotatelogs "github.com/lestrrat-go/file-rotatelogs"
"go.uber.org/zap/zapcore"
"github.com/api7/apisix-seed/internal/conf"
"github.com/api7/apisix-seed/internal/core/components"
"github.com/api7/apisix-seed/internal/core/storer"
"github.com/api7/apisix-seed/internal/discoverer"
)
func initLogger(logConf *conf.Log) error {
opts := []log.Option{
log.WithLogLevel(logConf.Level),
log.WithSkipFrames(3),
}
if logConf.Path != "" {
writer, err := rotatelogs.New(
logConf.Path+"-%Y%m%d%H%M%S",
rotatelogs.WithLinkName(logConf.Path),
rotatelogs.WithMaxAge(logConf.MaxAge),
rotatelogs.WithRotationSize(logConf.MaxSize),
rotatelogs.WithRotationTime(logConf.RotationTime),
)
if err != nil {
return err
}
opts = append(opts, log.WithWriteSyncer(zapcore.AddSync(writer)))
} else {
opts = append(opts, log.WithOutputFile("stderr"))
}
l, err := log.NewLogger(opts...)
if err != nil {
return err
}
log.DefaultLogger = l
return nil
}
func main() {
conf.InitConf()
if err := initLogger(conf.LogConfig); err != nil {
log.Fatal(err)
}
etcdClient, err := storer.NewEtcd(conf.ETCDConfig)
if err != nil {
panic(err)
}
wg := sync.WaitGroup{}
wg.Add(2)
go func() {
defer wg.Done()
err := storer.InitStores(etcdClient)
if err != nil {
panic(err)
}
}()
go func() {
defer wg.Done()
err := discoverer.InitDiscoverers()
if err != nil {
panic(err)
}
}()
wg.Wait()
rewriter := components.Rewriter{
Prefix: conf.ETCDConfig.Prefix,
}
rewriter.Init()
defer rewriter.Close()
watcher := components.Watcher{}
err = watcher.Init()
if err != nil {
log.Error(err.Error())
return
}
watcher.Watch()
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
sig := <-quit
log.Infof("APISIX-Seed receive %s and start shutting down", sig.String())
}