-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
138 lines (103 loc) · 2.66 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package main
import (
"bingzhilanmo/go-lua/config"
"bingzhilanmo/go-lua/controller"
"bingzhilanmo/go-lua/models"
"bingzhilanmo/go-lua/pkg/utils"
"bingzhilanmo/go-lua/router"
"bingzhilanmo/go-lua/service"
"flag"
"github.com/DeanThompson/ginpprof"
"github.com/gin-gonic/gin"
ginprometheus "github.com/mcuadros/go-gin-prometheus"
log "github.com/sirupsen/logrus"
"os"
"os/signal"
"syscall"
)
var (
ConfigPath string
LogLevel string
LogFile string
file *os.File
)
func init() {
flag.StringVar(&ConfigPath, "config_path", "config.toml", "config path.")
flag.StringVar(&LogLevel, "log_level", "debug", "config path.")
flag.StringVar(&LogFile, "log_file", "go_lua.log", "config path.")
}
func logConfig() {
level,err := log.ParseLevel(LogLevel)
if err != nil {
level = log.DebugLevel
}
log.SetLevel(level)
log.SetFormatter(&log.TextFormatter{TimestampFormat: "2006-01-02 15:04:05"})
log.SetReportCaller(true)
file, err = os.OpenFile(LogFile, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0666)
if err != nil {
panic(err)
}
log.SetOutput(file)
}
func run() {
log.Info("start go-lua state engine .....")
if err := config.LoadGlobalConfig(ConfigPath); err != nil {
log.Panicf("init load config file errr %s", err.Error())
}
if err := models.InitDb() ; err != nil {
log.Panicf("init database error %s", err.Error())
}
if config.GetGlobalConfig().DB.OpenListener {
models.OpenListener()
}
//Init Cache
utils.InitCache()
//process指标采集
utils.DoResourceMonitor()
//init vm cache
if config.GetGlobalConfig().Base.CacheLuaVm {
service.WarmLuaVm()
}
//初始化http引擎
gin.SetMode(gin.ReleaseMode)
r := gin.New()
p := ginprometheus.NewPrometheus("gin")
p.Use(r)
r.Use(gin.Recovery())
r.Use(gin.Logger())
r.Use(controller.RequestIdMiddleware())
//路由注册
router.RegisterRouter(r)
if config.GetGlobalConfig().Base.OpenPprof {
ginpprof.Wrap(r)
}
err := r.Run(config.GetGlobalConfig().Http.Host + ":" + config.GetGlobalConfig().Http.Port)
if err != nil {
log.Panicf("start server error %s", err.Error())
}
log.Infof("success start server ip %s, port %s", config.GetGlobalConfig().Http.Host, config.GetGlobalConfig().Http.Port)
log.Info("complete go-lua state engine .....")
}
// @title Svc engine API
// @version 0.1
// @description Server ft.
// @termsOfService
// @license.name Apache 2.0
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html
// @BasePath /
func main() {
logConfig()
go run()
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
for {
select {
case <- c:
models.CloseDb()
log.Debug("start go-lua state engine exit .....")
file.Close()
return
}
}
}