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