Skip to content
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

refactor: add extra logging #741

Merged
merged 3 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.2.696
0.2.697
12 changes: 11 additions & 1 deletion cmd/templ/generatecmd/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,13 @@ func (pwc passthroughWriteCloser) Close() error {
const unsupportedContentEncoding = "Unsupported content encoding, hot reload script not inserted."

func (h *Handler) modifyResponse(r *http.Response) error {
log := h.log.With(slog.String("url", r.Request.URL.String()))
if r.Header.Get("templ-skip-modify") == "true" {
log.Debug("Skipping response modification because templ-skip-modify header is set")
return nil
}
if contentType := r.Header.Get("Content-Type"); !strings.HasPrefix(contentType, "text/html") {
log.Debug("Skipping response modification because content type is not text/html", slog.String("content-type", contentType))
return nil
}

Expand All @@ -80,7 +83,7 @@ func (h *Handler) modifyResponse(r *http.Response) error {
return brotli.NewWriter(out)
}
case "":
// No content encoding.
log.Debug("No content encoding header found")
default:
h.log.Warn(unsupportedContentEncoding, slog.String("encoding", r.Header.Get("Content-Encoding")))
}
Expand All @@ -98,6 +101,13 @@ func (h *Handler) modifyResponse(r *http.Response) error {

// Update it.
updated := insertScriptTagIntoBody(string(body))
if log.Enabled(r.Request.Context(), slog.LevelDebug) {
if len(updated) == len(body) {
log.Debug("Reload script not inserted")
} else {
log.Debug("Reload script inserted")
}
}

// Encode the response.
var buf bytes.Buffer
Expand Down
62 changes: 57 additions & 5 deletions cmd/templ/generatecmd/proxy/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ func TestProxy(t *testing.T) {
r := &http.Response{
Body: io.NopCloser(strings.NewReader(`{"key": "value"}`)),
Header: make(http.Header),
Request: &http.Request{
URL: &url.URL{
Scheme: "http",
Host: "example.com",
},
},
}
r.Header.Set("Content-Type", "application/json")
r.Header.Set("Content-Length", "16")
Expand Down Expand Up @@ -84,6 +90,12 @@ func TestProxy(t *testing.T) {
r := &http.Response{
Body: io.NopCloser(strings.NewReader(`Hello`)),
Header: make(http.Header),
Request: &http.Request{
URL: &url.URL{
Scheme: "http",
Host: "example.com",
},
},
}
r.Header.Set("Content-Type", "text/html")
r.Header.Set("Content-Length", "5")
Expand Down Expand Up @@ -114,6 +126,12 @@ func TestProxy(t *testing.T) {
r := &http.Response{
Body: io.NopCloser(strings.NewReader(`<html><body></body></html>`)),
Header: make(http.Header),
Request: &http.Request{
URL: &url.URL{
Scheme: "http",
Host: "example.com",
},
},
}
r.Header.Set("Content-Type", "text/html, charset=utf-8")
r.Header.Set("Content-Length", "26")
Expand Down Expand Up @@ -148,6 +166,12 @@ func TestProxy(t *testing.T) {
r := &http.Response{
Body: io.NopCloser(strings.NewReader(`{"key": "value"}`)),
Header: make(http.Header),
Request: &http.Request{
URL: &url.URL{
Scheme: "http",
Host: "example.com",
},
},
}
r.Header.Set("Content-Type", "application/json")
// It's not actually gzipped here, but it doesn't matter, it shouldn't get that far.
Expand Down Expand Up @@ -200,6 +224,12 @@ func TestProxy(t *testing.T) {
r := &http.Response{
Body: io.NopCloser(&buf),
Header: make(http.Header),
Request: &http.Request{
URL: &url.URL{
Scheme: "http",
Host: "example.com",
},
},
}
r.Header.Set("Content-Type", "text/html, charset=utf-8")
r.Header.Set("Content-Encoding", "gzip")
Expand Down Expand Up @@ -255,6 +285,12 @@ func TestProxy(t *testing.T) {
r := &http.Response{
Body: io.NopCloser(&buf),
Header: make(http.Header),
Request: &http.Request{
URL: &url.URL{
Scheme: "http",
Host: "example.com",
},
},
}
r.Header.Set("Content-Type", "text/html, charset=utf-8")
r.Header.Set("Content-Encoding", "br")
Expand Down Expand Up @@ -375,12 +411,18 @@ func TestProxy(t *testing.T) {
r := &http.Response{
Body: io.NopCloser(bytes.NewReader([]byte("<p>Data</p>"))),
Header: make(http.Header),
Request: &http.Request{
URL: &url.URL{
Scheme: "http",
Host: "example.com",
},
},
}
r.Header.Set("Content-Type", "text/html, charset=utf-8")
r.Header.Set("Content-Encoding", "weird-encoding")

// Act
lh := newTestLogHandler()
lh := newTestLogHandler(slog.LevelInfo)
log := slog.New(lh)
h := New(log, "127.0.0.1", 7474, &url.URL{Scheme: "http", Host: "example.com"})
err := h.modifyResponse(r)
Expand All @@ -390,7 +432,12 @@ func TestProxy(t *testing.T) {

// Assert
if len(lh.records) != 1 {
t.Fatalf("expected 1 log entry, but got %d", len(lh.records))
var sb strings.Builder
for _, record := range lh.records {
sb.WriteString(record.Message)
sb.WriteString("\n")
}
t.Fatalf("expected 1 log entry, but got %d: \n%s", len(lh.records), sb.String())
}
record := lh.records[0]
if record.Message != unsupportedContentEncoding {
Expand All @@ -402,25 +449,30 @@ func TestProxy(t *testing.T) {
})
}

func newTestLogHandler() *testLogHandler {
func newTestLogHandler(level slog.Level) *testLogHandler {
return &testLogHandler{
m: new(sync.Mutex),
records: nil,
level: level,
}
}

type testLogHandler struct {
m *sync.Mutex
records []slog.Record
level slog.Level
}

func (h *testLogHandler) Enabled(context.Context, slog.Level) bool {
return true
func (h *testLogHandler) Enabled(ctx context.Context, l slog.Level) bool {
return l >= h.level
}

func (h *testLogHandler) Handle(ctx context.Context, r slog.Record) error {
h.m.Lock()
defer h.m.Unlock()
if r.Level < h.level {
return nil
}
h.records = append(h.records, r)
return nil
}
Expand Down
Loading