Repository navigation
feat: run a Laravel app's Reverb server for its site #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ab2fe9a
feat: run a Laravel app's Reverb server for its site
sineld cf7d855
Merge remote-tracking branch 'origin/develop' into feat/reverb
sineld 69f3301
Merge remote-tracking branch 'origin/develop' into feat/reverb
sineld 6d24820
docs: Reverb in PHP, the CLI reference, AI agents and the introduction
sineld ff931ca
fix(reverb): address review findings on status, retries, timeouts and…
sineld 1554eda
Update reverb handling to include abort directive
onairmarc 9c2e04d
fix(web): reject remote clients before any of a site's handle blocks
sineld e72857c
fix(reverb): release every Reverb server for all PHP versions, restar…
sineld File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| package cli | ||
|
|
||
| import ( | ||
| "errors" | ||
| "strings" | ||
|
|
||
| "github.com/spf13/cobra" | ||
|
|
||
| "flock/internal/api" | ||
| "flock/internal/reverb" | ||
| ) | ||
|
|
||
| func newReverbCommand() *cobra.Command { | ||
| cmd := &cobra.Command{ | ||
| Use: "reverb", | ||
| Short: "Run a Laravel app's Reverb server, reachable at wss://<site>.test/app", | ||
| 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. | ||
|
|
||
| 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`" + `) | ||
| flock reverb off [site] stop it | ||
| flock reverb restart [site] restart it, e.g. to load changed code`, | ||
| Args: cobra.MaximumNArgs(1), | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| s, err := load() | ||
| if err != nil { | ||
| return err | ||
| } | ||
| site, err := siteArg(s, args) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| return showReverb(cmd, s, site.Name) | ||
| }, | ||
| } | ||
|
|
||
| toggle := func(on bool) func(*cobra.Command, []string) error { | ||
| return func(cmd *cobra.Command, args []string) error { | ||
| s, err := load() | ||
| if err != nil { | ||
| return err | ||
| } | ||
| site, err := siteArg(s, args) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if on && !reverb.Installed(site.Path) { | ||
| return errors.New("Laravel Reverb isn't installed in " + site.Path + " — run `php artisan install:broadcasting --reverb` first") | ||
| } | ||
| s.cfg.SetReverb(&site, on) | ||
| s.cfg.Upsert(site) | ||
| if err := s.commit(cmd.Context(), cmd.ErrOrStderr()); err != nil { | ||
| return err | ||
| } | ||
| if !on { | ||
| cmd.Printf("stopped %s's Reverb server\n", site.Name) | ||
| return nil | ||
| } | ||
| // commit has already said that a stopped daemon applies the change when it starts. | ||
| if err := showReverb(cmd, s, site.Name); !errors.Is(err, api.ErrDaemonNotRunning) { | ||
| return err | ||
| } | ||
| return nil | ||
| } | ||
| } | ||
| on := &cobra.Command{Use: "on [site]", Short: "Run a site's Reverb server", Args: cobra.MaximumNArgs(1), RunE: toggle(true)} | ||
| off := &cobra.Command{Use: "off [site]", Short: "Stop a site's Reverb server", Args: cobra.MaximumNArgs(1), RunE: toggle(false)} | ||
|
|
||
| restart := &cobra.Command{ | ||
| Use: "restart [site]", | ||
| Short: "Restart a site's Reverb server (and its FrankenPHP), e.g. to load changed code", | ||
| Args: cobra.MaximumNArgs(1), | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| s, err := load() | ||
| if err != nil { | ||
| return err | ||
| } | ||
| site, err := siteArg(s, args) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if !site.Reverb { | ||
| return errors.New("Reverb is off for " + site.Name + " — `flock reverb on` runs it") | ||
| } | ||
| st, err := s.client().RestartSite(cmd.Context(), site.Name) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| printReverb(cmd, st, site.Name) | ||
| return nil | ||
| }, | ||
| } | ||
|
|
||
| cmd.AddCommand(on, off, restart) | ||
| return cmd | ||
| } | ||
|
|
||
| // showReverb prints the site's Reverb server as the daemon reports it. | ||
| func showReverb(cmd *cobra.Command, s *state, name string) error { | ||
| st, err := s.client().Status(cmd.Context()) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| printReverb(cmd, st, name) | ||
| return nil | ||
| } | ||
|
|
||
| func printReverb(cmd *cobra.Command, st api.Status, name string) { | ||
| for _, site := range st.Sites { | ||
| if site.Name != name { | ||
| continue | ||
| } | ||
| r := site.Reverb | ||
| switch { | ||
| case r == nil: | ||
| cmd.Printf("Reverb is off for %s — `flock reverb on` runs it\n", name) | ||
| return | ||
| case r.State == api.ReverbRunning: | ||
| cmd.Printf("%s's Reverb server is running, log: `flock logs %s`\n", name, r.Log) | ||
| default: | ||
| cmd.Printf("%s's Reverb server isn't running: %s\n", name, r.Problem) | ||
| } | ||
| cmd.Printf("in the app's .env:\n %s\n", strings.Join(r.Env, "\n ")) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| package cli | ||
|
|
||
| import ( | ||
| "errors" | ||
| "io" | ||
| "testing" | ||
|
|
||
| "flock/internal/api" | ||
| "flock/internal/config" | ||
| ) | ||
|
|
||
| func TestReverbStatusSaysWhenTheDaemonIsNotRunning(t *testing.T) { | ||
| root := t.TempDir() | ||
| t.Setenv("FLOCK_HOME", root) | ||
| cfg := config.Default() | ||
| cfg.Upsert(config.Site{Name: "app", Path: t.TempDir(), Reverb: true, ReverbPort: 21000}) | ||
| if err := cfg.Save(root); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| cmd := newReverbCommand() | ||
| cmd.SetArgs([]string{"app"}) | ||
| cmd.SetOut(io.Discard) | ||
| cmd.SetErr(io.Discard) | ||
|
|
||
| // It used to exit successfully without printing anything. | ||
| if err := cmd.Execute(); !errors.Is(err, api.ErrDaemonNotRunning) { | ||
| t.Fatalf("err = %v, want %v", err, api.ErrDaemonNotRunning) | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.