dockerimg: remove configurability from open browser request
Innie is untrusted, so we can't let it provide a url to open (duh).
There's a chicken-and-egg problem here: we need to start the git
server before launching the container, but we need the container
port information to store the ps1URL on the git server.
Solve it with a little sync/atomic. There's a logical race here,
but if we lose the race, the behavior is that nothing happens,
at which point the user tries again and it works.
Good enough for now.
Co-Authored-By: sketch <hello@sketch.dev>
diff --git a/dockerimg/dockerimg.go b/dockerimg/dockerimg.go
index 8c2a4e3..6164a31 100644
--- a/dockerimg/dockerimg.go
+++ b/dockerimg/dockerimg.go
@@ -18,6 +18,7 @@
"path/filepath"
"runtime"
"strings"
+ "sync/atomic"
"time"
"sketch.dev/browser"
@@ -311,13 +312,14 @@
}
// We open the browser after the init config because the above waits for the web server to be serving.
- if config.OpenBrowser {
- if config.SkabandAddr != "" {
- browser.Open(fmt.Sprintf("%s/s/%s", config.SkabandAddr, config.SessionID))
- } else {
- browser.Open("http://" + localAddr)
- }
+ ps1URL := "http://" + localAddr
+ if config.SkabandAddr != "" {
+ ps1URL = fmt.Sprintf("%s/s/%s", config.SkabandAddr, config.SessionID)
}
+ if config.OpenBrowser {
+ browser.Open(ps1URL)
+ }
+ gitSrv.ps1URL.Store(&ps1URL)
}()
go func() {
@@ -372,6 +374,7 @@
gitPort string
srv *http.Server
pass string
+ ps1URL atomic.Pointer[string]
}
func (gs *gitServer) shutdown(ctx context.Context) {
@@ -396,10 +399,11 @@
}
ret.gitLn = gitLn
- browserC := make(chan string, 1) // channel of URLs to open in browser
+ browserC := make(chan bool, 1) // channel of browser open requests
+
go func() {
- for url := range browserC {
- browser.Open(url)
+ for range browserC {
+ browser.Open(*ret.ps1URL.Load())
}
}()