-
Notifications
You must be signed in to change notification settings - Fork 317
/
main.go
189 lines (162 loc) · 5.46 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"runtime"
"strings"
_ "github.com/TangSengDaoDao/TangSengDaoDaoServer/internal"
"github.com/TangSengDaoDao/TangSengDaoDaoServer/modules/base/event"
"github.com/TangSengDaoDao/TangSengDaoDaoServerLib/config"
"github.com/TangSengDaoDao/TangSengDaoDaoServerLib/module"
"github.com/TangSengDaoDao/TangSengDaoDaoServerLib/pkg/log"
"github.com/TangSengDaoDao/TangSengDaoDaoServerLib/server"
"github.com/gin-gonic/gin"
"github.com/judwhite/go-svc"
"github.com/robfig/cron"
"github.com/spf13/viper"
)
// go ldflags
var Version string // version
var Commit string // git commit id
var CommitDate string // git commit date
var TreeState string // git tree state
func loadConfigFromFile(cfgFile string) *viper.Viper {
vp := viper.New()
vp.SetConfigFile(cfgFile)
if err := vp.ReadInConfig(); err == nil {
fmt.Println("Using config file:", vp.ConfigFileUsed())
}
return vp
}
func main() {
var CfgFile string //config file
flag.StringVar(&CfgFile, "config", "configs/tsdd.yaml", "config file")
flag.Parse()
vp := loadConfigFromFile(CfgFile)
vp.SetEnvPrefix("ts")
vp.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
vp.AutomaticEnv()
gin.SetMode(gin.ReleaseMode)
cfg := config.New()
cfg.Version = Version
cfg.ConfigureWithViper(vp)
// 初始化context
ctx := config.NewContext(cfg)
ctx.Event = event.New(ctx)
logOpts := log.NewOptions()
logOpts.Level = cfg.Logger.Level
logOpts.LineNum = cfg.Logger.LineNum
logOpts.LogDir = cfg.Logger.Dir
log.Configure(logOpts)
var serverType string
if len(os.Args) > 1 {
serverType = strings.TrimSpace(os.Args[1])
serverType = strings.Replace(serverType, "-", "", -1)
}
if serverType == "api" || serverType == "" || serverType == "config" { // api服务启动
runAPI(ctx)
}
}
func runAPI(ctx *config.Context) {
// 创建server
s := server.New(ctx)
ctx.SetHttpRoute(s.GetRoute())
// 替换web下的配置文件
replaceWebConfig(ctx.GetConfig())
// 初始化api
s.GetRoute().UseGin(ctx.Tracer().GinMiddle()) // 需要放在 api.Route(s.GetRoute())的前面
s.GetRoute().UseGin(func(c *gin.Context) {
ingorePaths := ingorePaths()
for _, ingorePath := range ingorePaths {
if ingorePath == c.FullPath() {
return
}
}
gin.Logger()(c)
})
// 模块安装
err := module.Setup(ctx)
if err != nil {
panic(err)
}
//开始定时处理事件
cn := cron.New()
//定时发布事件 每59秒执行一次
err = cn.AddFunc("0/59 * * * * ?", func() {
ctx.Event.(*event.Event).EventTimerPush()
})
if err != nil {
panic(err)
}
cn.Start()
// 打印服务器信息
printServerInfo(ctx)
// 运行
err = svc.Run(s)
if err != nil {
panic(err)
}
}
func printServerInfo(ctx *config.Context) {
infoStr := `
[?25l[?7lLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL
LLLLLLLLLLLL0CLLLLLLLLLLLLLLLLLLLLLLLLLL
LLLLLLLLLL08@880CfLLLLLLLLLLLLLLLLLLLLLL
LLLLLLLLfL8@8@@8LfLLLLLLLLLLLLLLLLLLLLLL
ffffffffft0@@8@8ffffffffffffffffffffffff
fffffffffCCL8@GLLfLLLfffffffffffffffffff
ffffffffCLLC0@GCCLLLLCffffffffffffffffff
ffffffffG0@@@@@@@8Ltffffffffffffffffffff
ffffffftC888888888Gtffffffffffffffffffff
ffffffftttttttttttttffffffffffffffffffff
fffffffttttttttttttfffffffffffffffffffff
tttttttttttttfftffttttttttttttttttfttttt
tttttttttttttttttttttttttttttttttttttttt
tttttttttttttttttttttttttttttttttttttttt
tttttttttttttttttttttttttttttttttttttttt
tttttttttttttttttttttttttttttttttttttttt
tttttttttttttttttttttttttttttttttttttttt
111t111111111tt1111111tt1111111t11111111[0m
[20A[9999999D[43C[0m[0m
[43C[0m[1m[32mTangSengDaoDao is running[0m
[43C[0m-------------------------[0m
[43C[0m[1m[33mMode[0m[0m:[0m #mode#[0m
[43C[0m[1m[33mConfig[0m[0m:[0m #configPath#[0m
[43C[0m[1m[33mApp name[0m[0m:[0m #appname#[0m
[43C[0m[1m[33mVersion[0m[0m:[0m #version#[0m
[43C[0m[1m[33mGit[0m[0m:[0m #git#[0m
[43C[0m[1m[33mGo build[0m[0m:[0m #gobuild#[0m
[43C[0m[1m[33mIM URL[0m[0m:[0m #imurl#[0m
[43C[0m[1m[33mFile Service[0m[0m:[0m #fileService#[0m
[43C[0m[1m[33mThe API is listening at[0m[0m:[0m #apiAddr#[0m
[43C[30m[40m [31m[41m [32m[42m [33m[43m [34m[44m [35m[45m [36m[46m [37m[47m [m
[43C[38;5;8m[48;5;8m [38;5;9m[48;5;9m [38;5;10m[48;5;10m [38;5;11m[48;5;11m [38;5;12m[48;5;12m [38;5;13m[48;5;13m [38;5;14m[48;5;14m [38;5;15m[48;5;15m [m
[?25h[?7h
`
cfg := ctx.GetConfig()
infoStr = strings.Replace(infoStr, "#mode#", string(cfg.Mode), -1)
infoStr = strings.Replace(infoStr, "#appname#", cfg.AppName, -1)
infoStr = strings.Replace(infoStr, "#version#", cfg.Version, -1)
infoStr = strings.Replace(infoStr, "#git#", fmt.Sprintf("%s-%s", CommitDate, Commit), -1)
infoStr = strings.Replace(infoStr, "#gobuild#", runtime.Version(), -1)
infoStr = strings.Replace(infoStr, "#fileService#", cfg.FileService.String(), -1)
infoStr = strings.Replace(infoStr, "#imurl#", cfg.WuKongIM.APIURL, -1)
infoStr = strings.Replace(infoStr, "#apiAddr#", cfg.Addr, -1)
infoStr = strings.Replace(infoStr, "#configPath#", cfg.ConfigFileUsed(), -1)
fmt.Println(infoStr)
}
func ingorePaths() []string {
return []string{
"/v1/robots/:robot_id/:app_key/events",
"/v1/ping",
}
}
func replaceWebConfig(cfg *config.Config) {
path := "./assets/web/js/config.js"
newConfigContent := fmt.Sprintf(`const apiURL = "%s/"`, cfg.External.APIBaseURL)
ioutil.WriteFile(path, []byte(newConfigContent), 0)
}