-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
100 lines (84 loc) · 2.42 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
99
100
package main
import (
"context"
"daslink/config"
"daslink/dao"
"fmt"
"os"
"sync"
"github.com/scorpiotzh/mylog"
"github.com/scorpiotzh/toolib"
"github.com/urfave/cli/v2"
)
var (
log = mylog.NewLogger("main", mylog.LevelDebug)
exit = make(chan struct{})
ctxServer, cancel = context.WithCancel(context.Background())
wgServer = sync.WaitGroup{}
)
func main() {
log.Debugf("server start:")
app := &cli.App{
Flags: []cli.Flag{
&cli.StringFlag{
Name: "config",
Aliases: []string{"c"},
Usage: "Load configuration from `FILE`",
},
},
Action: runServer,
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}
func runServer(ctx *cli.Context) error {
// config
configFilePath := ctx.String("config")
if err := config.InitCfg(configFilePath); err != nil {
return err
}
// db
cfgMysql := config.Cfg.DB.Mysql
db, err := dao.NewGormDataBase(cfgMysql.Addr, cfgMysql.User, cfgMysql.Password, cfgMysql.DbName, cfgMysql.MaxOpenConn, cfgMysql.MaxIdleConn)
if err != nil {
return fmt.Errorf("NewGormDataBase err:%s", err.Error())
}
dbDao := dao.Initialize(db, cfgMysql.LogMode)
log.Info("db ok")
// dns
cfgCloudflare := config.Cfg.CloudFlare
dnsData, err := NewDNSData(cfgCloudflare.ApiKey, cfgCloudflare.ApiEmail, cfgCloudflare.ZoneName, config.Cfg.Gateway.Ipfs, config.Cfg.Gateway.Skynet, config.Cfg.HostName.Suffix)
if err != nil {
return fmt.Errorf("NewDNSData err:%s", err.Error())
}
log.Info("dns data ok")
// read all das accounts that has ipfs/ipns/skynet record
contentRecordList, _ := dbDao.FindRecordInfoByKeys([]string{"ipfs", "ipns", "skynet"})
jobsChanLength := len(contentRecordList)
if jobsChanLength == 0 {
jobsChanLength = 1
}
jobsChan := make(chan string, jobsChanLength)
maxId := uint64(0)
if len(contentRecordList) > 0 {
maxId = contentRecordList[len(contentRecordList)-1].Id
}
runWatcher(&wgServer, dbDao, maxId, jobsChan)
log.Info("Watching new content records...")
runSyncContentRecords(contentRecordList, dnsData, jobsChan)
log.Info("All content records have been synchronized")
runWorker(&wgServer, dbDao, dnsData, jobsChan)
log.Info("Worker started")
// quit monitor
toolib.ExitMonitoring(func(sig os.Signal) {
log.Warn("ExitMonitoring:", sig.String())
cancel()
log.Warn("Wait for worker to finish...")
wgServer.Wait()
exit <- struct{}{}
})
<-exit
log.Warn("success exit server. bye bye!")
return nil
}