diff --git a/serv_nginx/api_bb/go.mod b/serv_nginx/api_bb/go.mod index d803169..3b72df5 100644 --- a/serv_nginx/api_bb/go.mod +++ b/serv_nginx/api_bb/go.mod @@ -12,38 +12,16 @@ require ( ) require ( - github.com/bytedance/sonic v1.14.0 // indirect - github.com/bytedance/sonic/loader v0.3.0 // indirect - github.com/cloudwego/base64x v0.1.6 // indirect github.com/gabriel-vasile/mimetype v1.4.10 // indirect - github.com/gin-contrib/sse v1.1.0 // indirect github.com/go-playground/locales v0.14.1 // indirect github.com/go-playground/universal-translator v0.18.1 // indirect - github.com/goccy/go-json v0.10.2 // indirect - github.com/goccy/go-yaml v1.18.0 // indirect - github.com/json-iterator/go v1.1.12 // indirect - github.com/klauspost/cpuid/v2 v2.3.0 // indirect github.com/leodido/go-urn v1.4.0 // indirect - github.com/mattn/go-isatty v0.0.20 // indirect - github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect - github.com/modern-go/reflect2 v1.0.2 // indirect - github.com/pelletier/go-toml/v2 v2.2.4 // indirect - github.com/quic-go/qpack v0.5.1 // indirect - github.com/quic-go/quic-go v0.54.0 // indirect - github.com/twitchyliquid64/golang-asm v0.15.1 // indirect - github.com/ugorji/go/codec v1.3.0 // indirect - go.uber.org/mock v0.5.0 // indirect + github.com/stretchr/testify v1.11.1 // indirect go.uber.org/multierr v1.10.0 // indirect - golang.org/x/arch v0.20.0 // indirect - golang.org/x/mod v0.28.0 // indirect - golang.org/x/net v0.45.0 // indirect golang.org/x/sys v0.37.0 // indirect - golang.org/x/tools v0.37.0 // indirect - google.golang.org/protobuf v1.36.9 // indirect ) require ( - github.com/gin-gonic/gin v1.11.0 github.com/go-playground/validator/v10 v10.28.0 github.com/jackc/pgpassfile v1.0.0 // indirect github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect diff --git a/serv_nginx/api_bb/go.sum b/serv_nginx/api_bb/go.sum index a7c2ae1..04c9bb8 100644 --- a/serv_nginx/api_bb/go.sum +++ b/serv_nginx/api_bb/go.sum @@ -1,35 +1,22 @@ -github.com/bytedance/sonic v1.14.0 h1:/OfKt8HFw0kh2rj8N0F6C/qPGRESq0BbaNZgcNXXzQQ= -github.com/bytedance/sonic v1.14.0/go.mod h1:WoEbx8WTcFJfzCe0hbmyTGrfjt8PzNEBdxlNUO24NhA= -github.com/bytedance/sonic/loader v0.3.0 h1:dskwH8edlzNMctoruo8FPTJDF3vLtDT0sXZwvZJyqeA= -github.com/bytedance/sonic/loader v0.3.0/go.mod h1:N8A3vUdtUebEY2/VQC0MyhYeKUFosQU6FxH2JmUe6VI= -github.com/cloudwego/base64x v0.1.6 h1:t11wG9AECkCDk5fMSoxmufanudBtJ+/HemLstXDLI2M= -github.com/cloudwego/base64x v0.1.6/go.mod h1:OFcloc187FXDaYHvrNIjxSe8ncn0OOM8gEHfghB2IPU= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/gabriel-vasile/mimetype v1.4.10 h1:zyueNbySn/z8mJZHLt6IPw0KoZsiQNszIpU+bX4+ZK0= github.com/gabriel-vasile/mimetype v1.4.10/go.mod h1:d+9Oxyo1wTzWdyVUPMmXFvp4F9tea18J8ufA774AB3s= -github.com/gin-contrib/sse v1.1.0 h1:n0w2GMuUpWDVp7qSpvze6fAu9iRxJY4Hmj6AmBOU05w= -github.com/gin-contrib/sse v1.1.0/go.mod h1:hxRZ5gVpWMT7Z0B0gSNYqqsSCNIJMjzvm6fqCz9vjwM= -github.com/gin-gonic/gin v1.11.0 h1:OW/6PLjyusp2PPXtyxKHU0RbX6I/l28FTdDlae5ueWk= -github.com/gin-gonic/gin v1.11.0/go.mod h1:+iq/FyxlGzII0KHiBGjuNn4UNENUlKbGlNmc+W50Dls= github.com/go-chi/chi/v5 v5.2.3 h1:WQIt9uxdsAbgIYgid+BpYc+liqQZGMHRaUwp0JUcvdE= github.com/go-chi/chi/v5 v5.2.3/go.mod h1:L2yAIGWB3H+phAw1NxKwWM+7eUH/lU8pOMm5hHcoops= github.com/go-chi/cors v1.2.2 h1:Jmey33TE+b+rB7fT8MUy1u0I4L+NARQlK6LhzKPSyQE= github.com/go-chi/cors v1.2.2/go.mod h1:sSbTewc+6wYHBBCW7ytsFSn836hqM7JxpglAy2Vzc58= +github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s= +github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA= github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY= github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY= github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY= github.com/go-playground/validator/v10 v10.28.0 h1:Q7ibns33JjyW48gHkuFT91qX48KG0ktULL6FgHdG688= github.com/go-playground/validator/v10 v10.28.0/go.mod h1:GoI6I1SjPBh9p7ykNE/yj3fFYbyDOpwMn5KXd+m2hUU= -github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU= -github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= -github.com/goccy/go-yaml v1.18.0 h1:8W7wMFS12Pcas7KU+VVkaiCng+kG8QiFeFwzFb+rwuw= -github.com/goccy/go-yaml v1.18.0/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA= github.com/golang-jwt/jwt/v5 v5.3.0 h1:pv4AsKCKKZuqlgs5sUmn4x8UlGa0kEVt/puTpKx9vvo= github.com/golang-jwt/jwt/v5 v5.3.0/go.mod h1:fxCRLWMO43lRc8nhHWY6LGqRcf+1gQWArsqaEUEa5bE= -github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg= github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo= @@ -44,64 +31,29 @@ github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= -github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= -github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= -github.com/klauspost/cpuid/v2 v2.3.0 h1:S4CRMLnYUhGeDFDqkGriYKdfoFlDnMtqTiI/sFzhA9Y= -github.com/klauspost/cpuid/v2 v2.3.0/go.mod h1:hqwkgyIinND0mEev00jJYCxPNVRVXFQeu1XKlok6oO0= github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ= github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI= -github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= -github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= -github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc= -github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= -github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= -github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= -github.com/pelletier/go-toml/v2 v2.2.4 h1:mye9XuhQ6gvn5h28+VilKrrPoQVanw5PMw/TB0t5Ec4= -github.com/pelletier/go-toml/v2 v2.2.4/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/quic-go/qpack v0.5.1 h1:giqksBPnT/HDtZ6VhtFKgoLOWmlyo9Ei6u9PqzIMbhI= -github.com/quic-go/qpack v0.5.1/go.mod h1:+PC4XFrEskIVkcLzpEkbLqq1uCoxPhQuvK5rH1ZgaEg= -github.com/quic-go/quic-go v0.54.0 h1:6s1YB9QotYI6Ospeiguknbp2Znb/jZYjZLRXn9kMQBg= -github.com/quic-go/quic-go v0.54.0/go.mod h1:e68ZEaCdyviluZmy44P6Iey98v/Wfz6HCjQEm+l8zTY= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= -github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= -github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= -github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= -github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI= -github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08= -github.com/ugorji/go/codec v1.3.0 h1:Qd2W2sQawAfG8XSvzwhBeoGq71zXOC/Q1E9y/wUcsUA= -github.com/ugorji/go/codec v1.3.0/go.mod h1:pRBVtBSKl77K30Bv8R2P+cLSGaTtex6fsA2Wjqmfxj4= -go.uber.org/mock v0.5.0 h1:KAMbZvZPyBPWgD14IrIQ38QCyjwpvVVV6K/bHl1IwQU= -go.uber.org/mock v0.5.0/go.mod h1:ge71pBPLYDk7QIi1LupWxdAykm7KIEFchiOqd6z7qMM= +github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U= +github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U= +go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto= +go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE= go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ= go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8= go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E= -golang.org/x/arch v0.20.0 h1:dx1zTU0MAE98U+TQ8BLl7XsJbgze2WnNKF/8tGp/Q6c= -golang.org/x/arch v0.20.0/go.mod h1:bdwinDaKcfZUGpH09BB7ZmOfhalA8lQdzl62l8gGWsk= golang.org/x/crypto v0.43.0 h1:dduJYIi3A3KOfdGOHX8AVZ/jGiyPa3IbBozJ5kNuE04= golang.org/x/crypto v0.43.0/go.mod h1:BFbav4mRNlXJL4wNeejLpWxB7wMbc79PdRGhWKncxR0= -golang.org/x/mod v0.28.0 h1:gQBtGhjxykdjY9YhZpSlZIsbnaE2+PgjfLWUQTnoZ1U= -golang.org/x/mod v0.28.0/go.mod h1:yfB/L0NOf/kmEbXjzCPOx1iK1fRutOydrCMsqRhEBxI= -golang.org/x/net v0.45.0 h1:RLBg5JKixCy82FtLJpeNlVM0nrSqpCRYzVU1n8kj0tM= -golang.org/x/net v0.45.0/go.mod h1:ECOoLqd5U3Lhyeyo/QDCEVQ4sNgYsqvCZ722XogGieY= golang.org/x/sync v0.17.0 h1:l60nONMj9l5drqw6jlhIELNv9I0A4OFgRsG9k2oT9Ug= golang.org/x/sync v0.17.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI= -golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.37.0 h1:fdNQudmxPjkdUTPnLn5mdQv7Zwvbvpaxqs831goi9kQ= golang.org/x/sys v0.37.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks= golang.org/x/text v0.30.0 h1:yznKA/E9zq54KzlzBEAWn1NXSQ8DIp/NYMy88xJjl4k= golang.org/x/text v0.30.0/go.mod h1:yDdHFIX9t+tORqspjENWgzaCVXgk0yYnYuSZ8UzzBVM= -golang.org/x/tools v0.37.0 h1:DVSRzp7FwePZW356yEAChSdNcQo6Nsp+fex1SUW09lE= -golang.org/x/tools v0.37.0/go.mod h1:MBN5QPQtLMHVdvsbtarmTNukZDdgwdwlO5qGacAzF0w= -google.golang.org/protobuf v1.36.9 h1:w2gp2mA27hUeUzj9Ex9FBjsBm40zfaDtEWow293U7Iw= -google.golang.org/protobuf v1.36.9/go.mod h1:fuxRtAxBytpl4zzqUh6/eyUujkJdNiuEkXntxiD/uRU= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= diff --git a/serv_nginx/api_bb/internal/handlers/review_handler.go b/serv_nginx/api_bb/internal/handlers/review_handler.go index fa29257..9ea0074 100644 --- a/serv_nginx/api_bb/internal/handlers/review_handler.go +++ b/serv_nginx/api_bb/internal/handlers/review_handler.go @@ -5,6 +5,7 @@ import ( "api_bb/internal/models" "api_bb/internal/service" "api_bb/pkg/logger" + "api_bb/pkg/middleware" "api_bb/pkg/utils" "net/http" "strconv" @@ -13,11 +14,13 @@ import ( "go.uber.org/zap" ) +// ReviewHandler обрабатывает HTTP-запросы, связанные с отзывами type ReviewHandler struct { - reviewService service.ReviewService - logger logger.LoggerInterface + reviewService service.ReviewService // Сервис для работы с отзывами + logger logger.LoggerInterface // Логгер для записи событий } +// NewReviewHandler создает новый экземпляр ReviewHandler func NewReviewHandler(reviewService service.ReviewService, logger logger.LoggerInterface) *ReviewHandler { return &ReviewHandler{ reviewService: reviewService, @@ -25,23 +28,6 @@ func NewReviewHandler(reviewService service.ReviewService, logger logger.LoggerI } } -func (h *ReviewHandler) Routes() chi.Router { - r := chi.NewRouter() - - r.Get("/", h.GetReviews) - r.Get("/stats", h.GetReviewsStats) - r.Get("/my", h.GetMyReviews) - r.Post("/", h.CreateReview) - - r.Route("/{id}", func(r chi.Router) { - r.Get("/", h.GetReviewByID) - r.Put("/", h.UpdateReview) - r.Delete("/", h.DeleteReview) - }) - - return r -} - // GetReviews возвращает список отзывов с пагинацией и фильтрацией func (h *ReviewHandler) GetReviews(w http.ResponseWriter, r *http.Request) { page, _ := strconv.Atoi(r.URL.Query().Get("page")) @@ -85,14 +71,21 @@ func (h *ReviewHandler) GetReviewsStats(w http.ResponseWriter, r *http.Request) utils.RespondWithJSON(w, http.StatusOK, stats) } -// GetMyReviews возвращает отзывы текущего пользователя +// GetMyReviews возвращает отзывы текущего аутентифицированного пользователя func (h *ReviewHandler) GetMyReviews(w http.ResponseWriter, r *http.Request) { - userID, ok := r.Context().Value("userID").(uint) + // Получаем ID пользователя из контекста (добавляется middleware аутентификации) + userID, ok := r.Context().Value("middleware.UserIDKey").(uint) if !ok { + h.logger.Warn("Failed to get userID from context in GetMyReviews", + zap.String("path", r.URL.Path), + zap.String("method", r.Method), + zap.String("remote_addr", r.RemoteAddr), + ) utils.RespondWithError(w, http.StatusUnauthorized, "User not authenticated") return } + // Получаем отзывы пользователя из сервиса reviews, err := h.reviewService.GetUserReviews(userID) if err != nil { h.logger.With(zap.Int("userID", int(userID))).Error("Failed to get user reviews", zap.Error(err)) @@ -103,21 +96,39 @@ func (h *ReviewHandler) GetMyReviews(w http.ResponseWriter, r *http.Request) { utils.RespondWithJSON(w, http.StatusOK, reviews) } -// CreateReview создает новый отзыв +// CreateReview создает новый отзыв от имени текущего пользователя func (h *ReviewHandler) CreateReview(w http.ResponseWriter, r *http.Request) { - userID, ok := r.Context().Value("userID").(uint) + // Получаем ID пользователя из контекста + userID, ok := r.Context().Value(middleware.UserIDKey).(uint) if !ok { + h.logger.Warn("Failed to get userID from context in CreateReview", + zap.String("path", r.URL.Path), + zap.String("method", r.Method), + zap.String("remote_addr", r.RemoteAddr), + zap.Uint("userID", userID), + ) utils.RespondWithError(w, http.StatusUnauthorized, "User not authenticated") return } + h.logger.Debug("Successfully extracted userID from context", + zap.Uint("userID", userID), + zap.String("path", r.URL.Path), + zap.String("method", r.Method), + ) + + // Декодируем тело запроса var req models.CreateReviewRequest if err := utils.DecodeJSONBody(w, r, &req); err != nil { - h.logger.Error("Failed to decode review request", zap.Error(err)) + h.logger.Error("Failed to decode review request", + zap.Error(err), + zap.Uint("userID", userID), + ) utils.RespondWithError(w, http.StatusBadRequest, "Invalid request body") return } + // Создаем отзыв через сервис review, err := h.reviewService.CreateReview(&req, userID) if err != nil { h.logger.With(zap.Int("userID", int(userID))).Error("Failed to create review", zap.Error(err)) @@ -125,11 +136,17 @@ func (h *ReviewHandler) CreateReview(w http.ResponseWriter, r *http.Request) { return } + h.logger.Info("Review created successfully", + zap.Uint("userID", userID), + zap.Any("review_id", review.ID), + ) + utils.RespondWithJSON(w, http.StatusCreated, review) } -// GetReviewByID возвращает отзыв по ID +// GetReviewByID возвращает отзыв по его идентификатору func (h *ReviewHandler) GetReviewByID(w http.ResponseWriter, r *http.Request) { + // Получаем ID отзыва из параметров URL idStr := chi.URLParam(r, "id") id, err := strconv.ParseUint(idStr, 10, 32) if err != nil { @@ -137,6 +154,7 @@ func (h *ReviewHandler) GetReviewByID(w http.ResponseWriter, r *http.Request) { return } + // Получаем отзыв из сервиса review, err := h.reviewService.GetReviewByID(uint(id)) if err != nil { h.logger.With(zap.Int("id", int(id))).Error("Failed to get review", zap.Error(err)) @@ -147,16 +165,25 @@ func (h *ReviewHandler) GetReviewByID(w http.ResponseWriter, r *http.Request) { utils.RespondWithJSON(w, http.StatusOK, review) } -// UpdateReview обновляет отзыв +// UpdateReview обновляет существующий отзыв func (h *ReviewHandler) UpdateReview(w http.ResponseWriter, r *http.Request) { - userID, ok := r.Context().Value("userID").(uint) + // Получаем ID пользователя из контекста + userID, ok := r.Context().Value(middleware.UserIDKey).(uint) if !ok { + h.logger.Warn("Failed to get userID from context in UpdateReview", + zap.String("path", r.URL.Path), + zap.String("method", r.Method), + zap.String("remote_addr", r.RemoteAddr), + zap.Uint("userID", userID), + ) utils.RespondWithError(w, http.StatusUnauthorized, "User not authenticated") return } - isAdmin, _ := r.Context().Value("isAdmin").(bool) + // Получаем флаг администратора из контекста + isAdmin, _ := r.Context().Value("IsAdmin").(bool) + // Получаем ID отзыва из параметров URL idStr := chi.URLParam(r, "id") id, err := strconv.ParseUint(idStr, 10, 32) if err != nil { @@ -164,13 +191,19 @@ func (h *ReviewHandler) UpdateReview(w http.ResponseWriter, r *http.Request) { return } + // Декодируем тело запроса var req models.UpdateReviewRequest if err := utils.DecodeJSONBody(w, r, &req); err != nil { - h.logger.Error("Failed to decode update review request", zap.Error(err)) + h.logger.Error("Failed to decode update review request", + zap.Error(err), + zap.Uint("userID", userID), + zap.Uint("review_id", uint(id)), + ) utils.RespondWithError(w, http.StatusBadRequest, "Invalid request body") return } + // Обновляем отзыв через сервис review, err := h.reviewService.UpdateReview(uint(id), &req, userID, isAdmin) if err != nil { h.logger.With(zap.Int("id", int(id))).With(zap.Int("userID", int(userID))).Error("Failed to update review", zap.Error(err)) @@ -182,19 +215,32 @@ func (h *ReviewHandler) UpdateReview(w http.ResponseWriter, r *http.Request) { return } + h.logger.Info("Review updated successfully", + zap.Uint("userID", userID), + zap.Uint("review_id", uint(id)), + ) + utils.RespondWithJSON(w, http.StatusOK, review) } // DeleteReview удаляет отзыв func (h *ReviewHandler) DeleteReview(w http.ResponseWriter, r *http.Request) { - userID, ok := r.Context().Value("userID").(uint) + // Получаем ID пользователя из контекста + userID, ok := r.Context().Value(middleware.UserIDKey).(uint) if !ok { + h.logger.Warn("Failed to get userID from context in DeleteReview", + zap.String("path", r.URL.Path), + zap.String("method", r.Method), + zap.String("remote_addr", r.RemoteAddr), + ) utils.RespondWithError(w, http.StatusUnauthorized, "User not authenticated") return } - isAdmin, _ := r.Context().Value("isAdmin").(bool) + // Получаем флаг администратора из контекста + isAdmin, _ := r.Context().Value("IsAdmin").(bool) + // Получаем ID отзыва из параметров URL idStr := chi.URLParam(r, "id") id, err := strconv.ParseUint(idStr, 10, 32) if err != nil { @@ -202,6 +248,7 @@ func (h *ReviewHandler) DeleteReview(w http.ResponseWriter, r *http.Request) { return } + // Удаляем отзыв через сервис err = h.reviewService.DeleteReview(uint(id), userID, isAdmin) if err != nil { h.logger.With(zap.Int("id", int(id))).With(zap.Int("userID", int(userID))).Error("Failed to delete review", zap.Error(err)) @@ -213,5 +260,10 @@ func (h *ReviewHandler) DeleteReview(w http.ResponseWriter, r *http.Request) { return } + h.logger.Info("Review deleted successfully", + zap.Uint("userID", userID), + zap.Uint("review_id", uint(id)), + ) + utils.RespondWithJSON(w, http.StatusOK, map[string]string{"message": "Review deleted successfully"}) } diff --git a/serv_nginx/api_bb/pkg/middleware/auth.go b/serv_nginx/api_bb/pkg/middleware/auth.go index 37f2fb3..0fc509c 100644 --- a/serv_nginx/api_bb/pkg/middleware/auth.go +++ b/serv_nginx/api_bb/pkg/middleware/auth.go @@ -9,6 +9,9 @@ import ( "api_bb/internal/models" "api_bb/internal/repository" "api_bb/internal/service" + "api_bb/pkg/logger" + + "go.uber.org/zap" ) type contextKey string @@ -22,11 +25,13 @@ func AuthMiddleware(jwtService service.JWTService, userRepo repository.UserRepos return func(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { var tokenString string + logger := logger.Get() // Пробуем получить токен из заголовка Authorization authHeader := r.Header.Get("Authorization") if strings.HasPrefix(authHeader, "Bearer ") { tokenString = strings.TrimPrefix(authHeader, "Bearer ") + logger.Debug("Token found in Authorization header") } // Если нет в заголовке, пробуем из куки @@ -34,28 +39,43 @@ func AuthMiddleware(jwtService service.JWTService, userRepo repository.UserRepos cookie, err := r.Cookie("auth_token") if err == nil { tokenString = cookie.Value + logger.Debug("Token found in auth_token cookie") + } else { + logger.Debug("No auth_token cookie found", zap.Error(err)) } } if tokenString == "" { + logger.Debug("No token found in request") next.ServeHTTP(w, r) return } token, err := jwtService.ValidateToken(tokenString) if err != nil || !token.Valid { + logger.Warn("Invalid token", + zap.Error(err), + zap.Bool("token_valid", token != nil && token.Valid)) next.ServeHTTP(w, r) return } userID, err := jwtService.ExtractUserID(token) if err != nil { + logger.Error("Failed to extract user ID from token", + zap.Error(err)) next.ServeHTTP(w, r) return } + logger.Debug("Extracted user ID from token", + zap.Any("user_id", userID)) + user, err := userRepo.FindByID(userID) if err != nil { + logger.Error("Failed to find user by ID", + zap.Any("user_id", userID), + zap.Error(err)) next.ServeHTTP(w, r) return } @@ -64,6 +84,10 @@ func AuthMiddleware(jwtService service.JWTService, userRepo repository.UserRepos ctx := context.WithValue(r.Context(), UserIDKey, userID) ctx = context.WithValue(ctx, UserKey, user) + logger.Debug("User authenticated successfully", + zap.Any("user_id", userID), + zap.String("username", user.FirstName)) + next.ServeHTTP(w, r.WithContext(ctx)) }) } @@ -72,17 +96,32 @@ func AuthMiddleware(jwtService service.JWTService, userRepo repository.UserRepos // RequireAuth middleware требует аутентификации func RequireAuth(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + logger := logger.Get() userID := r.Context().Value(UserIDKey) + if userID == nil { + logger.Warn("Authentication required but no user ID in context") http.Error(w, `{"error": "Authentication required"}`, http.StatusUnauthorized) return } + + logger.Debug("User authenticated", zap.Any("user_id", userID)) next.ServeHTTP(w, r) }) } // GetUserFromContext получает пользователя из контекста func GetUserFromContext(ctx context.Context) (*models.User, bool) { + logger := logger.Get() user, ok := ctx.Value(UserKey).(*models.User) + + if !ok { + logger.Debug("No user found in context") + } else { + logger.Debug("User retrieved from context", + zap.Any("user_id", user.ID), + zap.String("username", user.FirstName)) + } + return user, ok } \ No newline at end of file