add middleware for perflite requests and delete cors from user and auth

handlers
This commit is contained in:
2025-10-11 11:07:43 +05:00
parent 6850701b47
commit 2ccab0a17f
5 changed files with 33 additions and 25 deletions
@@ -48,9 +48,6 @@ func (h *AuthHandler) Routes() chi.Router {
// Обработчик для OPTIONS запросов // Обработчик для OPTIONS запросов
func (h *AuthHandler) handleOptions(w http.ResponseWriter, r *http.Request) { func (h *AuthHandler) handleOptions(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization")
w.Header().Set("Access-Control-Max-Age", "300")
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
} }
@@ -71,9 +68,6 @@ type LoginRequest struct {
} }
func (h *AuthHandler) Register(w http.ResponseWriter, r *http.Request) { func (h *AuthHandler) Register(w http.ResponseWriter, r *http.Request) {
// Устанавливаем CORS заголовки
w.Header().Set("Access-Control-Allow-Origin", r.Header.Get("Origin"))
w.Header().Set("Access-Control-Allow-Credentials", "true")
h.logger.Info("handling register request", h.logger.Info("handling register request",
zap.String("method", r.Method), zap.String("method", r.Method),
@@ -170,9 +164,6 @@ func (h *AuthHandler) Register(w http.ResponseWriter, r *http.Request) {
} }
func (h *AuthHandler) Login(w http.ResponseWriter, r *http.Request) { func (h *AuthHandler) Login(w http.ResponseWriter, r *http.Request) {
// Устанавливаем CORS заголовки
w.Header().Set("Access-Control-Allow-Origin", r.Header.Get("Origin"))
w.Header().Set("Access-Control-Allow-Credentials", "true")
h.logger.Info("handling login request", h.logger.Info("handling login request",
zap.String("method", r.Method), zap.String("method", r.Method),
@@ -2,6 +2,7 @@ package handlers
import ( import (
"api_bb/internal/models" "api_bb/internal/models"
"net/http"
) )
// Общая функция для преобразования User в UserResponse // Общая функция для преобразования User в UserResponse
@@ -20,3 +21,8 @@ func toUserResponse(user *models.User) UserResponse {
UpdatedAt: user.UpdatedAt, UpdatedAt: user.UpdatedAt,
} }
} }
// Обработчик для OPTIONS запросов
func (h *UserHandler) handleOptions(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}
@@ -43,14 +43,6 @@ func (h *UserHandler) Routes() chi.Router {
return r return r
} }
// Обработчик для OPTIONS запросов
func (h *UserHandler) handleOptions(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Methods", "PUT, GET, OPTIONS")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization")
w.Header().Set("Access-Control-Max-Age", "300")
w.WriteHeader(http.StatusOK)
}
type UserResponse struct { type UserResponse struct {
ID uint `json:"id"` ID uint `json:"id"`
Email string `json:"email"` Email string `json:"email"`
@@ -66,9 +58,6 @@ type UserResponse struct {
} }
func (h *UserHandler) GetProfile(w http.ResponseWriter, r *http.Request) { func (h *UserHandler) GetProfile(w http.ResponseWriter, r *http.Request) {
// Устанавливаем CORS заголовки
w.Header().Set("Access-Control-Allow-Origin", r.Header.Get("Origin"))
w.Header().Set("Access-Control-Allow-Credentials", "true")
h.logger.Info("handling get profile request", h.logger.Info("handling get profile request",
zap.String("method", r.Method), zap.String("method", r.Method),
@@ -101,11 +90,6 @@ type UpdateProfileRequest struct {
} }
func (h *UserHandler) UpdateProfile(w http.ResponseWriter, r *http.Request) { func (h *UserHandler) UpdateProfile(w http.ResponseWriter, r *http.Request) {
// Устанавливаем CORS заголовки
w.Header().Set("Access-Control-Allow-Origin", r.Header.Get("Origin"))
w.Header().Set("Access-Control-Allow-Credentials", "true")
w.Header().Set("Access-Control-Allow-Methods", "PUT, OPTIONS")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization")
h.logger.Info("handling update profile request", h.logger.Info("handling update profile request",
zap.String("method", r.Method), zap.String("method", r.Method),
+25
View File
@@ -0,0 +1,25 @@
// pkg/middleware/cors.go
package middleware
import (
"net/http"
)
// CORS middleware для обработки preflight запросов
func CORS(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Устанавливаем CORS заголовки
w.Header().Set("Access-Control-Allow-Origin", r.Header.Get("Origin"))
w.Header().Set("Access-Control-Allow-Credentials", "true")
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization, X-Requested-With")
// Если это OPTIONS запрос (preflight), сразу отвечаем
if r.Method == "OPTIONS" {
w.WriteHeader(http.StatusOK)
return
}
next.ServeHTTP(w, r)
})
}
@@ -9,6 +9,8 @@ import (
func CommonMiddleware() []func(http.Handler) http.Handler { func CommonMiddleware() []func(http.Handler) http.Handler {
return []func(http.Handler) http.Handler{ return []func(http.Handler) http.Handler{
CORS,
middleware.Logger,
ZapLogger, ZapLogger,
middleware.Recoverer, middleware.Recoverer,
middleware.RequestID, middleware.RequestID,