forked from webx-top/tower
/
main.go
395 lines (378 loc) · 10.3 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
package main
import (
"errors"
"flag"
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/admpub/confl"
"github.com/admpub/log"
c "github.com/webx-top/tower/config"
)
func init() {
log.DefaultLog.Category = `tower`
log.Sync(true)
log.DefaultLog.Formatter = func(_ *log.Logger, e *log.Entry) string {
return e.Message
}
log.SetFatalAction(log.ActionExit)
}
const ConfigName = "tower.yml"
var (
app App
build = "1"
)
func main() {
c.Conf.App.ExecFile = flag.String("f", "tower-app-*.exe", "path to your app's main file.")
c.Conf.App.MainFile = flag.String("m", "", "path to your app's main file.")
c.Conf.App.Port = flag.String("p", "5001-5050", "port range of your app.")
c.Conf.Proxy.Port = flag.String("r", "8080", "proxy port of your app.")
c.Conf.Proxy.Engine = flag.String("e", "standard", "fast/standard")
c.Conf.App.BuildDir = flag.String("o", "", "save the executable file the folder.")
c.Conf.App.PortParamName = flag.String("n", "", "app's port param name.")
c.Conf.App.RunParams = flag.String("s", "", "app's run params.")
c.Conf.Verbose = flag.Bool("v", false, "show more stuff.")
c.Conf.ConfigFile = flag.String("c", ConfigName, "yaml configuration file location.")
c.Conf.Admin.Password = flag.String("w", "", "admin password.")
c.Conf.Admin.IPs = flag.String("i", "127.0.0.1,::1", "admin allow IP.")
c.Conf.AutoClear = flag.Bool("a", true, "automatically deletes previously compiled files when you startup Tower in the compile mode")
c.Conf.LogLevel = flag.String("logLevel", "Debug", "logger level(Debug/Info/Warn/Error/Fatal)")
c.Conf.Offline = flag.Bool("offline", true, "offline mode")
c.Conf.LogRequest = flag.Bool("logRequest", true, "")
c.Conf.Watch.FileExtension = flag.String("fileExtention", "go", "")
c.Conf.Watch.OtherDir = flag.String("watchOtherDir", "", "")
c.Conf.Watch.IgnoredPath = flag.String("watchIgnoredPath", "/\\.git", "")
prod := flag.String("prod", "", "Production mode")
flag.Parse()
args := flag.Args()
if len(args) == 1 && args[0] == "init" {
generateExampleConfig()
return
}
if !fileExist(*c.Conf.ConfigFile) {
generateExampleConfig()
}
if len(*prod) > 0 && atob(*prod) {
build = "0"
}
startTower()
}
func fileExist(path string) bool {
fi, err := os.Stat(path)
return (err == nil || os.IsExist(err)) && !fi.IsDir()
}
func saveFile(filePath string, b []byte) (int, error) {
os.MkdirAll(filepath.Dir(filePath), os.ModePerm)
fw, err := os.Create(filePath)
if err != nil {
return 0, err
}
defer fw.Close()
return fw.Write(b)
}
func generateExampleConfig() {
configContent := defaultConfig
var err error
/*
c.Conf.Fixed()
configContent, err = confl.Marshal(c.Conf)
if err != nil {
log.Fatal(err)
return
}
*/
_, err = saveFile(ConfigName, configContent)
if err != nil {
log.Error(err)
return
}
log.Info("== Generated config file " + ConfigName)
}
func atob(a string) bool {
return a == `1` || a == `true` || a == `on` || a == `yes`
}
func findBinFile(f string) string {
var prefix, suffix string
tg := strings.Split(filepath.Base(f), `*`)
switch len(tg) {
case 2:
prefix = tg[0]
suffix = tg[1]
default:
panic(`error format.`)
}
var file string
err := filepath.Walk(filepath.Dir(f), func(filePath string, info os.FileInfo, e error) (err error) {
if e != nil {
return e
}
if info.IsDir() {
return
}
name := info.Name()
if strings.HasPrefix(name, prefix) && strings.HasSuffix(name, suffix) {
file = filePath
return filepath.SkipDir
}
return
})
if err != nil && err != filepath.SkipDir {
panic(err)
}
return file
}
func checkBinFile(appMainFile string, suffix string, _suffix *string, appBuildDir *string) error {
_, err := os.Stat(appMainFile)
if err != nil {
if len(*c.Conf.App.BuildDir) == 0 {
return errors.New(err.Error() + `: ` + appMainFile)
}
appMainFile = filepath.Join(*c.Conf.App.BuildDir, appMainFile)
_, err = os.Stat(appMainFile)
if err != nil {
return errors.New(err.Error() + `: ` + appMainFile)
}
}
appMainFile, err = filepath.Abs(appMainFile)
if err != nil {
return errors.New(err.Error() + `: ` + appMainFile)
}
if len(*c.Conf.App.BuildDir) == 0 {
*c.Conf.App.BuildDir = filepath.Dir(appMainFile)
}
fileName := filepath.Base(appMainFile)
AppBin = fileName
if strings.HasSuffix(AppBin, suffix) {
AppBin = strings.TrimSuffix(AppBin, suffix)
*_suffix = suffix
}
nameOk := strings.HasPrefix(AppBin, BinPrefix)
if nameOk {
fileName := strings.TrimPrefix(AppBin, BinPrefix)
_, err := strconv.ParseInt(fileName, 10, 64)
if err != nil {
nameOk = false
}
}
if !nameOk {
return fmt.Errorf("exec参数指定的可执行文件名称格式应该为:%v0%v(当前为:%v)。\n其中的“0”是代表版本号的整数,请修改为此格式。", BinPrefix, *_suffix, fileName)
}
return nil
}
func startTower() {
var (
allowBuild = atob(build)
suffix = ".exe"
_suffix = ""
)
if len(*c.Conf.ConfigFile) == 0 {
*c.Conf.ConfigFile = ConfigName
}
configFile := *c.Conf.ConfigFile
_, err := confl.DecodeFile(configFile, c.Conf)
if err != nil {
if strings.HasSuffix(err.Error(), `. Expected map but found 'string'.`) {
err = convertOldConfigFormat(configFile)
if err != nil {
log.Error(err.Error())
} else {
os.Rename(configFile, configFile+`.`+time.Now().Format(`20060102150405`))
c.Conf.Fixed()
configContent, err := confl.Marshal(c.Conf)
if err != nil {
log.Fatal(err)
return
}
_, err = saveFile(configFile, configContent)
if err != nil {
log.Error(err)
return
}
log.Info("== Upgrade config file " + ConfigName)
}
} else {
log.Error(err.Error())
}
} else {
if strings.Contains(*c.Conf.Watch.IgnoredPath, `\\`) {
*c.Conf.Watch.IgnoredPath = strings.Replace(*c.Conf.Watch.IgnoredPath, `\\`, `\`, -1)
}
}
c.Conf.Fixed()
if !allowBuild {
if len(*c.Conf.App.ExecFile) == 0 {
log.Error("请设置exec参数用来指定执行文件位置")
time.Sleep(time.Second * 10)
return
}
}
if *c.Conf.Verbose {
*c.Conf.LogLevel = `Debug`
}
log.DefaultLog.SetLevel(*c.Conf.LogLevel)
if len(*c.Conf.Proxy.Port) > 0 {
err := dialAddress("127.0.0.1:"+*c.Conf.Proxy.Port, 1)
if err == nil {
log.Error("Error: port (" + *c.Conf.Proxy.Port + ") already in used.")
os.Exit(1)
}
}
if !allowBuild {
if strings.Contains(*c.Conf.App.ExecFile, `*`) {
orgiMainFile := *c.Conf.App.ExecFile
*c.Conf.App.ExecFile = findBinFile(*c.Conf.App.ExecFile)
if len(*c.Conf.App.ExecFile) == 0 {
if len(*c.Conf.App.BuildDir) > 0 {
*c.Conf.App.ExecFile = filepath.Join(*c.Conf.App.BuildDir, orgiMainFile)
*c.Conf.App.ExecFile = findBinFile(*c.Conf.App.ExecFile)
}
}
}
if err := checkBinFile(*c.Conf.App.ExecFile, suffix, &_suffix, c.Conf.App.BuildDir); err != nil {
fmt.Println(err)
time.Sleep(time.Second * 300)
return
}
app = NewApp(*c.Conf.App.ExecFile, *c.Conf.App.Port, *c.Conf.App.BuildDir, *c.Conf.App.PortParamName)
} else {
if len(*c.Conf.App.BuildDir) == 0 {
*c.Conf.App.MainFile, _ = filepath.Abs(*c.Conf.App.MainFile)
*c.Conf.App.BuildDir = filepath.Dir(*c.Conf.App.MainFile)
}
if *c.Conf.AutoClear {
err := filepath.Walk(*c.Conf.App.BuildDir, func(filePath string, info os.FileInfo, e error) (err error) {
if e != nil {
return e
}
if info.IsDir() {
return
}
name := info.Name()
if strings.HasPrefix(name, BinPrefix) {
err = os.Remove(filePath)
if err != nil {
return
}
}
return
})
if err != nil {
log.Error(err)
}
}
app = NewApp(*c.Conf.App.MainFile, *c.Conf.App.Port, *c.Conf.App.BuildDir, *c.Conf.App.PortParamName)
}
app.OfflineMode = *c.Conf.Offline
app.DisabledLogRequest = *c.Conf.LogRequest == false
if len(*c.Conf.App.RunParams) > 0 {
app.RunParams = strings.Split(*c.Conf.App.RunParams, ` `)
}
watchedDir := app.Root
if !allowBuild {
if len(app.BuildDir) > 0 {
watchedDir = app.BuildDir
}
}
if len(*c.Conf.Watch.OtherDir) > 0 {
watchedDir = *c.Conf.Watch.OtherDir + "|" + watchedDir
}
watcher := NewWatcher(watchedDir, *c.Conf.Watch.FileExtension, *c.Conf.Watch.IgnoredPath)
proxy := NewProxy(&app, &watcher)
proxy.AdminPwd = *c.Conf.Admin.Password
proxy.Engine = *c.Conf.Proxy.Engine
if len(*c.Conf.Admin.IPs) > 0 {
proxy.AdminIPs = strings.Split(*c.Conf.Admin.IPs, `,`)
}
if allowBuild {
watcher.OnChanged = func(file string) {
watcher.Reset()
fileName := filepath.Base(file)
if strings.HasPrefix(fileName, BinPrefix) {
log.Info(`忽略`, fileName, `更改`)
return
}
port, err := getPort()
if err != nil {
log.Error(err)
return
}
err = app.Start(true, port)
if err != nil {
log.Error(err)
}
}
} else {
watcher.OnChanged = func(file string) {
watcher.Reset()
port, err := getPort()
if err != nil {
log.Error(err)
return
}
log.Debug(`== Switch port to `, port)
fileName := filepath.Base(file)
if !strings.HasPrefix(fileName, BinPrefix) {
log.Info(`忽略非`, BinPrefix, `前缀文件更改`)
return
}
if len(_suffix) > 0 {
fileName = strings.TrimSuffix(fileName, _suffix)
}
newAppBin := fileName
fileName = strings.TrimPrefix(fileName, BinPrefix)
newFileTs, err := strconv.ParseInt(fileName, 10, 64)
if err != nil {
log.Error(err)
return
}
fileName = strings.TrimPrefix(AppBin, BinPrefix)
oldFileTs, err := strconv.ParseInt(fileName, 10, 64)
if err != nil {
log.Error(err)
return
}
if newFileTs <= oldFileTs {
log.Info(`新文件时间戳小于旧文件,忽略`)
return
}
AppBin = newAppBin
err = app.Start(true, port)
if err != nil {
log.Error(err)
}
}
watcher.OnlyWatchBin = true
app.DisabledBuild = true
}
proxy.Port = *c.Conf.Proxy.Port
go func() {
mustSuccess(watcher.Watch())
}()
err = app.Start(true, app.Port)
if err != nil {
log.Error(err)
}
mustSuccess(proxy.Listen())
}
func getPort() (port string, err error) {
port = app.Port
if !app.DisabledVisitPort() {
if !app.SupportMutiPort() {
err = errors.New(`Unspecified switchable other ports.`)
return
}
port = app.UseRandPort()
for i := 0; i < 3 && port == app.Port; i++ {
app.Clean()
time.Sleep(time.Second)
port = app.UseRandPort()
}
if port == app.Port {
err = errors.New(`取得的端口与当前端口相同,无法编译切换`)
}
}
return
}