Skip to content

Commit

Permalink
Merge pull request #251 from SiaFoundation/nate/auto-open-browser
Browse files Browse the repository at this point in the history
Automatically open browser after startup is complete
  • Loading branch information
n8maninger authored Dec 29, 2023
2 parents 18800d1 + eb5ccca commit ee75b69
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions cmd/hostd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import (
"net"
"net/http"
"os"
"os/exec"
"os/signal"
"path/filepath"
"runtime"
"strings"
"syscall"
"time"
Expand Down Expand Up @@ -174,6 +176,19 @@ func startAPIListener(log *zap.Logger) (l net.Listener, err error) {
return
}

func openBrowser(url string) error {
switch runtime.GOOS {
case "linux":
return exec.Command("xdg-open", url).Start()
case "windows":
return exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start()
case "darwin":
return exec.Command("open", url).Start()
default:
return fmt.Errorf("unsupported platform %q", runtime.GOOS)
}
}

// mustSetWalletkey prompts the user to enter a wallet seed phrase if one is not
// already set via environment variable or config file.
func mustSetWalletkey(log *zap.Logger) {
Expand Down Expand Up @@ -389,6 +404,14 @@ func main() {
}
}()

time.Sleep(time.Millisecond) // give the web server a chance to start
_, port, err := net.SplitHostPort(apiListener.Addr().String())
if err != nil {
log.Debug("failed to parse API address", zap.Error(err))
} else if err := openBrowser(fmt.Sprintf("http://127.0.0.1:%s", port)); err != nil {
log.Debug("failed to open browser", zap.Error(err))
}

signalCh := make(chan os.Signal, 1)
signal.Notify(signalCh, os.Interrupt, syscall.SIGTERM)
<-signalCh
Expand Down

0 comments on commit ee75b69

Please sign in to comment.