Skip to content

Commit

Permalink
open browser automatically after starting server
Browse files Browse the repository at this point in the history
  • Loading branch information
ayoisaiah committed Nov 3, 2024
1 parent 3a0b708 commit bc9a59f
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions stats/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ import (
"embed"
"fmt"
"html/template"
"log"
"math"
"net/http"
"os/exec"
"runtime"
"strings"
"time"

Expand Down Expand Up @@ -116,6 +119,25 @@ func (s *Stats) index(w http.ResponseWriter, r *http.Request) error {
return nil
}

func openbrowser(url string) {
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")
}
if err != nil {
log.Fatal(err)
}
}

func (s *Stats) Server(port uint) error {
mux := http.NewServeMux()

Expand All @@ -127,6 +149,8 @@ func (s *Stats) Server(port uint) error {

pterm.Info.Printfln("starting server on port: %d", port)

openbrowser("http://localhost:1111")

//nolint:gosec // no timeout is ok
return http.ListenAndServe(fmt.Sprintf(":%d", port), mux)
}

0 comments on commit bc9a59f

Please sign in to comment.