From dcf9fbaa5013a87a1001f6c3031f3bab8cf285bd Mon Sep 17 00:00:00 2001 From: Robin Olsen Date: Thu, 26 Feb 2026 18:54:51 +0100 Subject: [PATCH] automaticaly open browser when starting server --- internal/utils/browser.go | 24 ++++++++++++++++++++++++ pkg/web/web.go | 12 ++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 internal/utils/browser.go diff --git a/internal/utils/browser.go b/internal/utils/browser.go new file mode 100644 index 0000000..1c4bac7 --- /dev/null +++ b/internal/utils/browser.go @@ -0,0 +1,24 @@ +package utils + +import ( + "fmt" + "os/exec" + "runtime" +) + +// Taken and modified from https://gist.github.com/hyg/9c4afcd91fe24316cbf0 +func OpenBrowser(url string) error { + var err error + + switch runtime.GOOS { + case "linux": + err = exec.Command("xdg-open", url).Start() + case "windows": + err = exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start() + case "darwin": + err = exec.Command("open", url).Start() + default: + err = fmt.Errorf("unsupported platform") + } + return err +} diff --git a/pkg/web/web.go b/pkg/web/web.go index 896d3d6..3eb1fe1 100644 --- a/pkg/web/web.go +++ b/pkg/web/web.go @@ -6,9 +6,11 @@ import ( "errors" "fmt" "net/http" + "strings" "time" "github.com/LazyBachelor/LazyPM/internal/service" + "github.com/LazyBachelor/LazyPM/internal/utils" "github.com/LazyBachelor/LazyPM/pkg/task" "github.com/LazyBachelor/LazyPM/pkg/web/handler" "github.com/LazyBachelor/LazyPM/pkg/web/server" @@ -44,6 +46,16 @@ func (w *Web) Run(ctx context.Context, config Config) error { fmt.Printf("Starting web server on %s...\n", config.WebAddress) + address := config.WebAddress + if !strings.Contains(address, "http") { + address = "http://localhost" + address + } + + err = utils.OpenBrowser(address) + if err != nil { + return fmt.Errorf("failed to open browser: %w", err) + } + serverErr := make(chan error, 1) go func() { if err := httpServer.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {