2022-12-31 10:22:38 +01:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
2023-03-14 22:20:56 +01:00
|
|
|
"bytes"
|
2023-05-22 19:31:54 +02:00
|
|
|
"io"
|
|
|
|
|
2022-12-31 10:22:38 +01:00
|
|
|
"github.com/gin-gonic/gin"
|
2023-01-30 19:59:42 +01:00
|
|
|
"github.com/twofas/2fas-server/internal/common/logging"
|
2022-12-31 10:22:38 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func RequestJsonLogger() gin.HandlerFunc {
|
|
|
|
return func(c *gin.Context) {
|
2023-03-14 22:20:56 +01:00
|
|
|
var buf bytes.Buffer
|
|
|
|
|
|
|
|
tee := io.TeeReader(c.Request.Body, &buf)
|
|
|
|
body, _ := io.ReadAll(tee)
|
|
|
|
|
|
|
|
c.Request.Body = io.NopCloser(&buf)
|
|
|
|
|
|
|
|
logging.WithFields(logging.Fields{
|
2022-12-31 10:22:38 +01:00
|
|
|
"method": c.Request.Method,
|
|
|
|
"path": c.Request.URL.Path,
|
2023-03-14 22:20:56 +01:00
|
|
|
"headers": c.Request.Header,
|
|
|
|
"body": string(body),
|
2022-12-31 10:22:38 +01:00
|
|
|
"request_id": c.GetString(RequestIdKey),
|
|
|
|
"correlation_id": c.GetString(CorrelationIdKey),
|
2023-03-14 22:20:56 +01:00
|
|
|
}).Info("Request")
|
2022-12-31 10:22:38 +01:00
|
|
|
|
|
|
|
c.Next()
|
|
|
|
|
2023-03-14 22:20:56 +01:00
|
|
|
logging.WithFields(logging.Fields{
|
|
|
|
"method": c.Request.Method,
|
|
|
|
"path": c.Request.URL.Path,
|
|
|
|
"request_id": c.GetString(RequestIdKey),
|
|
|
|
"correlation_id": c.GetString(CorrelationIdKey),
|
|
|
|
}).Info("Response")
|
2022-12-31 10:22:38 +01:00
|
|
|
}
|
|
|
|
}
|