feat: create logger middleware
This commit is contained in:
parent
dc4f17952e
commit
dd6d1c9b32
5 changed files with 59 additions and 10 deletions
|
@ -121,6 +121,8 @@ func (app *App) Run() {
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
app.repo.ForceUpdate()
|
||||||
|
|
||||||
err := http.ListenAndServe(fmt.Sprintf(":%d", app.config.Port), app.router.Setup())
|
err := http.ListenAndServe(fmt.Sprintf(":%d", app.config.Port), app.router.Setup())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
|
|
|
@ -24,7 +24,7 @@ func (rd *TaskUploadResponse) Render(w http.ResponseWriter, r *http.Request) err
|
||||||
//
|
//
|
||||||
// @Summary Upload files to a task
|
// @Summary Upload files to a task
|
||||||
// @Description Upload multiple files associated with a specific task ID. Each file must be less than 10MB.
|
// @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
|
// @Accept multipart/form-data
|
||||||
// @Produce json
|
// @Produce json
|
||||||
// @Param taskID path string true "Task ID"
|
// @Param taskID path string true "Task ID"
|
||||||
|
|
|
@ -11,11 +11,11 @@ var (
|
||||||
|
|
||||||
// Logger определяет интерфейс для логгера
|
// Logger определяет интерфейс для логгера
|
||||||
type Logger interface {
|
type Logger interface {
|
||||||
Debug(msg string, fields map[string]interface{})
|
Debug(msg string, fields ...map[string]interface{})
|
||||||
Info(msg string, fields map[string]interface{})
|
Info(msg string, fields ...map[string]interface{})
|
||||||
Warn(msg string, fields map[string]interface{})
|
Warn(msg string, fields ...map[string]interface{})
|
||||||
Error(msg string, fields map[string]interface{})
|
Error(msg string, fields ...map[string]interface{})
|
||||||
Fatal(msg string, fields map[string]interface{})
|
Fatal(msg string, fields ...map[string]interface{})
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetLogger() *ZapLogger {
|
func GetLogger() *ZapLogger {
|
||||||
|
|
46
internal/middlewares/logger.go
Normal file
46
internal/middlewares/logger.go
Normal file
|
@ -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,
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
|
@ -2,12 +2,12 @@ package router
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/go-chi/chi/v5"
|
"github.com/go-chi/chi/v5"
|
||||||
"github.com/go-chi/chi/v5/middleware"
|
|
||||||
httpSwagger "github.com/swaggo/http-swagger"
|
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/docs"
|
||||||
"code.alt-gnome.ru/aides-infra/aides-repo-api/internal/config"
|
"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/controllers/taskcontroller"
|
||||||
|
"code.alt-gnome.ru/aides-infra/aides-repo-api/internal/logger"
|
||||||
"code.alt-gnome.ru/aides-infra/aides-repo-api/internal/middlewares"
|
"code.alt-gnome.ru/aides-infra/aides-repo-api/internal/middlewares"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -27,10 +27,11 @@ func New(
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Router) Setup() *chi.Mux {
|
func (r *Router) Setup() *chi.Mux {
|
||||||
router := chi.NewRouter()
|
|
||||||
router.Use(middleware.Logger)
|
|
||||||
|
|
||||||
authGuard := middlewares.CreateAuthGuard(r.config)
|
authGuard := middlewares.CreateAuthGuard(r.config)
|
||||||
|
logger := middlewares.LoggerMiddleware(logger.GetLogger())
|
||||||
|
|
||||||
|
router := chi.NewRouter()
|
||||||
|
router.Use(logger)
|
||||||
|
|
||||||
router.Get("/swagger/*", httpSwagger.WrapHandler)
|
router.Get("/swagger/*", httpSwagger.WrapHandler)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue