This repository has been archived by the owner on Feb 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
177 lines (156 loc) · 4.85 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
165
166
167
168
169
170
171
172
173
174
175
176
177
package main
import (
"encoding/json"
"fmt"
"image/color"
"os"
"fyne.io/fyne/v2/dialog"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/driver/desktop"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/layout"
"fyne.io/fyne/v2/widget"
"github.com/MCSManager/Launcher/cmd"
"github.com/MCSManager/Launcher/uiw"
"github.com/MCSManager/Launcher/utils"
)
type WebConfig struct {
HttpPort int `json:"httpPort"`
}
func main() {
var webConfig WebConfig
STOPPED_TEXT := "状态: 未运行"
STARTED_TEXT := "状态: 正在运行"
if utils.IsFileExists("C:/Windows/Fonts/msyh.ttc") {
os.Setenv("FYNE_FONT", "C:/Windows/Fonts/msyh.ttc")
} else {
os.Setenv("FYNE_FONT", "./config/msyh.ttc")
}
app := app.New()
app.Settings().SetTheme(theme.LightTheme())
window := app.NewWindow("MCSManager Launcher")
window.Resize(fyne.Size{Width: 320, Height: 260})
window.SetFullScreen(false)
window.SetFixedSize(true)
infoLabel := uiw.NewMyLabel("MCSManager 管理面板启动器")
infoLabel.SetFontSize(12)
statusLabel := uiw.NewMyLabel(STOPPED_TEXT)
statusLabel.SetFontSize(12)
tipLabel := uiw.NewMyLabel("端口: 23333")
tipLabel.SetFontSize(12)
tipLabelWrapper := container.New(layout.NewHBoxLayout(), tipLabel.Canvas)
operationButton := widget.NewButton("启动后台程序", nil)
btnWrapper := container.New(
layout.NewMaxLayout(),
operationButton,
)
WEB_CONFIG_FILE_PATH := "./mcsmanager/web/data/SystemConfig/config.json"
if utils.IsFileExists(WEB_CONFIG_FILE_PATH) {
content, err := os.ReadFile(WEB_CONFIG_FILE_PATH)
if err != nil {
tipLabel.SetText("文件错误:请放置到正确位置")
} else {
fmt.Printf("Read config: %s\n", string(content))
json.Unmarshal(content, &webConfig)
tipLabel.SetText(fmt.Sprintf("端口: %d", webConfig.HttpPort))
}
}
openBrowser := widget.NewButton("访问面板", func() {
var port = 23333
if webConfig.HttpPort > 0 {
port = webConfig.HttpPort
}
if err := utils.Open(fmt.Sprintf("http://localhost:%d/", port)); err != nil {
fmt.Printf("Open Browser err %v\n", err)
}
})
//守护进程管理
pwd, _ := os.Getwd()
fmt.Println("CWD:", pwd)
// 程序所在目录
daemon := cmd.NewProcessMgr(pwd+"/mcsmanager/daemon/", "./node_app.exe", "app.js")
web := cmd.NewProcessMgr(pwd+"/mcsmanager/web/", "./node_app.exe", "app.js")
//监听程序运行状态
daemon.ListenStop(func(err error) {
fmt.Println("EVENT: daemon exit!")
if web.Started {
web.End()
}
operationButton.Enable()
operationButton.SetText("启动后台程序")
statusLabel.SetText(STOPPED_TEXT)
statusLabel.SetColor(color.Black)
})
web.ListenStop(func(err error) {
fmt.Println("EVENT: web exit!")
if daemon.Started {
daemon.End()
}
})
// 启动/关闭按钮点击事件
operationButton.OnTapped = func() {
var err error
if !daemon.Started {
if err = daemon.Start(); err != nil {
utils.WriteErrLog(fmt.Sprintf("Start daemon error:%s", err.Error()))
return
}
if err = web.Start(); err != nil {
utils.WriteErrLog(fmt.Sprintf("Start web error:%s", err.Error()))
daemon.End()
return
}
statusLabel.SetText(STARTED_TEXT)
statusLabel.SetColor(utils.GREEN)
operationButton.SetText("停止后台程序")
} else {
operationButton.SetText("停止中...")
operationButton.Disable()
if err = web.End(); err != nil {
utils.WriteErrLog(fmt.Sprintf("Stop daemon error:%s", err.Error()))
return
}
}
}
stopButton := widget.NewButton("关闭程序", func() {
dialog.ShowConfirm("警告", "确定要退出程序吗?", func(b bool) {
if b {
if daemon.Started {
dialog.ShowInformation("错误", "您必须关闭后台程序才能关闭本窗口", window)
return
}
os.Exit(0)
}
}, window)
})
if desk, ok := app.(desktop.App); ok {
m := fyne.NewMenu("MCSManager",
fyne.NewMenuItem("显示", func() {
window.Show()
}))
fyne.NewMenuItem("关闭", func() {
window.Show()
dialog.ShowConfirm("警告", "确定要退出程序吗?", func(b bool) {
if b {
if daemon.Started {
dialog.ShowInformation("错误", "您必须关闭后台程序才能关闭本窗口", window)
return
}
os.Exit(0)
}
}, window)
})
desk.SetSystemTrayMenu(m)
}
window.SetCloseIntercept(func() {
window.Hide()
})
paddingContainer1 := container.New(layout.NewPaddedLayout(), infoLabel.Canvas)
paddingContainer2 := container.New(layout.NewPaddedLayout(), container.New(layout.NewVBoxLayout(), statusLabel.Canvas, tipLabelWrapper))
paddingContainer3 := container.New(layout.NewPaddedLayout(), container.New(layout.NewVBoxLayout(), btnWrapper, openBrowser, stopButton))
content := container.New(layout.NewVBoxLayout(), paddingContainer1, layout.NewSpacer(), paddingContainer2, paddingContainer3)
window.SetContent(container.New(layout.NewPaddedLayout(), content))
window.ShowAndRun()
}