Skip to content

Commit

Permalink
Addressed feedback from kwilczynski, thank you!
Browse files Browse the repository at this point in the history
  • Loading branch information
AxelTheGerman committed Nov 24, 2024
1 parent 04309c5 commit a343f4d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
13 changes: 6 additions & 7 deletions internal/server/request_start_middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import (
"time"
)

const (
requestStartHeader = "X-Request-Start"
)

type RequestStartMiddleware struct {
next http.Handler
}
Expand All @@ -17,13 +21,8 @@ func WithRequestStartMiddleware(next http.Handler) http.Handler {
}

func (h *RequestStartMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("X-Request-Start") == "" {
r.Header.Set("X-Request-Start", h.nowStr())
if r.Header.Get(requestStartHeader) == "" {
r.Header.Set(requestStartHeader, strconv.FormatInt(time.Now().UnixMilli(), 10))
}
h.next.ServeHTTP(w, r)
}

func (h *RequestStartMiddleware) nowStr() string {
var now = time.Now().UnixMilli()
return strconv.FormatInt(now, 10)
}
10 changes: 5 additions & 5 deletions internal/server/request_start_middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (

func TestRequestStartMiddleware_AddsUnixMilliWhenNotPresent(t *testing.T) {
handler := WithRequestStartMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
id := r.Header.Get("X-Request-Start")
assert.NotEmpty(t, id)
timestamp := r.Header.Get(requestStartHeader)
assert.NotEmpty(t, timestamp)
}))

r := httptest.NewRequest("GET", "/", nil)
Expand All @@ -23,13 +23,13 @@ func TestRequestStartMiddleware_AddsUnixMilliWhenNotPresent(t *testing.T) {

func TestRequestStartMiddleware_PreservesExistingHeaderWhenPresent(t *testing.T) {
next := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
id := r.Header.Get("X-Request-Start")
assert.Equal(t, "1234", id)
timestamp := r.Header.Get(requestStartHeader)
assert.Equal(t, "1234", timestamp)
})
handler := WithRequestStartMiddleware(next)

r := httptest.NewRequest("GET", "/", nil)
r.Header.Set("X-Request-Start", "1234")
r.Header.Set(requestStartHeader, "1234")

w := httptest.NewRecorder()
handler.ServeHTTP(w, r)
Expand Down
3 changes: 2 additions & 1 deletion internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,12 @@ func (s *Server) startCommandHandler() error {
func (s *Server) buildHandler() http.Handler {
var handler http.Handler

// Note: handlers are executed in the inverse order.
handler = s.router
handler = WithRequestStartMiddleware(handler)
handler, _ = WithErrorPageMiddleware(pages.DefaultErrorPages, true, handler)
handler = WithLoggingMiddleware(slog.Default(), s.config.HttpPort, s.config.HttpsPort, handler)
handler = WithRequestIDMiddleware(handler)
handler = WithRequestStartMiddleware(handler)

return handler
}

0 comments on commit a343f4d

Please sign in to comment.