From 6fb6fc4f102a5ae12e838cbe64a260d608bb0ddc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B4=E6=96=87=E5=B3=B0?= Date: Thu, 4 Jun 2026 17:49:31 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=B7=A8=E7=AB=AF=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config.go | 13 ++++++++- config_test.go | 31 +++++++++++++++++++++ main.go | 4 ++- meshmap_frontend/src/components/MeshMap.vue | 4 ++- 4 files changed, 49 insertions(+), 3 deletions(-) diff --git a/config.go b/config.go index f06d786..4e3b72a 100644 --- a/config.go +++ b/config.go @@ -178,11 +178,19 @@ func defaultWebSocketPath() string { func defaultWebSocketPathForGOOS(goos string) string { if goos == "windows" { - return filepath.Join(".", "win", "opt", "mesh_mqtt_go", "web.sock") + return "" } return filepath.Join(string(filepath.Separator), "opt", "mesh_mqtt_go", "web.sock") } +func clearWebSocketPathOnUnsupportedGOOS(cfg *config, goos string) bool { + if goos != "windows" || cfg.Web.SocketPath == "" { + return false + } + cfg.Web.SocketPath = "" + return true +} + func defaultSQLitePathForGOOS(goos string) string { if goos == "windows" { return filepath.Join(".", "win", "etc", "mesh_mqtt_go", "mesh_mqtt_go.db") @@ -221,6 +229,9 @@ func loadConfig(path string) (*config, error) { } cfg, changed := normalizeConfig(raw) + if clearWebSocketPathOnUnsupportedGOOS(cfg, runtime.GOOS) { + changed = true + } if err := validateConfig(cfg); err != nil { return nil, err } diff --git a/config_test.go b/config_test.go index 013ad38..6a480ff 100644 --- a/config_test.go +++ b/config_test.go @@ -154,6 +154,37 @@ func TestLoadConfigMalformedYAMLDoesNotOverwrite(t *testing.T) { } } +func TestDefaultWebSocketPathForGOOS(t *testing.T) { + if windowsPath := defaultWebSocketPathForGOOS("windows"); windowsPath != "" { + t.Fatalf("windows web socket path = %q, want empty", windowsPath) + } + + linuxPath := defaultWebSocketPathForGOOS("linux") + want := filepath.Join(string(filepath.Separator), "opt", "mesh_mqtt_go", "web.sock") + if linuxPath != want { + t.Fatalf("linux web socket path = %q, want %q", linuxPath, want) + } +} + +func TestClearWebSocketPathOnUnsupportedGOOS(t *testing.T) { + cfg := defaultConfig() + cfg.Web.SocketPath = filepath.Join(".", "win", "opt", "mesh_mqtt_go", "web.sock") + if !clearWebSocketPathOnUnsupportedGOOS(cfg, "windows") { + t.Fatalf("clearWebSocketPathOnUnsupportedGOOS() = false, want true") + } + if cfg.Web.SocketPath != "" { + t.Fatalf("windows web socket path = %q, want empty", cfg.Web.SocketPath) + } + + cfg.Web.SocketPath = "/opt/mesh_mqtt_go/web.sock" + if clearWebSocketPathOnUnsupportedGOOS(cfg, "linux") { + t.Fatalf("linux clearWebSocketPathOnUnsupportedGOOS() = true, want false") + } + if cfg.Web.SocketPath == "" { + t.Fatalf("linux web socket path was cleared") + } +} + func TestDefaultSQLitePathForGOOS(t *testing.T) { windowsPath := defaultSQLitePathForGOOS("windows") if !strings.Contains(windowsPath, filepath.Join("win", "etc", "mesh_mqtt_go", "mesh_mqtt_go.db")) { diff --git a/main.go b/main.go index dd4f781..c34d45e 100644 --- a/main.go +++ b/main.go @@ -9,6 +9,7 @@ import ( "net/http" "os" "os/signal" + "runtime" "strconv" "syscall" "time" @@ -214,7 +215,7 @@ func parseArgs() (*config, error) { flag.BoolVar(&cfg.Web.Enabled, "web", cfg.Web.Enabled, "Enable Gin web server") flag.StringVar(&cfg.Web.Host, "web-host", cfg.Web.Host, "Web server listen host") flag.IntVar(&cfg.Web.Port, "web-port", cfg.Web.Port, "Web server listen port") - flag.StringVar(&cfg.Web.SocketPath, "web-socket-path", cfg.Web.SocketPath, "Web server Unix socket path; empty uses host and port") + flag.StringVar(&cfg.Web.SocketPath, "web-socket-path", cfg.Web.SocketPath, "Web server Unix socket path; empty uses host and port; unsupported on Windows") flag.StringVar(&cfg.Web.StaticDir, "web-static-dir", cfg.Web.StaticDir, "Web frontend static files directory") flag.StringVar(&cfg.Web.Admin.Username, "admin-username", cfg.Web.Admin.Username, "Web admin username") flag.Parse() @@ -225,6 +226,7 @@ func parseArgs() (*config, error) { if value := os.Getenv("MESH_ADMIN_SESSION_SECRET"); value != "" { cfg.Web.Admin.SessionSecret = value } + clearWebSocketPathOnUnsupportedGOOS(cfg, runtime.GOOS) if err := validateConfig(cfg); err != nil { return nil, err diff --git a/meshmap_frontend/src/components/MeshMap.vue b/meshmap_frontend/src/components/MeshMap.vue index 31464a3..70cf9be 100644 --- a/meshmap_frontend/src/components/MeshMap.vue +++ b/meshmap_frontend/src/components/MeshMap.vue @@ -32,6 +32,8 @@ let markerLayer: L.LayerGroup | null = null let hasFitBounds = false const minMapZoom = 3 +const defaultMapCenter: L.LatLngExpression = [35.8617, 104.1954] +const defaultMapZoom = 4 const worldBounds = L.latLngBounds( [-85.05112878, -180], [85.05112878, 180], @@ -50,7 +52,7 @@ onMounted(async () => { maxBounds: worldBounds, maxBoundsViscosity: 1.0, worldCopyJump: false, - }).setView([0, 0], minMapZoom) + }).setView(defaultMapCenter, defaultMapZoom) L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', { minZoom: minMapZoom, maxZoom: 19,