/
AngelCareMe
/
task_queue
Обзор
Документация
Войти
/
AngelCareMe
/
task_queue
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
internal/handler/middleware/middleware.go
66 строк
1 KB
AngelCareMe
done
28 июл 2025, 21:07
28 июл 2025, 21:07
c70014b
Код
Авторство
О чём код?
package middleware import ( "task-queue/pkg/logger" "time" "github.com/gin-gonic/gin" "github.com/sirupsen/logrus" ) func Logging() gin.HandlerFunc { logger := logger.Get() return func(c *gin.Context) { start := time.Now() path := c.Request.URL.Path raw := c.Request.URL.RawQuery // process request c.Next() // log after request endTime := time.Now() latency := endTime.Sub(start) clientIP := c.ClientIP() method := c.Request.Method statusCode := c.Writer.Status() errorMessage := c.Errors.ByType(gin.ErrorTypePrivate).String() if raw != "" { path = path + "?" + raw } entry := logger.WithFields(logrus.Fields{ "status_code": statusCode, "latency": latency, "client_ip": clientIP, "method": method, "path": path, "error": errorMessage, "request_time": endTime.Format(time.RFC3339), }) if len(c.Errors) > 0 { entry.Error("http request") } else { entry.Info("http request") } } } func CORS() gin.HandlerFunc { return func(c *gin.Context) { c.Header("Access-Control-Allow-Origin", "*") c.Header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS") c.Header("Access-Control-Allow-Headers", "Origin, Content-Type, Accept, Authorization") if c.Request.Method == "OPTIONS" { c.AbortWithStatus(204) return } c.Next() } }