Skip to content

Commit

Permalink
feat: add --proxybind flag to generate watch mode (#615)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlyxPink authored Mar 15, 2024
1 parent 358286c commit d45ff2e
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 7 deletions.
7 changes: 5 additions & 2 deletions cmd/templ/generatecmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,10 +301,13 @@ func (cmd *Generate) StartProxy(ctx context.Context) (p *proxy.Handler, err erro
if cmd.Args.ProxyPort == 0 {
cmd.Args.ProxyPort = 7331
}
p = proxy.New(cmd.Args.ProxyPort, target)
if cmd.Args.ProxyBind == "" {
cmd.Args.ProxyBind = "127.0.0.1"
}
p = proxy.New(cmd.Args.ProxyBind, cmd.Args.ProxyPort, target)
go func() {
cmd.Log.Info("Proxying", slog.String("from", p.URL), slog.String("to", p.Target.String()))
if err := http.ListenAndServe(fmt.Sprintf("127.0.0.1:%d", cmd.Args.ProxyPort), p); err != nil {
if err := http.ListenAndServe(fmt.Sprintf("%s:%d", cmd.Args.ProxyBind, cmd.Args.ProxyPort), p); err != nil {
cmd.Log.Error("Proxy failed", slog.Any("error", err))
}
}()
Expand Down
1 change: 1 addition & 0 deletions cmd/templ/generatecmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type Arguments struct {
Watch bool
OpenBrowser bool
Command string
ProxyBind string
ProxyPort int
Proxy string
WorkerCount int
Expand Down
4 changes: 2 additions & 2 deletions cmd/templ/generatecmd/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func modifyResponse(r *http.Response) error {
return modifier(r)
}

func New(port int, target *url.URL) *Handler {
func New(bind string, port int, target *url.URL) *Handler {
p := httputil.NewSingleHostReverseProxy(target)
p.ErrorLog = log.New(os.Stderr, "Proxy to target error: ", 0)
p.Transport = &roundTripper{
Expand All @@ -100,7 +100,7 @@ func New(port int, target *url.URL) *Handler {
}
p.ModifyResponse = modifyResponse
return &Handler{
URL: fmt.Sprintf("http://127.0.0.1:%d", port),
URL: fmt.Sprintf("http://%s:%d", bind, port),
Target: target,
p: p,
sse: sse.New(),
Expand Down
10 changes: 7 additions & 3 deletions cmd/templ/generatecmd/testwatch/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,14 +307,15 @@ loop:
}
}

func NewTestArgs(modRoot, appDir string, appPort int, proxyPort int) TestArgs {
func NewTestArgs(modRoot, appDir string, appPort int, proxyBind string, proxyPort int) TestArgs {
return TestArgs{
ModRoot: modRoot,
AppDir: appDir,
AppPort: appPort,
AppURL: fmt.Sprintf("http://localhost:%d", appPort),
ProxyBind: proxyBind,
ProxyPort: proxyPort,
ProxyURL: fmt.Sprintf("http://localhost:%d", proxyPort),
ProxyURL: fmt.Sprintf("http://%s:%d", proxyBind, proxyPort),
}
}

Expand All @@ -323,6 +324,7 @@ type TestArgs struct {
AppDir string
AppPort int
AppURL string
ProxyBind string
ProxyPort int
ProxyURL string
}
Expand All @@ -349,8 +351,9 @@ func Setup(gzipEncoding bool) (args TestArgs, teardown func(t *testing.T), err e
if err != nil {
return args, teardown, fmt.Errorf("failed to get available port: %v", err)
}
proxyBind := "localhost"

args = NewTestArgs(moduleRoot, appDir, appPort, proxyPort)
args = NewTestArgs(moduleRoot, appDir, appPort, proxyBind, proxyPort)
ctx, cancel := context.WithCancel(context.Background())

var wg sync.WaitGroup
Expand All @@ -369,6 +372,7 @@ func Setup(gzipEncoding bool) (args TestArgs, teardown func(t *testing.T), err e
Path: appDir,
Watch: true,
Command: command,
ProxyBind: proxyBind,
ProxyPort: proxyPort,
Proxy: args.AppURL,
IncludeVersion: false,
Expand Down
4 changes: 4 additions & 0 deletions cmd/templ/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ Args:
Set the URL to proxy after generating code and executing the command.
-proxyport
The port the proxy will listen on. (default 7331)
-proxybind
The address the proxy will listen on. (default 127.0.0.1)
-w
Number of workers to use when generating code. (default runtime.NumCPUs)
-pprof
Expand Down Expand Up @@ -127,6 +129,7 @@ func generateCmd(w io.Writer, args []string) (code int) {
cmdFlag := cmd.String("cmd", "", "")
proxyFlag := cmd.String("proxy", "", "")
proxyPortFlag := cmd.Int("proxyport", 7331, "")
proxyBindFlag := cmd.String("proxybind", "127.0.0.1", "")
workerCountFlag := cmd.Int("w", runtime.NumCPU(), "")
pprofPortFlag := cmd.Int("pprof", 0, "")
keepOrphanedFilesFlag := cmd.Bool("keep-orphaned-files", false, "")
Expand Down Expand Up @@ -160,6 +163,7 @@ func generateCmd(w io.Writer, args []string) (code int) {
Command: *cmdFlag,
Proxy: *proxyFlag,
ProxyPort: *proxyPortFlag,
ProxyBind: *proxyBindFlag,
WorkerCount: *workerCountFlag,
GenerateSourceMapVisualisations: *sourceMapVisualisationsFlag,
IncludeVersion: *includeVersionFlag,
Expand Down
2 changes: 2 additions & 0 deletions docs/docs/09-commands-and-tools/01-cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ Args:
Set the URL to proxy after generating code and executing the command.
-proxyport
The port the proxy will listen on. (default 7331)
-proxybind
The address the proxy will listen on. (default 127.0.0.1)
-w
Number of workers to use when generating code. (default runtime.NumCPUs)
-pprof
Expand Down
2 changes: 2 additions & 0 deletions docs/docs/09-commands-and-tools/03-hot-reload.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ Finally, to trigger your web browser to reload automatically (without pressing F

The `--proxy` argument starts a HTTP proxy which proxies requests to your app. For example, if your app runs on port 8080, you would use `--proxy="http://localhost:8080"`. The proxy inserts client-side JavaScript before the `</body>` tag that will cause the browser to reload the window when the app is restarted instead of you having to reload the page manually. Note that the html being served by the webserver MUST have a `<body>` tag, otherwise there will be no javascript injection thus making the browser not reload automatically.

By default, the proxy binds to `127.0.0.1`. You can use `--proxybind` to bind to another address, e.g., `--proxybind="0.0.0.0"`.

Altogether, to setup hot reload on an app that listens on port 8080, run the following.

```
Expand Down

0 comments on commit d45ff2e

Please sign in to comment.