-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
116 lines (98 loc) · 2.54 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
/*
* _____ _ __ _____ _____ _ _____ _____
* / _ \ | | / / / _ \ | _ \ | | / _ \ / ___|
* | | | | | | __ / / | | | | | |_| | | | | | | | | |
* | | | | | | / | / / | | | | | _ { | | | | | | | | _
* | |_| | | |/ |/ / | |_| | | |_| | | |___ | |_| | | |_| |
* \_____/ |___/|___/ \_____/ |_____/ |_____| \_____/ \_____/
*
* Copyright (c) 2023 by OwOTeam-DGMT (OwOBlog).
* @Date : 2024-06-06 02:24:56
* @Author : HanskiJay
* @LastEditors : HanskiJay
* @LastEditTime : 2024-07-19 14:48:04
* @E-Mail : support@owoblog.com
* @Telegram : https://t.me/HanskiJay
* @GitHub : https://github.com/Tommy131
*/
package main
import (
"bufio"
"os"
"os/signal"
"owoweb/a"
"owoweb/cmd"
"owoweb/i18n"
"owoweb/routes"
"owoweb/utils"
"strings"
"syscall"
"github.com/gin-gonic/gin"
"github.com/logrusorgru/aurora"
log "github.com/sirupsen/logrus"
)
// 主函数
func main() {
// 激活头部包
a.DoNothing()
if !utils.DEBUG_MODE {
gin.SetMode(gin.ReleaseMode)
}
config, err := utils.LoadConfig()
if err != nil {
log.Fatalf("Failed to load config: %v", err)
}
if len(os.Args) > 1 {
cmd.Execute()
return
}
go runWebServer(config.WebListeningAddress)
log.Info(i18n.Lpk.FormatMessage("main.web_service_listening", aurora.BrightYellow(utils.CreateClickableLink("http://"+utils.WEB_ADDRESS))))
done := make(chan bool, 1)
go scanCommands(done)
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
select {
case <-sigs:
log.Info(i18n.Lpk.GetTranslate("main.forcibly_stopped_terminal"))
case <-done:
log.Info(i18n.Lpk.GetTranslate("main.stopping_terminal"))
}
}
// runWebServer 启动Web服务
func runWebServer(address string) {
router := gin.Default()
router.Static("/static", utils.STORAGE_PATH+"static")
router.LoadHTMLGlob(utils.STORAGE_PATH + "static/*.html")
// 注册自定义路由
routes.RegisterRouters(router)
// 启动Web服务
router.Run(address)
}
// scanCommands 扫描并执行指令
func scanCommands(done chan bool) {
scanner := bufio.NewScanner(os.Stdin)
for {
if !scanner.Scan() {
break
}
command := strings.TrimSpace(scanner.Text())
if command == "" {
return
}
switch command {
case "stop", "shutdown":
done <- true
return
default:
args := strings.Split(command, " ")
if len(args) == 0 {
return
}
cmd.RootCmd.SetArgs(args)
if err := cmd.RootCmd.Execute(); err != nil {
log.Error("Error:", err)
}
}
}
}