feat(logger): implement a logger middleware

This commit is contained in:
Julien Dessaux 2024-10-01 08:47:32 +02:00
parent 4ff490806c
commit 2cdbc4e782
Signed by: adyxax
GPG key ID: F92E51B86E07177E
4 changed files with 117 additions and 1 deletions

46
pkg/logger/body_writer.go Normal file
View file

@ -0,0 +1,46 @@
package logger
import (
"bufio"
"errors"
"net"
"net/http"
)
type bodyWriter struct {
http.ResponseWriter
status int
}
func newBodyWriter(writer http.ResponseWriter) *bodyWriter {
return &bodyWriter{
ResponseWriter: writer,
status: http.StatusNotImplemented,
}
}
// implements http.ResponseWriter
func (w *bodyWriter) Write(b []byte) (int, error) {
return w.ResponseWriter.Write(b)
}
// implements http.ResponseWriter
func (w *bodyWriter) WriteHeader(code int) {
w.status = code
w.ResponseWriter.WriteHeader(code)
}
// implements http.Flusher
func (w *bodyWriter) Flush() {
if f, ok := w.ResponseWriter.(http.Flusher); ok {
f.Flush()
}
}
// implements http.Hijacker
func (w *bodyWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
if hi, ok := w.ResponseWriter.(http.Hijacker); ok {
return hi.Hijack()
}
return nil, nil, errors.New("Hijack not supported")
}