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:
@@ -36,3 +36,13 @@ func (a *App) GetAPIBaseURL() string {
|
||||
}
|
||||
return strings.TrimRight(a.backendURL, "/") + "/api/v1"
|
||||
}
|
||||
|
||||
// GetDesktopHTTPPortSetting returns the saved local API port (0 = random port each launch).
|
||||
func (a *App) GetDesktopHTTPPortSetting() int {
|
||||
return LoadDesktopPrefsHTTPPort()
|
||||
}
|
||||
|
||||
// SetDesktopHTTPPortSetting saves the preferred local API port to application support. Restart the app for it to take effect unless it matches the current listener.
|
||||
func (a *App) SetDesktopHTTPPortSetting(port int) error {
|
||||
return SaveDesktopHTTPPortPreference(port)
|
||||
}
|
||||
|
||||
+14
-6
@@ -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() {
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
const desktopPrefsFileName = "desktop-prefs.json"
|
||||
|
||||
type desktopPrefs struct {
|
||||
HTTPPort int `json:"http_port"`
|
||||
}
|
||||
|
||||
func desktopPrefsDir() (string, error) {
|
||||
cfg, err := os.UserConfigDir()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
dir := filepath.Join(cfg, "WeKnora Lite")
|
||||
if err := os.MkdirAll(dir, 0o755); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return dir, nil
|
||||
}
|
||||
|
||||
func desktopPrefsFilePath() (string, error) {
|
||||
dir, err := desktopPrefsDir()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return filepath.Join(dir, desktopPrefsFileName), nil
|
||||
}
|
||||
|
||||
// LoadDesktopPrefsHTTPPort returns http_port from prefs file, or 0 if unset / invalid (ephemeral port on each launch).
|
||||
func LoadDesktopPrefsHTTPPort() int {
|
||||
path, err := desktopPrefsFilePath()
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
var p desktopPrefs
|
||||
if json.Unmarshal(data, &p) != nil {
|
||||
return 0
|
||||
}
|
||||
if p.HTTPPort < 0 || p.HTTPPort > 65535 {
|
||||
return 0
|
||||
}
|
||||
return p.HTTPPort
|
||||
}
|
||||
|
||||
// SaveDesktopHTTPPortPreference persists listen port preference. port 0 means use a random free port on each launch.
|
||||
func SaveDesktopHTTPPortPreference(port int) error {
|
||||
if port < 0 || port > 65535 {
|
||||
return fmt.Errorf("invalid port")
|
||||
}
|
||||
path, err := desktopPrefsFilePath()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
p := desktopPrefs{HTTPPort: port}
|
||||
data, err := json.MarshalIndent(p, "", " ")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return os.WriteFile(path, data, 0o600)
|
||||
}
|
||||
Reference in New Issue
Block a user