fix: use random port instead of fixed port for desktop app backend to avoid conflicts
This commit is contained in:
+11
-4
@@ -54,15 +54,22 @@ func main() {
|
|||||||
resourceCleaner interfaces.ResourceCleaner,
|
resourceCleaner interfaces.ResourceCleaner,
|
||||||
) error {
|
) error {
|
||||||
server := &http.Server{Handler: router}
|
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
|
// Use port 0 to assign a random free port for the desktop app
|
||||||
app.backendURL = fmt.Sprintf("http://%s", addr)
|
// Force localhost binding to prevent firewall popups on macOS
|
||||||
|
addr := "127.0.0.1:0"
|
||||||
|
|
||||||
listener, err := listenWithRetry(addr, 10, 300*time.Millisecond)
|
listener, err := listenWithRetry(addr, 10, 300*time.Millisecond)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to start server: %v", err)
|
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
|
// Handle graceful shutdown from Wails OnShutdown hook
|
||||||
go func() {
|
go func() {
|
||||||
@@ -91,7 +98,7 @@ func main() {
|
|||||||
app.shutdownCh <- struct{}{} // trigger shutdown
|
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 {
|
if err := server.Serve(listener); err != nil && err != http.ErrServerClosed {
|
||||||
return fmt.Errorf("server error: %v", err)
|
return fmt.Errorf("server error: %v", err)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user