-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
94 lines (85 loc) · 2.01 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
package main
import (
"embed"
"fmt"
"html/template"
"io/fs"
"log"
"net/http"
"os"
"quest/global"
"quest/router"
"quest/service"
"github.com/gin-gonic/gin"
_ "modernc.org/sqlite"
"xorm.io/xorm"
)
//go:embed page/error/* page/dist/*
var f embed.FS
func main() {
fmt.Print(`┌────────────────────────────┐
│ ____ __ │
│ / __ \__ _____ ___ / /_ │
│ / /_/ / // / -_|_-</ __/ │
│ \___\_\_,_/\__/___/\__/ │
│ │
│ github.com/skye-z/quest │
└────────────────────────────┘
Version: ` + global.Version + "\n\n")
log.Println("[Core] release model")
// 关闭调试
gin.SetMode(gin.ReleaseMode)
// 初始化系统配置
global.InitConfig()
// 初始化数据库
engine := loadDBEngine()
go service.InitDatabase(engine)
log.Println("[Core] service starting")
// 注册路由
r := register(engine)
// 获取端口号
port := getPort()
log.Println("[Core] service started, port is", port)
r.Run(":" + port)
engine.Close()
}
func loadDBEngine() *xorm.Engine {
engine, err := xorm.NewEngine("sqlite", "./local.store")
if err != nil {
panic(err)
}
return engine
}
func register(engine *xorm.Engine) *gin.Engine {
r := gin.Default()
// 加载静态资源
fp, _ := fs.Sub(f, "page/dist")
r.StaticFS("/app", http.FS(fp))
// 加载模版页面
templ := template.Must(template.New("").ParseFS(f, "page/error/*.html"))
r.SetHTMLTemplate(templ)
// 配置404
r.NoRoute(func(ctx *gin.Context) {
data, err := f.ReadFile("page/dist/index.html")
if err != nil {
ctx.HTML(http.StatusOK, "404.html", gin.H{
"title": "404",
})
return
}
ctx.Data(http.StatusOK, "text/html; charset=utf-8", data)
})
// 注册路由
router.RegisterRoute(r, engine)
return r
}
func getPort() string {
port := os.Getenv("PORT")
if port == "" {
port = global.GetString("basic.port")
}
if port == "" {
port = "80"
}
return port
}