63 lines
1.3 KiB
Go
63 lines
1.3 KiB
Go
|
package apis
|
||
|
|
||
|
import (
|
||
|
"log/slog"
|
||
|
"net"
|
||
|
"net/http"
|
||
|
"strings"
|
||
|
"time"
|
||
|
|
||
|
"github.com/go-chi/chi/v5/middleware"
|
||
|
)
|
||
|
|
||
|
// ClientIP getIP returns the ip address from the http request
|
||
|
func ClientIP(r *http.Request) string {
|
||
|
xForwardedFor := r.Header.Get("X-Forwarded-For")
|
||
|
ip := strings.TrimSpace(strings.Split(xForwardedFor, ",")[0])
|
||
|
if ip != "" {
|
||
|
return ip
|
||
|
}
|
||
|
|
||
|
ip = strings.TrimSpace(r.Header.Get("X-Real-Ip"))
|
||
|
if ip != "" {
|
||
|
return ip
|
||
|
}
|
||
|
|
||
|
if ip, _, err := net.SplitHostPort(strings.TrimSpace(r.RemoteAddr)); err == nil {
|
||
|
return ip
|
||
|
}
|
||
|
|
||
|
return ""
|
||
|
}
|
||
|
|
||
|
// Logger returns a `func(http.Handler) http.Handler` (middleware) that logs requests using slog.
|
||
|
func Logger(next http.Handler) http.Handler {
|
||
|
fn := func(w http.ResponseWriter, r *http.Request) {
|
||
|
st := time.Now()
|
||
|
ww := middleware.NewWrapResponseWriter(w, r.ProtoMajor)
|
||
|
|
||
|
defer func() {
|
||
|
msg := r.Method + " " + r.RequestURI + " " + r.Proto
|
||
|
|
||
|
ip := ClientIP(r)
|
||
|
status := ww.Status()
|
||
|
if status == 0 {
|
||
|
status = http.StatusOK
|
||
|
}
|
||
|
|
||
|
attrs := []slog.Attr{
|
||
|
slog.String("ip", ip),
|
||
|
slog.String("id", middleware.GetReqID(r.Context())),
|
||
|
slog.Int("status", status),
|
||
|
slog.Duration("latency", time.Since(st)),
|
||
|
slog.Int("length", ww.BytesWritten()),
|
||
|
}
|
||
|
slog.LogAttrs(r.Context(), slog.LevelInfo, msg, attrs...)
|
||
|
}()
|
||
|
|
||
|
next.ServeHTTP(ww, r)
|
||
|
}
|
||
|
|
||
|
return http.HandlerFunc(fn)
|
||
|
}
|