Skip to content

Commit

Permalink
fix: templ CLI panic with --proxy option (#365)
Browse files Browse the repository at this point in the history
Co-authored-by: Adrian Hesketh <adrianhesketh@hushmail.com>
  • Loading branch information
eussam and a-h authored Dec 30, 2023
1 parent 4580762 commit f0fb971
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.2.507
0.2.508
25 changes: 19 additions & 6 deletions cmd/templ/generatecmd/proxy/proxy.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package proxy

import (
"bytes"
"fmt"
"io"
"log"
Expand Down Expand Up @@ -89,17 +90,29 @@ type roundTripper struct {
}

func (rt *roundTripper) RoundTrip(r *http.Request) (*http.Response, error) {
var err error
// Read and buffer the body.
var bodyBytes []byte
if r.Body != nil && r.Body != http.NoBody {
var err error
bodyBytes, err = io.ReadAll(r.Body)
if err != nil {
return nil, err
}
r.Body.Close()
}

// Retry logic.
var resp *http.Response
var err error
for retries := 0; retries < rt.maxRetries; retries++ {
// Clone the request and set the body.
req := r.Clone(r.Context())

req.Body, err = r.GetBody()
if err != nil {
return nil, err
if bodyBytes != nil {
req.Body = io.NopCloser(bytes.NewReader(bodyBytes))
}

resp, err := http.DefaultTransport.RoundTrip(req)
// Execute the request.
resp, err = http.DefaultTransport.RoundTrip(req)
if err != nil {
time.Sleep(rt.initialDelay * time.Duration(math.Pow(rt.backoffExponent, float64(retries))))
continue
Expand Down

0 comments on commit f0fb971

Please sign in to comment.