-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathmain.go
96 lines (82 loc) · 2.14 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
package main
import (
"choccy/server/database"
"choccy/server/database/model"
"choccy/server/middleware"
"choccy/server/router"
"choccy/server/taskmanager"
"choccy/server/util"
"embed"
"flag"
"github.com/gin-gonic/gin"
"log"
"path/filepath"
"strings"
)
//go:embed assets/*
var assets embed.FS
//go:embed index.html favicon.ico
var index embed.FS
//go:embed examples/*
var examples embed.FS
func run() {
//获取命令行参数,只有addr和token
addr := flag.String("addr", "0.0.0.0:80", "监听地址和端口")
token := flag.String("token", "", "系统Token")
flag.Parse()
//初始化数据目录,数据库,环境变量
{
storageDir := "choccy_data"
util.InitStorageDir(storageDir)
// isNew,只在不存在choccy.db时为true,也就是程序第一次启动的时候
isNew := database.InitDB(filepath.Join(util.GetStorageDir(), "choccy.db"))
if isNew { // 程序第一次启动进行各项初始化
if strings.TrimSpace(*token) == "" { //如果用户不指定token,则随机生成
*token = util.RandomString(16)
}
util.MakeDataDir(util.GetStorageDir())
util.InitExample(util.GetStorageDir(), &examples)
}
util.InitSetting(storageDir)
util.InitStatus()
util.InitEnv()
}
//设置系统Token
{
if strings.TrimSpace(*token) != "" {
database.DB.Model(model.Setting{}).
Select("SystemToken").Where(true).Updates(model.Setting{SystemToken: *token})
}
}
//初始化任务执行器,恢复未执行的任务
{
taskmanager.InitTask()
err := taskmanager.SetCronTask()
if err != nil {
log.Println("Error: " + err.Error())
}
go taskmanager.Consumer()
}
//注册路由
{
gin.SetMode(gin.ReleaseMode)
r := gin.Default()
r.MaxMultipartMemory = 1024 << 20 //1G
r.Use(middleware.ErrorMiddleware)
r.Use(middleware.GetCorsMiddleware())
router.SetupFrontendRoutes(r, &assets, &index)
router.SetupApiRoutes(r)
var setting model.Setting
result := database.DB.Take(&setting)
if result.Error != nil {
panic(result.Error.Error())
}
log.Println("Token : " + setting.SystemToken)
log.Println("Listen: " + *addr)
err := r.Run(*addr)
log.Fatalln(err)
}
}
func main() {
run()
}