-
Notifications
You must be signed in to change notification settings - Fork 17
/
main.go
164 lines (150 loc) · 3.63 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
/*
* @Author: SpenserCai
* @Date: 2023-08-15 21:55:36
* @version:
* @LastEditors: SpenserCai
* @LastEditTime: 2023-10-08 17:00:50
* @Description: file content
*/
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"net/http/httputil"
"net/url"
"os"
"os/exec"
"path/filepath"
"runtime"
"github.com/SpenserCai/sd-webui-discord/api"
"github.com/SpenserCai/sd-webui-discord/cluster"
"github.com/SpenserCai/sd-webui-discord/dbot"
"github.com/SpenserCai/sd-webui-discord/global"
"github.com/SpenserCai/sd-webui-discord/queue"
"github.com/SpenserCai/sd-webui-discord/user"
)
func LoadConfig() error {
exePath, err := os.Executable()
if err != nil {
return err
}
exeDir := filepath.Dir(exePath)
configPath := filepath.Join(exeDir, "config.json")
file, err := os.Open(configPath)
if err != nil {
return err
}
defer file.Close()
err = json.NewDecoder(file).Decode(&global.Config)
if err != nil {
return err
}
return nil
}
func InitClusterManager() {
global.ClusterManager = cluster.NewClusterService(global.Config)
global.ClusterManager.Start()
}
func InitUserCenterService() error {
var err error
global.UserCenterSvc, err = user.NewUserCenterService(&global.Config.UserCenter)
return err
}
func PrintEvent() {
for {
event := global.ClusterManager.GetEvent()
eventName := "Event"
switch event.EventType {
case queue.EventPendding:
eventName = "Pendding"
case queue.EventRunning:
eventName = "Running"
case queue.EventSuccess:
eventName = "Success"
case queue.EventFaile:
eventName = "Failed"
case queue.EventCancel:
eventName = "Cancel"
default:
eventName = "Unknown"
}
log.Printf("[Event]: ID:%v Type:%v", event.ID, eventName)
}
}
func RunWebSite() error {
exePath, err := os.Executable()
if err != nil {
return err
}
exeDir := filepath.Dir(exePath)
websiteDir := filepath.Join(exeDir, "website")
fs := http.FileServer(http.Dir(websiteDir))
http.Handle("/", fs)
log.Println("website dir:", websiteDir)
apiURL, err := url.Parse(fmt.Sprintf("http://%s:%d", global.Config.WebSite.Api.Host, global.Config.WebSite.Api.Port))
if err != nil {
return err
}
http.Handle("/api/", httputil.NewSingleHostReverseProxy(apiURL))
log.Println("api url:", apiURL)
// 启动Web服务器
addr := fmt.Sprintf("%s:%d", global.Config.WebSite.Web.Host, global.Config.WebSite.Web.Port) // 替换为实际的端口号
log.Printf("WebSite started on port %s\n", addr)
err = http.ListenAndServe(addr, nil)
if err != nil {
return err
}
return nil
}
func OpenWebSite(url string) error {
var cmd *exec.Cmd
switch runtime.GOOS {
case "darwin": // macOS
cmd = exec.Command("open", url)
case "windows": // Windows
cmd = exec.Command("cmd", "/c", "start", url)
default: // Linux 或其他 Unix 系统
cmd = exec.Command("xdg-open", url)
}
err := cmd.Start()
if err != nil {
return err
}
return nil
}
func main() {
err := LoadConfig()
if err != nil {
log.Println(err)
return
}
if global.Config.UserCenter.Enable {
err := InitUserCenterService()
if err != nil {
log.Println(err)
return
}
log.Println("UserCenterService Init Success")
}
InitClusterManager()
go PrintEvent()
disBot, err := dbot.NewDiscordBot(global.Config.Discord.Token, global.Config.Discord.ServerId)
if err != nil {
log.Println(err)
return
}
go api.StartApiService()
if global.Config.WebSite.Web.StartWithServer {
go RunWebSite()
if global.Config.WebSite.Web.OpenBrowser {
// 使用默认浏览器打开网页
err = OpenWebSite(fmt.Sprintf("http://%s:%d", global.Config.WebSite.Web.Host, global.Config.WebSite.Web.Port))
if err != nil {
log.Println(err)
}
}
}
disBot.Run()
}