From a5a49ff08056a67cc57435f219aa157342a0d9a0 Mon Sep 17 00:00:00 2001 From: Mitch Riedstra Date: Mon, 2 Aug 2021 20:10:23 -0400 Subject: Compatibility with MacOS. Starting browsers is now a bit simplier on each platform. --- cmd/web/darwin.go | 18 ++++++++++++++++++ cmd/web/main.go | 19 +++++++++++-------- cmd/web/startBrowser.go | 13 +++++++++++++ cmd/web/unix.go | 10 ++++++---- cmd/web/windows.go | 14 +++++--------- 5 files changed, 53 insertions(+), 21 deletions(-) create mode 100644 cmd/web/darwin.go create mode 100644 cmd/web/startBrowser.go (limited to 'cmd/web') diff --git a/cmd/web/darwin.go b/cmd/web/darwin.go new file mode 100644 index 0000000..7330733 --- /dev/null +++ b/cmd/web/darwin.go @@ -0,0 +1,18 @@ +// +build darwin + +package main + +import ( + "os" + "path/filepath" +) + +// DefaultLib path for the operating system in question +var DefaultLib string = filepath.Join(os.Getenv("HOME"), + "Library/Application Support/Steam/steamapps") + +// BrowserCommand returns a []string containing the command that will start the +// browser for a given url specific to the OS +func BrowserCommand(url string) []string { + return []string{"open", url} +} diff --git a/cmd/web/main.go b/cmd/web/main.go index c799624..6db58d9 100644 --- a/cmd/web/main.go +++ b/cmd/web/main.go @@ -5,6 +5,7 @@ import ( "flag" "fmt" "log" + "net" "net/http" "os" "strconv" @@ -67,15 +68,11 @@ func main() { http.FileServer(http.FS(embeddedStatic))) r.HandleFunc("/", a.HandleIndex) - s := http.Server{ - Handler: r, - Addr: Listen, - } - - go startBrowser() + s := http.Server{Handler: r} for i := 0; i < 5; i++ { - err = s.ListenAndServe() + l, err := net.Listen("tcp", Listen) + if err != nil { Logger.Printf("Encountered: %s", err) @@ -89,8 +86,14 @@ func main() { Listen = fmt.Sprintf("%s:%d", parts[0], port) Logger.Printf("Trying: %s", Listen) - s.Addr = Listen + continue } + startBrowser("http://localhost" + Listen) + + err = s.Serve(l) + if err != nil { + panic(err) + } } } diff --git a/cmd/web/startBrowser.go b/cmd/web/startBrowser.go new file mode 100644 index 0000000..0184055 --- /dev/null +++ b/cmd/web/startBrowser.go @@ -0,0 +1,13 @@ +package main + +import ( + "os/exec" +) + +func startBrowser(url string) { + command := BrowserCommand(url) + + c := exec.Command(command[0], command[1:]...) + Logger.Printf("Running command: %s result: %v", command, c.Run()) + return +} diff --git a/cmd/web/unix.go b/cmd/web/unix.go index ba028ab..ba57e22 100644 --- a/cmd/web/unix.go +++ b/cmd/web/unix.go @@ -1,4 +1,4 @@ -// +build darwin dragonfly freebsd linux nacl netbsd openbsd solaris +// +build dragonfly freebsd linux nacl netbsd openbsd solaris package main @@ -7,9 +7,11 @@ import ( "path/filepath" ) +// DefaultLib path for the operating system in question var DefaultLib string = filepath.Join(os.Getenv("HOME"), ".steam/steam/steamapps") -// TODO -func startBrowser() { - return +// BrowserCommand returns a []string containing the command that will start the +// browser for a given url specific to the OS +func BrowserCommand(url string) []string { + return []string{"xdg-open", url} } diff --git a/cmd/web/windows.go b/cmd/web/windows.go index f59761a..76491f1 100644 --- a/cmd/web/windows.go +++ b/cmd/web/windows.go @@ -2,15 +2,11 @@ package main -import ( - "os/exec" - "time" -) - +// DefaultLib path for the operating system in question var DefaultLib string = `C:\Program Files (x86)\Steam\steamapps` -func startBrowser() { - time.Sleep(time.Second * 3) - c := exec.Command("cmd", "/c", "start", "http://127.0.0.1"+Listen) - Logger.Printf("Running command: %s", c.Run()) +// BrowserCommand returns a []string containing the command that will start the +// browser for a given url specific to the OS +func BrowserCommand(url string) []string { + return []string{"cmd", "/c", "start", url} } -- cgit v1.2.3