From 1b96c96c297f5fd41a191eca8a343e6fe5a713aa Mon Sep 17 00:00:00 2001 From: Lukas Frey Date: Fri, 27 Nov 2026 10:29:48 +0100 Subject: [PATCH] feat(reverb): show the app's whole Reverb block for its .env The lines Flock shows for a site's Reverb server now include the app's id, key and secret: the ones the app's .env has, or ones named after the site (acme, acme_key, acme_secret) for an app without them. Pasting the block over an app's Reverb section no longer drops its credential. --- docs/php.md | 7 ++++++- internal/cli/reverb.go | 3 ++- internal/daemon/reverb.go | 8 +++++++- internal/daemon/reverb_test.go | 13 ++++++++++-- internal/reverb/reverb.go | 36 +++++++++++++++++++++++++++++----- 5 files changed, 57 insertions(+), 10 deletions(-) diff --git a/docs/php.md b/docs/php.md index 64d4a4ea..afffa6e4 100644 --- a/docs/php.md +++ b/docs/php.md @@ -138,14 +138,19 @@ Flock can run a Laravel app's [Reverb](https://reverb.laravel.com) server for it Only Reverb's traffic goes to it: WebSocket connections under `/app/` and the signed requests Laravel broadcasts through under `/apps/`. Every other request on those paths stays the app's, so a panel at `/app` keeps working. This needs Reverb's default paths, so leave `REVERB_SERVER_PATH` unset. -The app has to send its broadcasts and its Echo client to the site rather than to Reverb's own port. Flock shows the `.env` lines that do this, but doesn't change the project's `.env` itself: +The app has to send its broadcasts and its Echo client to the site rather than to Reverb's own port. Flock shows the app's whole Reverb block for its `.env`, but doesn't change the project's `.env` itself: ```env +REVERB_APP_ID=acme +REVERB_APP_KEY=acme_key +REVERB_APP_SECRET=acme_secret REVERB_HOST=acme.test REVERB_PORT=443 REVERB_SCHEME=https ``` +The id, key and secret are a credential only the app and its own Reverb server share, and both read it from this `.env`, so any values work. Flock shows the ones the `.env` already has, and ones named after the site for an app without them. Restart the Reverb server after changing them. + On a site served over HTTP only, they are `REVERB_PORT=80` and `REVERB_SCHEME=http`. `install:broadcasting --reverb` writes the `VITE_REVERB_*` variables as references to these, so the Echo client follows them. Rebuild the front end (`npm run build`, or restart `npm run dev`) after changing them. Restart the Reverb server to load changed code. It restarts on its own, so the site's FrankenPHP keeps running, and restarting FrankenPHP keeps Reverb's connections. Reverb's output goes to the `reverb-acme` [log](logs.md). diff --git a/internal/cli/reverb.go b/internal/cli/reverb.go index df20a450..c77f4652 100644 --- a/internal/cli/reverb.go +++ b/internal/cli/reverb.go @@ -17,7 +17,8 @@ func newReverbCommand() *cobra.Command { Long: `Flock runs ` + "`php artisan reverb:start`" + ` for the site with its PHP version, on a loopback port, and its web server sends the site's WebSocket connections (/app/...) and broadcasts (/apps/...) there. Browsers connect to the site's own address, over HTTPS when the site is secured; the app's .env -needs the REVERB_HOST, REVERB_PORT and REVERB_SCHEME lines ` + "`flock reverb`" + ` shows. +needs the REVERB_* lines ` + "`flock reverb`" + ` shows: its id, key and secret (the ones it has, or +ones named after the site) and the site's host, port and scheme. flock reverb [site] show the site's Reverb server and its .env lines flock reverb on [site] run it (the project needs ` + "`php artisan install:broadcasting --reverb`" + `) diff --git a/internal/daemon/reverb.go b/internal/daemon/reverb.go index 2b67f76c..f7b8c9fa 100644 --- a/internal/daemon/reverb.go +++ b/internal/daemon/reverb.go @@ -2,6 +2,7 @@ package daemon import ( "fmt" + "path/filepath" "strings" "flock/internal/api" @@ -9,6 +10,7 @@ import ( "flock/internal/ports" "flock/internal/reverb" "flock/internal/supervisor" + "flock/internal/worktree" ) // reverbStatus is a site's Reverb server as status reports it, before its state is known. @@ -17,7 +19,11 @@ func reverbStatus(cfg *config.Config, site config.Site, scheme string) api.SiteR if scheme == "https" { port = cfg.Ports.HTTPS } - return api.SiteReverb{Port: site.ReverbPort, Log: config.ReverbPortID(site.Name), Env: reverb.Env(cfg.Domain(site), scheme, port)} + current := func(string) string { return "" } + if env, err := worktree.ReadDotenv(filepath.Join(site.Path, ".env")); err == nil { + current = env.Get + } + return api.SiteReverb{Port: site.ReverbPort, Log: config.ReverbPortID(site.Name), Env: reverb.Env(site.Name, cfg.Domain(site), scheme, port, current)} } // reverbBlocked is the status of an enabled Reverb server that can't run because the site itself diff --git a/internal/daemon/reverb_test.go b/internal/daemon/reverb_test.go index f931ddd2..da2da028 100644 --- a/internal/daemon/reverb_test.go +++ b/internal/daemon/reverb_test.go @@ -39,9 +39,18 @@ func TestReverbSiteRunsReverbStartOrSaysWhyNot(t *testing.T) { if spec == nil || st.Problem != "" || st.Log != "reverb-app" || st.Port != site.ReverbPort { t.Fatalf("with Reverb: %v %+v", spec, st) } - if want := []string{"REVERB_HOST=app.test", "REVERB_PORT=8443", "REVERB_SCHEME=https"}; !slices.Equal(st.Env, want) { + if want := []string{"REVERB_APP_ID=app", "REVERB_APP_KEY=app_key", "REVERB_APP_SECRET=app_secret", "REVERB_HOST=app.test", "REVERB_PORT=8443", "REVERB_SCHEME=https"}; !slices.Equal(st.Env, want) { t.Errorf(".env lines: %v", st.Env) } + + // The credential the app's .env has is the one shown; a variable it leaves empty is named after the site. + if err := os.WriteFile(filepath.Join(app, ".env"), []byte("REVERB_APP_ID=shop\nREVERB_APP_KEY=\"local key\"\nREVERB_APP_SECRET=\nREVERB_HOST=localhost\n"), 0o644); err != nil { + t.Fatal(err) + } + _, st = d.reverbSite(cfg, site, "8.4", "https", nil) + if want := []string{"REVERB_APP_ID=shop", `REVERB_APP_KEY="local key"`, "REVERB_APP_SECRET=app_secret", "REVERB_HOST=app.test", "REVERB_PORT=8443", "REVERB_SCHEME=https"}; !slices.Equal(st.Env, want) { + t.Errorf(".env lines of an app with a credential: %v", st.Env) + } args := strings.Join(spec.Args, " ") if !strings.HasPrefix(args, "artisan reverb:start --host=127.0.0.1 --port=") || !strings.Contains(args, "--hostname=127.0.0.1") || spec.Dir != app { t.Errorf("command: %s in %s", args, spec.Dir) @@ -126,7 +135,7 @@ func TestAnEnabledReverbServerSaysWhyItCantRun(t *testing.T) { plain, _ := cfg.FindSite("plain") st := reverbBlocked(cfg, app, "http", "PHP 8.4 is not installed") - if st == nil || st.State != api.ReverbUnavailable || st.Problem != "PHP 8.4 is not installed" || st.Log != "reverb-app" || len(st.Env) != 3 { + if st == nil || st.State != api.ReverbUnavailable || st.Problem != "PHP 8.4 is not installed" || st.Log != "reverb-app" || len(st.Env) != 6 { t.Fatalf("blocked: %+v", st) } if reverbBlocked(cfg, plain, "http", "anything") != nil { diff --git a/internal/reverb/reverb.go b/internal/reverb/reverb.go index 1dd3fd03..22995a46 100644 --- a/internal/reverb/reverb.go +++ b/internal/reverb/reverb.go @@ -7,6 +7,7 @@ import ( "os" "path/filepath" "strconv" + "strings" "syscall" "flock/internal/config" @@ -51,11 +52,36 @@ func Spec(root string, s Site) supervisor.Spec { } } -// Env is what the app's .env needs for its broadcasts and its Echo client to go through the web -// server: the site's host, and the scheme and port it is served on. install:broadcasting's -// VITE_REVERB_* variables refer to these. -func Env(host, scheme string, port int) []string { - return []string{"REVERB_HOST=" + host, "REVERB_PORT=" + strconv.Itoa(port), "REVERB_SCHEME=" + scheme} +// Env is the app's whole Reverb block for its .env. The app's id, key and secret are a credential +// only the app and its own Reverb server share, both reading it from this .env: the ones the app +// has are kept (current returns a variable's value there, "" when unset), and an app without +// them gets ones named after the site. The host, scheme and port send its broadcasts and its +// Echo client through the web server. install:broadcasting's VITE_REVERB_* variables refer to +// these. +func Env(site, host, scheme string, port int, current func(key string) string) []string { + credential := func(key, fallback string) string { + if value := current(key); value != "" { + return key + "=" + dotenvValue(value) + } + return key + "=" + fallback + } + return []string{ + credential("REVERB_APP_ID", site), + credential("REVERB_APP_KEY", site+"_key"), + credential("REVERB_APP_SECRET", site+"_secret"), + "REVERB_HOST=" + host, + "REVERB_PORT=" + strconv.Itoa(port), + "REVERB_SCHEME=" + scheme, + } +} + +// dotenvValue writes a value as a .env file takes it: in double quotes when it has a character +// that would end or change an unquoted one. +func dotenvValue(value string) string { + if !strings.ContainsAny(value, " \t#\"'\\$") { + return value + } + return `"` + strings.NewReplacer(`\`, `\\`, `"`, `\"`).Replace(value) + `"` } func exists(path string) bool {