This repository has been archived by the owner on Oct 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.go
103 lines (81 loc) · 2.36 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
package main
import (
"flag"
"fmt"
"io"
"os"
"strings"
"time"
"github.com/eric2788/biligo-live-ws/controller/listening"
"github.com/eric2788/biligo-live-ws/controller/subscribe"
ws "github.com/eric2788/biligo-live-ws/controller/websocket"
"github.com/eric2788/biligo-live-ws/middleware"
"github.com/eric2788/biligo-live-ws/services/database"
"github.com/eric2788/biligo-live-ws/services/updater"
"github.com/gin-contrib/pprof"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
)
var release = flag.Bool("release", os.Getenv("GIN_MODE") == "release", "set release mode")
var port = flag.Int("port", 8080, "set the websocket port")
var version string
func main() {
flag.Parse()
updater.VersionTag = version
log.Infof("biligo-live-ws %v", version)
if *release {
gin.SetMode(gin.ReleaseMode)
log.SetLevel(log.InfoLevel)
} else {
log.SetLevel(log.DebugLevel)
log.Debug("啟動debug模式")
}
log.Info("正在初始化數據庫...")
if err := database.StartDB(); err != nil {
log.Fatalf("初始化數據庫時出現嚴重錯誤: %v", err)
} else {
log.Info("數據庫已成功初始化。")
}
if os.Getenv("ENABLE_LOG_FILE") == "true" {
ts := time.Now().Format("2006-01-02 15:04:05")
// create log with unix timestamp
logFile, err := os.Create("./cache/networkLog/" + ts + ".log")
if err != nil {
log.Fatalf("创建 /cache/networkLog/%s.log 文件时错误: %v", ts, err)
}
mw := io.MultiWriter(os.Stdout, logFile)
log.SetOutput(mw)
gin.DefaultWriter = mw
}
router := gin.New()
router.Use(func(c *gin.Context) {
if os.Getenv("NO_LISTENING_LOG") == "true" && strings.HasPrefix(c.Request.URL.Path, "/listening") {
c.Next()
return
}
gin.Logger()(c)
})
router.Use(middleware.CORS())
router.Use(middleware.ErrorHandler())
router.Use(middleware.Identifier())
router.GET("", Index)
router.POST("validate", ValidateProcess)
subscribe.Register(router.Group("subscribe"))
ws.Register(router.Group("ws"))
listening.Register(router.Group("listening"))
pprof.Register(router)
port := fmt.Sprintf(":%d", *port)
log.Infof("使用端口 %s\n", port)
go updater.StartUpdater()
if err := router.Run(port); err != nil {
log.Fatal(err)
}
if err := database.CloseDB(); err != nil {
log.Errorf("關閉數據庫時錯誤: %v", err)
}
}
func Index(c *gin.Context) {
c.IndentedJSON(200, gin.H{
"status": "working",
})
}