diff --git a/main_dc/yalarba/api_yal/internal/middleware/authMiddleware.go b/main_dc/yalarba/api_yal/internal/middleware/authMiddleware.go new file mode 100644 index 0000000..1331936 --- /dev/null +++ b/main_dc/yalarba/api_yal/internal/middleware/authMiddleware.go @@ -0,0 +1,35 @@ +package middleware + +import ( + "context" + "net/http" +) + +type contextKey string + +const ( + UserIDKey contextKey = "userID" + IsAuthKey contextKey = "isAuthenticated" +) + +// AuthMiddlewareWithContext добавляет информацию об авторизации в контекст +func AuthMiddlewareWithContext(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + // Создаем контекст с тестовыми данными + ctx := r.Context() + ctx = context.WithValue(ctx, UserIDKey, 0) + ctx = context.WithValue(ctx, IsAuthKey, false) + + // В реальном проекте здесь будет: + // token := r.Header.Get("Authorization") + // if token != "" { + // userID, err := validateToken(token) + // if err == nil { + // ctx = context.WithValue(ctx, UserIDKey, userID) + // ctx = context.WithValue(ctx, IsAuthKey, true) + // } + // } + + next.ServeHTTP(w, r.WithContext(ctx)) + }) +} \ No newline at end of file diff --git a/main_dc/yalarba/api_yal/internal/router/router.go b/main_dc/yalarba/api_yal/internal/router/router.go index 577d7d2..0141f79 100644 --- a/main_dc/yalarba/api_yal/internal/router/router.go +++ b/main_dc/yalarba/api_yal/internal/router/router.go @@ -23,6 +23,8 @@ func SetupRouter(db *gorm.DB, config *config.Config) http.Handler { json.NewEncoder(w).Encode(map[string]string{"status": "healthy"}) }) + r.Group() + zapLogger.Info("End setup routers") // Логируем все зарегистрированные маршруты