automaticaly open browser when starting server

This commit is contained in:
Robin Olsen
2026-02-26 18:54:51 +01:00
parent f0a4630866
commit dcf9fbaa50
2 changed files with 36 additions and 0 deletions

24
internal/utils/browser.go Normal file
View File

@@ -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
}

View File

@@ -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) {