From c2b0d81a817503f8a84bd2f2a593441a55b4eb2a Mon Sep 17 00:00:00 2001 From: wizardchen Date: Sat, 11 Apr 2026 00:46:21 +0800 Subject: [PATCH] fix: use random port instead of fixed port for desktop app backend to avoid conflicts --- cmd/desktop/main.go | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/cmd/desktop/main.go b/cmd/desktop/main.go index d99c3ad9..99778f95 100644 --- a/cmd/desktop/main.go +++ b/cmd/desktop/main.go @@ -54,15 +54,22 @@ func main() { resourceCleaner interfaces.ResourceCleaner, ) error { server := &http.Server{Handler: router} - addr := fmt.Sprintf("%s:%d", cfg.Server.Host, cfg.Server.Port) - // Store the backend URL so Wails can load it - app.backendURL = fmt.Sprintf("http://%s", addr) + // 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" 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) + + // Store the backend URL so Wails can load it + app.backendURL = fmt.Sprintf("http://%s", actualAddr) // Handle graceful shutdown from Wails OnShutdown hook go func() { @@ -91,7 +98,7 @@ func main() { app.shutdownCh <- struct{}{} // trigger shutdown }() - logger.Infof(context.Background(), "Server is running at %s", addr) + logger.Infof(context.Background(), "Server is running at %s", actualAddr) if err := server.Serve(listener); err != nil && err != http.ErrServerClosed { return fmt.Errorf("server error: %v", err) }