mirror of
https://github.com/lordmathis/lemma.git
synced 2025-11-06 07:54:22 +00:00
Add logging to context
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"novamd/internal/logging"
|
||||||
"novamd/internal/models"
|
"novamd/internal/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -28,10 +29,22 @@ type HandlerContext struct {
|
|||||||
Workspace *models.Workspace // Optional, only set for workspace routes
|
Workspace *models.Workspace // Optional, only set for workspace routes
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var logger logging.Logger
|
||||||
|
|
||||||
|
func getLogger() logging.Logger {
|
||||||
|
if logger == nil {
|
||||||
|
logger = logging.WithGroup("context")
|
||||||
|
}
|
||||||
|
return logger
|
||||||
|
}
|
||||||
|
|
||||||
// GetRequestContext retrieves the handler context from the request
|
// GetRequestContext retrieves the handler context from the request
|
||||||
func GetRequestContext(w http.ResponseWriter, r *http.Request) (*HandlerContext, bool) {
|
func GetRequestContext(w http.ResponseWriter, r *http.Request) (*HandlerContext, bool) {
|
||||||
ctx := r.Context().Value(HandlerContextKey)
|
ctx := r.Context().Value(HandlerContextKey)
|
||||||
if ctx == nil {
|
if ctx == nil {
|
||||||
|
getLogger().Error("missing handler context in request",
|
||||||
|
"path", r.URL.Path,
|
||||||
|
"method", r.Method)
|
||||||
http.Error(w, "Internal server error", http.StatusInternalServerError)
|
http.Error(w, "Internal server error", http.StatusInternalServerError)
|
||||||
return nil, false
|
return nil, false
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,6 +13,9 @@ func WithUserContextMiddleware(next http.Handler) http.Handler {
|
|||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
claims, err := GetUserFromContext(r.Context())
|
claims, err := GetUserFromContext(r.Context())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
getLogger().Error("failed to get user from context",
|
||||||
|
"error", err,
|
||||||
|
"path", r.URL.Path)
|
||||||
http.Error(w, "Unauthorized", http.StatusUnauthorized)
|
http.Error(w, "Unauthorized", http.StatusUnauthorized)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -22,6 +25,11 @@ func WithUserContextMiddleware(next http.Handler) http.Handler {
|
|||||||
UserRole: claims.Role,
|
UserRole: claims.Role,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getLogger().Debug("user context extracted from claims",
|
||||||
|
"userID", claims.UserID,
|
||||||
|
"role", claims.Role,
|
||||||
|
"path", r.URL.Path)
|
||||||
|
|
||||||
r = WithHandlerContext(r, hctx)
|
r = WithHandlerContext(r, hctx)
|
||||||
next.ServeHTTP(w, r)
|
next.ServeHTTP(w, r)
|
||||||
})
|
})
|
||||||
@@ -39,11 +47,21 @@ func WithWorkspaceContextMiddleware(db db.WorkspaceReader) func(http.Handler) ht
|
|||||||
workspaceName := chi.URLParam(r, "workspaceName")
|
workspaceName := chi.URLParam(r, "workspaceName")
|
||||||
workspace, err := db.GetWorkspaceByName(ctx.UserID, workspaceName)
|
workspace, err := db.GetWorkspaceByName(ctx.UserID, workspaceName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "Workspace not found", http.StatusNotFound)
|
getLogger().Error("failed to get workspace",
|
||||||
|
"error", err,
|
||||||
|
"userID", ctx.UserID,
|
||||||
|
"workspace", workspaceName,
|
||||||
|
"path", r.URL.Path)
|
||||||
|
http.Error(w, "Failed to get workspace", http.StatusNotFound)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update existing context with workspace
|
getLogger().Debug("workspace context added",
|
||||||
|
"userID", ctx.UserID,
|
||||||
|
"workspaceID", workspace.ID,
|
||||||
|
"workspaceName", workspace.Name,
|
||||||
|
"path", r.URL.Path)
|
||||||
|
|
||||||
ctx.Workspace = workspace
|
ctx.Workspace = workspace
|
||||||
r = WithHandlerContext(r, ctx)
|
r = WithHandlerContext(r, ctx)
|
||||||
next.ServeHTTP(w, r)
|
next.ServeHTTP(w, r)
|
||||||
|
|||||||
Reference in New Issue
Block a user