From dd6d1c9b32c6d8ae3389f709d21498602a006603 Mon Sep 17 00:00:00 2001 From: Maxim Slipenko Date: Mon, 16 Dec 2024 19:34:51 +0300 Subject: [PATCH] feat: create logger middleware --- internal/app/app.go | 2 + internal/controllers/taskcontroller/upload.go | 2 +- internal/logger/logger.go | 10 ++-- internal/middlewares/logger.go | 46 +++++++++++++++++++ internal/router/router.go | 9 ++-- 5 files changed, 59 insertions(+), 10 deletions(-) create mode 100644 internal/middlewares/logger.go diff --git a/internal/app/app.go b/internal/app/app.go index 08f3f66..0a39477 100644 --- a/internal/app/app.go +++ b/internal/app/app.go @@ -121,6 +121,8 @@ func (app *App) Run() { }, ) + app.repo.ForceUpdate() + err := http.ListenAndServe(fmt.Sprintf(":%d", app.config.Port), app.router.Setup()) if err != nil { panic(err) diff --git a/internal/controllers/taskcontroller/upload.go b/internal/controllers/taskcontroller/upload.go index 7d4c2eb..335a7d8 100644 --- a/internal/controllers/taskcontroller/upload.go +++ b/internal/controllers/taskcontroller/upload.go @@ -24,7 +24,7 @@ func (rd *TaskUploadResponse) Render(w http.ResponseWriter, r *http.Request) err // // @Summary Upload files to a task // @Description Upload multiple files associated with a specific task ID. Each file must be less than 10MB. -// @Tags tasks +// @Tags Tasks // @Accept multipart/form-data // @Produce json // @Param taskID path string true "Task ID" diff --git a/internal/logger/logger.go b/internal/logger/logger.go index 580987f..edce443 100644 --- a/internal/logger/logger.go +++ b/internal/logger/logger.go @@ -11,11 +11,11 @@ var ( // Logger определяет интерфейс для логгера type Logger interface { - Debug(msg string, fields map[string]interface{}) - Info(msg string, fields map[string]interface{}) - Warn(msg string, fields map[string]interface{}) - Error(msg string, fields map[string]interface{}) - Fatal(msg string, fields map[string]interface{}) + Debug(msg string, fields ...map[string]interface{}) + Info(msg string, fields ...map[string]interface{}) + Warn(msg string, fields ...map[string]interface{}) + Error(msg string, fields ...map[string]interface{}) + Fatal(msg string, fields ...map[string]interface{}) } func GetLogger() *ZapLogger { diff --git a/internal/middlewares/logger.go b/internal/middlewares/logger.go new file mode 100644 index 0000000..1c2e524 --- /dev/null +++ b/internal/middlewares/logger.go @@ -0,0 +1,46 @@ +package middlewares + +import ( + "net/http" + + "code.alt-gnome.ru/aides-infra/aides-repo-api/internal/logger" +) + +// CustomResponseWriter wraps http.ResponseWriter to capture status code and response size +type CustomResponseWriter struct { + http.ResponseWriter + StatusCode int + BytesWritten int +} + +// WriteHeader captures the status code and writes it to the response +func (w *CustomResponseWriter) WriteHeader(statusCode int) { + w.StatusCode = statusCode + w.ResponseWriter.WriteHeader(statusCode) +} + +// Write captures the size of the response body +func (w *CustomResponseWriter) Write(data []byte) (int, error) { + bytesWritten, err := w.ResponseWriter.Write(data) + w.BytesWritten += bytesWritten + return bytesWritten, err +} + +func LoggerMiddleware(logger logger.Logger) func(http.Handler) http.Handler { + return func(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + customWriter := &CustomResponseWriter{ + ResponseWriter: w, + StatusCode: http.StatusOK, // Default status if WriteHeader is not called + } + next.ServeHTTP(customWriter, r) + logger.Info("request", map[string]interface{}{ + "path": r.URL.Path, + "method": r.Method, + "ip": r.RemoteAddr, + "status_code": customWriter.StatusCode, + "response_size": customWriter.BytesWritten, + }) + }) + } +} diff --git a/internal/router/router.go b/internal/router/router.go index b85b9e5..942b2e1 100644 --- a/internal/router/router.go +++ b/internal/router/router.go @@ -2,12 +2,12 @@ package router import ( "github.com/go-chi/chi/v5" - "github.com/go-chi/chi/v5/middleware" httpSwagger "github.com/swaggo/http-swagger" _ "code.alt-gnome.ru/aides-infra/aides-repo-api/docs" "code.alt-gnome.ru/aides-infra/aides-repo-api/internal/config" "code.alt-gnome.ru/aides-infra/aides-repo-api/internal/controllers/taskcontroller" + "code.alt-gnome.ru/aides-infra/aides-repo-api/internal/logger" "code.alt-gnome.ru/aides-infra/aides-repo-api/internal/middlewares" ) @@ -27,10 +27,11 @@ func New( } func (r *Router) Setup() *chi.Mux { - router := chi.NewRouter() - router.Use(middleware.Logger) - authGuard := middlewares.CreateAuthGuard(r.config) + logger := middlewares.LoggerMiddleware(logger.GetLogger()) + + router := chi.NewRouter() + router.Use(logger) router.Get("/swagger/*", httpSwagger.WrapHandler)