Skip to content

Commit

Permalink
feat: Add logging middleware for better debugging.
Browse files Browse the repository at this point in the history
  • Loading branch information
kaiwalyajoshi committed May 15, 2023
1 parent 5946146 commit 2e51c4e
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions pkg/proxy/server.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package proxy

import (
"fmt"
"log"
"net/http"

Expand All @@ -22,7 +23,32 @@ func New(cfg *rest.Config, b bundle.Bundle, rr rewriter.ResourceRewriter) http.H
proxyHandler.ModifyResponse = proxyModifyResponse(rr) //nolint:bodyclose // false positive

r := mux.NewRouter()
r.Use(loggingMiddleware)
r.Handle("/api/v1/namespaces/{namespace}/pods/{pod}/log", LogsHandler(b))
r.PathPrefix("/").Handler(proxyHandler)
return r
}

type LogRecorder struct {
http.ResponseWriter
status int
}

func (r *LogRecorder) Write(p []byte) (int, error) {
return r.ResponseWriter.Write(p)
}

func (r *LogRecorder) WriteHeader(status int) {
r.status = status
r.ResponseWriter.WriteHeader(status)
}

func loggingMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
writerWrap := &LogRecorder{
ResponseWriter: w,
}
next.ServeHTTP(writerWrap, r)
fmt.Println(writerWrap.status, r.Method, r.RequestURI)
})
}

0 comments on commit 2e51c4e

Please sign in to comment.