Skip to content

Commit f234560

Browse files
committed
Open browser on startup, fixes #4
1 parent a08a51a commit f234560

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

main.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"log"
99
"net/http"
1010
"sync"
11+
"runtime"
12+
"os/exec"
1113
)
1214

1315
var es eventsource.EventSource
@@ -31,10 +33,18 @@ func main() {
3133
http.Handle("/", http.FileServer(AssetFile()))
3234

3335
log.Printf("Starting webserver at %s:%d\n", *host, *port)
36+
37+
url := fmt.Sprintf("localhost:%d", *port)
38+
if (*host != "") {
39+
url = fmt.Sprintf("%s:%d", *host, *port)
40+
}
41+
openbrowser(fmt.Sprintf("%s", url))
42+
3443
err := http.ListenAndServe(fmt.Sprintf("%s:%d", *host, *port), nil)
3544
if err != nil {
3645
log.Fatalln(err)
3746
}
47+
3848
}
3949

4050
func clientEvent(w http.ResponseWriter, r *http.Request) {
@@ -96,3 +106,24 @@ func unlock(w http.ResponseWriter, r *http.Request) {
96106

97107
log.Printf("Removed lock for %s\n", id)
98108
}
109+
110+
111+
func openbrowser(url string) {
112+
var err error
113+
114+
log.Printf("Try to opening URL %s in browser\n", url)
115+
switch runtime.GOOS {
116+
case "linux":
117+
err = exec.Command("xdg-open", url).Run()
118+
case "windows":
119+
err = exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Run()
120+
case "darwin":
121+
err = exec.Command("open", url).Run()
122+
default:
123+
err = fmt.Errorf("unsupported platform")
124+
}
125+
126+
if err != nil {
127+
log.Println(err)
128+
}
129+
}

0 commit comments

Comments
 (0)