feat: implement fixed local API port settings for desktop app, allowing users to specify a port for consistent access, with corresponding UI and translations

This commit is contained in:
wizardchen
2026-04-11 16:02:52 +08:00
committed by lyingbug
parent 3f0f69815f
commit d99e2cbac3
13 changed files with 392 additions and 279 deletions
+14 -6
View File
@@ -214,18 +214,15 @@ func main() {
) error {
server := &http.Server{Handler: router}
// Use port 0 to assign a random free port for the desktop app
// Force localhost binding to prevent firewall popups on macOS
addr := "127.0.0.1:0"
// 127.0.0.1 + saved port from settings (desktop-prefs.json), or :0 for random free port.
addr := desktopBackendListenAddr()
listener, err := listenWithRetry(addr, 10, 300*time.Millisecond)
if err != nil {
return fmt.Errorf("failed to start server: %v", err)
}
// Get the actual assigned port
actualPort := listener.Addr().(*net.TCPAddr).Port
actualAddr := fmt.Sprintf("127.0.0.1:%d", actualPort)
actualAddr := listener.Addr().(*net.TCPAddr).String()
// Store the backend URL so Wails can load it
app.backendURL = fmt.Sprintf("http://%s", actualAddr)
@@ -413,6 +410,17 @@ func resolveDesktopDataPath(rawPath, defaultRelativePath, appSupportDir string)
return filepath.Join(appSupportDir, filepath.Clean(trimmed))
}
// desktopBackendListenAddr returns the TCP address for the embedded Gin server (Wails desktop).
// Always binds loopback 127.0.0.1. Uses saved UI preference (desktop-prefs.json) when set, else ephemeral port :0.
func desktopBackendListenAddr() string {
const host = "127.0.0.1"
pref := LoadDesktopPrefsHTTPPort()
if pref >= 1 && pref <= 65535 {
return net.JoinHostPort(host, strconv.Itoa(pref))
}
return net.JoinHostPort(host, "0")
}
func migrateLegacyDesktopData(resourcesDir, targetDataDir string) {
legacyDataDir := filepath.Join(resourcesDir, "data")
if info, err := os.Stat(legacyDataDir); err != nil || !info.IsDir() {