switched to gin-gonic

This commit is contained in:
$(pass /github/name)
2024-07-11 12:28:39 +02:00
parent 20ed629e92
commit de88a603d5
11 changed files with 330 additions and 169 deletions

View File

@@ -1,17 +1,33 @@
package middlewares
import (
"GoMembership/pkg/logger"
"net/http"
"time"
"GoMembership/pkg/logger"
"github.com/gin-gonic/gin"
)
// LoggerMiddleware logs each incoming HTTP request
func LoggerMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
logger.Info.Printf("%s %s %s", r.Method, r.RequestURI, time.Since(start))
next.ServeHTTP(w, r)
})
// LoggerMiddleware logs the incoming requests.
func LoggerMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
startTime := time.Now()
// Process the request
c.Next()
// Calculate the latency
latency := time.Since(startTime)
// Get the status code
statusCode := c.Writer.Status()
// Log the details
logger.Info.Printf("| %3d | %13v | %15s | %-7s %#v\n",
statusCode,
latency,
c.ClientIP(),
c.Request.Method,
c.Request.URL.Path,
)
}
}