mirror of
https://github.com/maputnik/editor.git
synced 2026-08-30 08:57:27 +00:00
fix(desktop): open default browser on startup (#2095)
Fixes #934 ## Summary The Go desktop binary started its local server and printed the URL, but never opened a browser automatically. This PR: - opens the default browser after the listener successfully binds - uses the actual runtime URL - uses a stdlib-only cross-platform launcher - keeps browser-open failures non-fatal - adds `--no-browser` for headless/Docker use - leaves the normal Vite/web development flow unchanged The listener is created before launching the browser so the browser cannot race the server startup path. ## Platform behavior - Windows: `rundll32 url.dll,FileProtocolHandler` - macOS: `open` - Linux: `xdg-open` No shell command strings are used; arguments are passed directly through `exec.Command`. ## Testing - `go test ./...` - `go vet ./...` - `go build ./...` - `git diff --check` - repeated manual Windows startup verification (3 clean runs, confirmed the browser opened and hit the server, no duplicate launches) - `--no-browser` verification (confirmed no launch attempt occurs) - non-fatal launcher failure covered by a unit test that injects a nonexistent opener binary macOS/Linux launch paths were not runtime-tested on this Windows machine — they follow the standard `open`/`xdg-open` convention used across other Go CLIs but are unverified here. The full packaged release flow (`gox`, `go.rice`, `go-winres`) was not exercised locally.
This commit is contained in:
+26
-2
@@ -2,6 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@@ -9,6 +10,7 @@ import (
|
||||
"github.com/gorilla/handlers"
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/maputnik/desktop/filewatch"
|
||||
"github.com/pkg/browser"
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
|
||||
@@ -36,6 +38,10 @@ func main() {
|
||||
Name: "static",
|
||||
Usage: "Serve directory under /static/",
|
||||
},
|
||||
&cli.BoolFlag{
|
||||
Name: "no-browser",
|
||||
Usage: "Do not automatically open the default browser",
|
||||
},
|
||||
}
|
||||
|
||||
app.Action = func(c *cli.Context) error {
|
||||
@@ -71,8 +77,26 @@ func main() {
|
||||
loggedRouter := handlers.LoggingHandler(os.Stdout, router)
|
||||
corsRouter := handlers.CORS(handlers.AllowedHeaders([]string{"Content-Type"}), handlers.AllowedMethods([]string{"GET", "PUT"}), handlers.AllowedOrigins([]string{"*"}), handlers.AllowCredentials())(loggedRouter)
|
||||
|
||||
fmt.Printf("Exposing Maputnik on http://localhost:%d\n", c.Int("port"))
|
||||
return http.ListenAndServe(fmt.Sprintf(":%d", c.Int("port")), corsRouter)
|
||||
listener, err := net.Listen("tcp", fmt.Sprintf(":%d", c.Int("port")))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
url := fmt.Sprintf("http://localhost:%d", c.Int("port"))
|
||||
fmt.Printf("Exposing Maputnik on %s\n", url)
|
||||
|
||||
// Listener is already accepting connections, so this can't race http.Serve below.
|
||||
// xdg-open is known to hang on some headless Linux setups, so this runs in its own
|
||||
// goroutine to keep a stuck opener from stalling server startup.
|
||||
if !c.Bool("no-browser") {
|
||||
go func() {
|
||||
if err := browser.OpenURL(url); err != nil {
|
||||
fmt.Printf("Could not open browser automatically: %s\nPlease open %s manually.\n", err, url)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
return http.Serve(listener, corsRouter)
|
||||
}
|
||||
|
||||
app.Run(os.Args)
|
||||
|
||||
Reference in New Issue
Block a user