refactor(backend/handlers): use withRequestLogAttrs helper for logging
Replace manual IP logging using `uploadClientIP(r)` with the `withRequestLogAttrs` helper function in `manage.go`. This simplifies the log statements and standardizes the extraction of request-related attributes.
This commit is contained in:
@@ -31,7 +31,7 @@ func (a *App) ManageBox(w http.ResponseWriter, r *http.Request) {
|
|||||||
Description: "Delete this anonymous Warpbox upload.",
|
Description: "Delete this anonymous Warpbox upload.",
|
||||||
Data: a.managePageData(box, r.PathValue("token")),
|
Data: a.managePageData(box, r.PathValue("token")),
|
||||||
})
|
})
|
||||||
a.logger.Info("anonymous manage page viewed", "source", "anonymous-delete", "severity", "user_activity", "code", 2102, "box_id", box.ID, "ip", uploadClientIP(r))
|
a.logger.Info("anonymous manage page viewed", withRequestLogAttrs(r, "source", "anonymous-delete", "severity", "user_activity", "code", 2102, "box_id", box.ID)...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *App) ManageDeleteBox(w http.ResponseWriter, r *http.Request) {
|
func (a *App) ManageDeleteBox(w http.ResponseWriter, r *http.Request) {
|
||||||
@@ -41,11 +41,11 @@ func (a *App) ManageDeleteBox(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if err := a.uploadService.DeleteBoxWithToken(box.ID, r.PathValue("token")); err != nil {
|
if err := a.uploadService.DeleteBoxWithToken(box.ID, r.PathValue("token")); err != nil {
|
||||||
a.logger.Warn("anonymous delete failed", "source", "anonymous-delete", "severity", "warn", "code", 4102, "box_id", box.ID, "ip", uploadClientIP(r), "error", err.Error())
|
a.logger.Warn("anonymous delete failed", withRequestLogAttrs(r, "source", "anonymous-delete", "severity", "warn", "code", 4102, "box_id", box.ID, "error", err.Error())...)
|
||||||
http.NotFound(w, r)
|
http.NotFound(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
a.logger.Info("anonymous box deleted", "source", "anonymous-delete", "severity", "user_activity", "code", 2103, "box_id", box.ID, "ip", uploadClientIP(r))
|
a.logger.Info("anonymous box deleted", withRequestLogAttrs(r, "source", "anonymous-delete", "severity", "user_activity", "code", 2103, "box_id", box.ID)...)
|
||||||
http.Redirect(w, r, "/d/"+box.ID+"/deleted", http.StatusSeeOther)
|
http.Redirect(w, r, "/d/"+box.ID+"/deleted", http.StatusSeeOther)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -60,12 +60,12 @@ func (a *App) ManageDeleted(w http.ResponseWriter, r *http.Request) {
|
|||||||
func (a *App) loadManagedBox(w http.ResponseWriter, r *http.Request) (services.Box, bool) {
|
func (a *App) loadManagedBox(w http.ResponseWriter, r *http.Request) (services.Box, bool) {
|
||||||
box, err := a.uploadService.GetBox(r.PathValue("boxID"))
|
box, err := a.uploadService.GetBox(r.PathValue("boxID"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
a.logger.Warn("anonymous manage missing box", "source", "anonymous-delete", "severity", "warn", "code", 4103, "box_id", r.PathValue("boxID"), "ip", uploadClientIP(r))
|
a.logger.Warn("anonymous manage missing box", withRequestLogAttrs(r, "source", "anonymous-delete", "severity", "warn", "code", 4103, "box_id", r.PathValue("boxID"))...)
|
||||||
http.NotFound(w, r)
|
http.NotFound(w, r)
|
||||||
return services.Box{}, false
|
return services.Box{}, false
|
||||||
}
|
}
|
||||||
if !a.uploadService.VerifyDeleteToken(box, r.PathValue("token")) {
|
if !a.uploadService.VerifyDeleteToken(box, r.PathValue("token")) {
|
||||||
a.logger.Warn("anonymous manage invalid token", "source", "anonymous-delete", "severity", "warn", "code", 4104, "box_id", box.ID, "ip", uploadClientIP(r))
|
a.logger.Warn("anonymous manage invalid token", withRequestLogAttrs(r, "source", "anonymous-delete", "severity", "warn", "code", 4104, "box_id", box.ID)...)
|
||||||
http.NotFound(w, r)
|
http.NotFound(w, r)
|
||||||
return services.Box{}, false
|
return services.Box{}, false
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,64 +0,0 @@
|
|||||||
package middleware
|
|
||||||
|
|
||||||
import (
|
|
||||||
"log/slog"
|
|
||||||
"net/http"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"warpbox.dev/backend/libs/services"
|
|
||||||
)
|
|
||||||
|
|
||||||
type statusRecorder struct {
|
|
||||||
http.ResponseWriter
|
|
||||||
status int
|
|
||||||
bytes int
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *statusRecorder) WriteHeader(status int) {
|
|
||||||
r.status = status
|
|
||||||
r.ResponseWriter.WriteHeader(status)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *statusRecorder) Write(data []byte) (int, error) {
|
|
||||||
if r.status == 0 {
|
|
||||||
r.status = http.StatusOK
|
|
||||||
}
|
|
||||||
n, err := r.ResponseWriter.Write(data)
|
|
||||||
r.bytes += n
|
|
||||||
return n, err
|
|
||||||
}
|
|
||||||
|
|
||||||
func Logger(logger *slog.Logger) Middleware {
|
|
||||||
return func(next http.Handler) http.Handler {
|
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
start := time.Now()
|
|
||||||
recorder := &statusRecorder{ResponseWriter: w}
|
|
||||||
|
|
||||||
next.ServeHTTP(recorder, r)
|
|
||||||
|
|
||||||
status := recorder.status
|
|
||||||
if status == 0 {
|
|
||||||
status = http.StatusOK
|
|
||||||
}
|
|
||||||
ip, ok := services.ClientIPFromContext(r)
|
|
||||||
if !ok {
|
|
||||||
ip = services.ClientIP(r.RemoteAddr, r.Header.Get("X-Forwarded-For"), r.Header.Get("X-Real-IP"), nil)
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.Info("http request",
|
|
||||||
"source", "http",
|
|
||||||
"severity", "dev",
|
|
||||||
"code", status,
|
|
||||||
"method", r.Method,
|
|
||||||
"path", r.URL.Path,
|
|
||||||
"status", status,
|
|
||||||
"bytes", recorder.bytes,
|
|
||||||
"duration_ms", time.Since(start).Milliseconds(),
|
|
||||||
"request_id", RequestIDFromContext(r.Context()),
|
|
||||||
"ip", ip,
|
|
||||||
"remote_addr", r.RemoteAddr,
|
|
||||||
"user_agent", r.UserAgent(),
|
|
||||||
)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user