package audit import ( "github.com/labstack/echo/v4" ) // RequestMetaMiddleware injects request metadata into the request context for audit logging. func RequestMetaMiddleware() echo.MiddlewareFunc { return func(next echo.HandlerFunc) echo.HandlerFunc { return func(c echo.Context) error { requestID := c.Response().Header().Get(echo.HeaderXRequestID) if requestID == "" { requestID = c.Request().Header.Get(echo.HeaderXRequestID) } ctx := WithRequestMeta(c.Request().Context(), RequestMeta{ IP: c.RealIP(), RequestID: requestID, UserAgent: c.Request().UserAgent(), }) c.SetRequest(c.Request().WithContext(ctx)) return next(c) } } } // EnrichEchoContext copies Echo actor values into the Go context when present. func EnrichEchoContext(c echo.Context, actorType string) { actorID := "" if actorType == ActorTypeAdmin { if id, ok := c.Get("user_id").(string); ok { actorID = id } } if actorType == ActorTypeCustomer { if id, ok := c.Get("customer_id").(string); ok { actorID = id } } ctx := c.Request().Context() if actorID != "" { ctx = WithActor(ctx, Actor{ID: actorID, Type: actorType}) } c.SetRequest(c.Request().WithContext(ctx)) }