fix: pdate audit trails exclude
This commit is contained in:
parent
dda6f6fea6
commit
840acca7fe
|
|
@ -2,19 +2,31 @@ package middleware
|
|||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"gorm.io/gorm"
|
||||
"log"
|
||||
"netidhub-saas-be/app/database/entity"
|
||||
utilSvc "netidhub-saas-be/utils/service"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func AuditTrailsMiddleware(db *gorm.DB) fiber.Handler {
|
||||
return func(c *fiber.Ctx) error {
|
||||
start := time.Now()
|
||||
requestBody := c.Body()
|
||||
|
||||
// Get content type to determine if we should store request/response body
|
||||
contentType := c.Get("Content-Type")
|
||||
isJSONContent := strings.Contains(strings.ToLower(contentType), "application/json")
|
||||
|
||||
var requestBody string
|
||||
var responseBody string
|
||||
|
||||
// Only store request body for JSON content types
|
||||
if isJSONContent {
|
||||
requestBody = string(c.Body())
|
||||
}
|
||||
|
||||
headersMap := c.GetReqHeaders()
|
||||
headersJSON, _ := json.Marshal(headersMap)
|
||||
|
|
@ -24,6 +36,11 @@ func AuditTrailsMiddleware(db *gorm.DB) fiber.Handler {
|
|||
|
||||
err := c.Next()
|
||||
|
||||
// Only store response body for JSON content types
|
||||
if isJSONContent {
|
||||
responseBody = string(c.Response().Body())
|
||||
}
|
||||
|
||||
audit := entity.AuditTrails{
|
||||
Method: c.Method(),
|
||||
Path: c.OriginalURL(),
|
||||
|
|
@ -31,8 +48,8 @@ func AuditTrailsMiddleware(db *gorm.DB) fiber.Handler {
|
|||
Status: c.Response().StatusCode(),
|
||||
UserID: userId,
|
||||
RequestHeaders: string(headersJSON),
|
||||
RequestBody: string(requestBody),
|
||||
ResponseBody: string(c.Response().Body()),
|
||||
RequestBody: requestBody,
|
||||
ResponseBody: responseBody,
|
||||
DurationMs: time.Since(start).Milliseconds(),
|
||||
CreatedAt: time.Now(),
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue