Skip to content

Commit f397130

Browse files
committed
chore: minor update
1 parent a388289 commit f397130

File tree

3 files changed

+42
-70
lines changed

3 files changed

+42
-70
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ __pycache__
66
target
77
service/uni-token-service
88
service/uni-token-service.exe
9+
service/data.db
910
frontend/public/release
1011
.eslintcache
1112
docs/.vitepress/cache

service/main.go

Lines changed: 23 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -95,64 +95,42 @@ func main() {
9595
}
9696

9797
if len(os.Args) < 2 {
98-
// Directly run the executable
9998
handleSetup()
10099
logic.OpenUI(url.Values{}, false)
101100
return
102101
}
103102

104103
command := os.Args[1]
105-
106-
if command == "run" || command == "debug" {
107-
err := s.Run()
108-
if err != nil {
109-
panic(err)
110-
}
111-
return
112-
}
113-
114-
if command == "version" {
115-
fmt.Println(constants.Version)
116-
return
117-
}
118-
119-
if command == "url" {
120-
handleUrlScheme(os.Args[2])
121-
return
122-
}
123-
124-
if command == "setup" {
125-
handleSetup()
126-
return
127-
}
128-
129-
if command == "install-and-start" {
130-
handleInstallAndStart(&s, serviceName)
131-
return
132-
}
133-
134-
if command == "uninstall" {
135-
handleSudo(false, []string{"uninstall-impl"})
104+
commandHandlers := map[string]func(){
105+
"run": func() { mustRun(s.Run()) },
106+
"debug": func() { mustRun(s.Run()) },
107+
"version": func() { fmt.Println(constants.Version) },
108+
"url": func() { handleUrlScheme(os.Args[2]) },
109+
"setup": handleSetup,
110+
"install-and-start": func() { handleInstallAndStart(&s, serviceName) },
111+
"uninstall": func() { handleSudo(false, []string{"uninstall-impl"}) },
112+
"uninstall-impl": func() { handleUninstall(s, serviceName) },
113+
"sudo": func() { handleSudoCommand() },
114+
}
115+
116+
if handler, exists := commandHandlers[command]; exists {
117+
handler()
136118
return
137119
}
138120

139-
if command == "uninstall-impl" {
140-
handleUninstall(s, serviceName)
141-
return
142-
}
121+
prg.logger.Infof("Executing command: %s\n", command)
122+
mustRun(service.Control(s, command))
123+
}
143124

144-
if command == "sudo" {
145-
if len(os.Args) < 3 {
146-
fmt.Println("Usage: sudo <command>")
147-
return
148-
}
149-
handleSudo(false, os.Args[2:])
125+
func handleSudoCommand() {
126+
if len(os.Args) < 3 {
127+
fmt.Println("Usage: sudo <command>")
150128
return
151129
}
130+
handleSudo(false, os.Args[2:])
131+
}
152132

153-
prg.logger.Infof("Executing command: %s\n", command)
154-
155-
err = service.Control(s, command)
133+
func mustRun(err error) {
156134
if err != nil {
157135
panic(err)
158136
}
@@ -222,10 +200,6 @@ func handleSudo(useInstalled bool, args []string) {
222200
}
223201

224202
func handleUrlScheme(url string) {
225-
if !strings.HasPrefix(url, "uni-token://") {
226-
fmt.Println("Invalid URL scheme. Expected 'uni-token://'.")
227-
return
228-
}
229203
url = strings.TrimPrefix(url, "uni-token://")
230204

231205
switch url {

service/server/server.go

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,16 @@ func SetupAPIServer() (int, error) {
3030
MaxAge: 240 * time.Hour,
3131
}))
3232

33-
// Setup routes
33+
setupRoutes(router)
34+
35+
port := findAvailablePort()
36+
logic.ServerPort = port
37+
38+
go router.Run(":" + strconv.Itoa(port))
39+
return port, nil
40+
}
41+
42+
func setupRoutes(router *gin.Engine) {
3443
SetupActionAPI(router)
3544
SetupGatewayAPI(router)
3645
SetupAppAPI(router)
@@ -39,33 +48,21 @@ func SetupAPIServer() (int, error) {
3948
SetupUsageAPI(router)
4049
SetupSiliconFlowAPI(router)
4150
SetupAuthAPI(router)
42-
43-
// Get a random available port
44-
port := getPort()
45-
logic.ServerPort = port
46-
47-
// Start server in a goroutine
48-
go func() {
49-
router.Run(":" + strconv.Itoa(port))
50-
}()
51-
52-
return port, nil
5351
}
5452

55-
func checkPortAvailability(port int) bool {
53+
func isPortAvailable(port int) bool {
5654
_, err := net.Dial("tcp", "localhost:"+strconv.Itoa(port))
5755
return err != nil
5856
}
5957

60-
const PORT_RANGE_START = 18760
58+
const portRangeStart = 18760
6159

62-
func getPort() int {
63-
for i := PORT_RANGE_START; i < PORT_RANGE_START+10; i++ {
64-
if checkPortAvailability(i) {
65-
return i
66-
} else {
67-
log.Printf("Port %d is not available", i)
60+
func findAvailablePort() int {
61+
for port := portRangeStart; port < portRangeStart+10; port++ {
62+
if isPortAvailable(port) {
63+
return port
6864
}
65+
log.Printf("Port %d is not available", port)
6966
}
70-
panic(fmt.Sprintf("No available port found in the range %d-%d", PORT_RANGE_START, PORT_RANGE_START+10))
67+
panic(fmt.Sprintf("No available port found in range %d-%d", portRangeStart, portRangeStart+10))
7168
}

0 commit comments

Comments
 (0)