-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
109 lines (88 loc) · 3.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
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
101
102
103
104
105
106
107
108
109
package main
import (
"fmt"
"os"
"syscall"
"time"
"github.com/gin-gonic/gin"
"github.com/spf13/pflag"
"github.com/spf13/viper"
commonDb "github.com/fwtpe/owl-backend/common/db"
commonGin "github.com/fwtpe/owl-backend/common/gin"
log "github.com/fwtpe/owl-backend/common/logruslog"
commonOs "github.com/fwtpe/owl-backend/common/os"
commonQueue "github.com/fwtpe/owl-backend/common/queue"
"github.com/fwtpe/owl-backend/common/vipercfg"
"github.com/fwtpe/owl-backend/modules/mysqlapi/rdb"
"github.com/fwtpe/owl-backend/modules/mysqlapi/restful"
"github.com/fwtpe/owl-backend/modules/mysqlapi/service"
"github.com/fwtpe/owl-backend/modules/mysqlapi/service/hbscache"
owlSrv "github.com/fwtpe/owl-backend/modules/mysqlapi/service/owl"
)
var logger = log.NewDefaultLogger("INFO")
func main() {
/**
* Initialize loader of configurations
*/
confLoader := vipercfg.NewOwlConfigLoader()
confLoader.FlagDefiner = pflagDefine
confLoader.ProcessTrueValueCallbacks()
// :~)
config := confLoader.MustLoadConfigFile()
rdb.InitPortalRdb(toRdbConfig(config, "portal"))
rdb.InitGraphRdb(toRdbConfig(config, "graph"))
rdb.InitBossRdb(toRdbConfig(config, "boss"))
restful.InitGin(toGinConfig(config))
restful.InitCache(toCacheConfig(config))
service.InitNqmHeartbeat(toNqmHeartbeatConfig(config))
service.InitCachedTargetList(toTargetListConfig(config))
owlSrv.InitQueryObjectService(*toQueryObjectServiceConfig(config))
hbscache.Init()
commonOs.HoldingAndWaitSignal(exitApp, syscall.SIGINT, syscall.SIGTERM)
}
func exitApp(signal os.Signal) {
service.CloseNqmHeartbeat()
service.CloseCachedTargetList()
rdb.ReleaseAllRdb()
}
func toGinConfig(config *viper.Viper) *commonGin.GinConfig {
return &commonGin.GinConfig{
Mode: gin.ReleaseMode,
Host: config.GetString("restful.listen.host"),
Port: uint16(config.GetInt("restful.listen.port")),
}
}
func toRdbConfig(config *viper.Viper, dbname string) *commonDb.DbConfig {
return &commonDb.DbConfig{
Dsn: config.GetString(fmt.Sprintf("rdb.%s.dsn", dbname)),
MaxIdle: config.GetInt(fmt.Sprintf("rdb.%s.maxIdle", dbname)),
}
}
func toQueryObjectServiceConfig(config *viper.Viper) *owlSrv.QueryObjectServiceConfig {
return &owlSrv.QueryObjectServiceConfig{
CacheSize: config.GetInt64("queryObject.cache.size"),
CacheDuration: time.Duration(config.GetInt64("queryObject.cache.hourDuration")) * time.Hour,
}
}
func toCacheConfig(config *viper.Viper) *restful.CacheConfig {
return &restful.CacheConfig{
Size: config.GetInt("nqm.pingList.cache.size"),
Lifetime: config.GetInt("nqm.pingList.cache.lifetime"),
}
}
func toNqmHeartbeatConfig(config *viper.Viper) *commonQueue.Config {
return &commonQueue.Config{
Num: config.GetInt("heartbeat.nqm.batchSize"),
Dur: time.Duration(config.GetInt("heartbeat.nqm.duration")) * time.Second,
}
}
func toTargetListConfig(config *viper.Viper) *service.NqmCachedTargetListConfig {
return &service.NqmCachedTargetListConfig{
Size: config.GetInt("heartbeat.nqm.targetList.size"),
Dur: time.Duration(config.GetInt("heartbeat.nqm.targetList.duration")) * time.Minute,
}
}
func pflagDefine() {
pflag.StringP("config", "c", "cfg.json", "configuration file")
pflag.BoolP("help", "h", false, "usage")
}