2024-02-12 19:38:03 +00:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
2024-02-27 20:38:59 +00:00
|
|
|
"github.com/pikami/cosmium/internal/logger"
|
2024-02-12 19:38:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func RequestLogger() gin.HandlerFunc {
|
|
|
|
return func(c *gin.Context) {
|
|
|
|
buf, _ := io.ReadAll(c.Request.Body)
|
|
|
|
rdr1 := io.NopCloser(bytes.NewBuffer(buf))
|
|
|
|
rdr2 := io.NopCloser(bytes.NewBuffer(buf))
|
|
|
|
|
2024-02-13 19:22:55 +00:00
|
|
|
bodyStr := readBody(rdr1)
|
|
|
|
if bodyStr != "" {
|
2024-02-27 20:38:59 +00:00
|
|
|
logger.Debug(bodyStr)
|
2024-02-13 19:22:55 +00:00
|
|
|
}
|
2024-02-12 19:38:03 +00:00
|
|
|
|
|
|
|
c.Request.Body = rdr2
|
|
|
|
c.Next()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func readBody(reader io.Reader) string {
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
buf.ReadFrom(reader)
|
|
|
|
|
|
|
|
s := buf.String()
|
|
|
|
return s
|
|
|
|
}
|