Launch Browser on Server Start

This commit is contained in:
Nick Eyre
2014-06-03 14:14:22 -07:00
parent f3f2980951
commit 8c22f7a9ac

23
web.go
View File

@@ -11,6 +11,9 @@ import (
"html/template"
"log"
"net/http"
"os/exec"
"runtime"
"strconv"
)
const httpPort = 8080
@@ -32,6 +35,26 @@ func ServeWebInterface() {
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static/"))))
http.Handle("/", newHandler())
log.Printf("Serving HTTP requests on port %d", httpPort)
// Open in Default Web Browser
// Necessary to Authenticate
url := "http://localhost:"+strconv.Itoa(httpPort)
var err error
switch runtime.GOOS {
case "linux":
err = exec.Command("xdg-open", url).Start()
case "darwin":
err = exec.Command("open", url).Start()
case "windows":
err = exec.Command(`rundll32.exe`, "url.dll,FileProtocolHandler", url).Start()
default:
err = fmt.Errorf("unsupported platform")
}
if err != nil {
println(err.Error())
}
// Start Server
http.ListenAndServe(fmt.Sprintf(":%d", httpPort), nil)
}