Skip to content

Commit

Permalink
open browser after gin initiated
Browse files Browse the repository at this point in the history
  • Loading branch information
Sagleft committed Feb 19, 2022
1 parent 9f90b87 commit 15b0894
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
16 changes: 16 additions & 0 deletions helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ package main
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"os/exec"
"runtime"
)

func (sol *solution) parseConfig() error {
Expand All @@ -30,3 +33,16 @@ func checkErrors(errChecks ...errorFunc) error {
}
return nil
}

func openBrowserURL(urlAddress string) error {
switch runtime.GOOS {
case "linux":
return exec.Command("xdg-open", urlAddress).Start()
case "windows":
return exec.Command("rundll32", "url.dll,FileProtocolHandler", urlAddress).Start()
case "darwin":
return exec.Command("open", urlAddress).Start()
default:
return fmt.Errorf("unsupported platform")
}
}
7 changes: 7 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ func (sol *solution) initGin() error {
}

func (sol *solution) runGin() error {
go func() {
time.Sleep(time.Millisecond * 400)
err := openBrowserURL("http://127.0.0.1:" + sol.Config.BindPort)
if err != nil {
log.Fatalln(err)
}
}()
return sol.Gin.Run(":" + sol.Config.BindPort)
}

Expand Down

0 comments on commit 15b0894

Please sign in to comment.