Kazim68/logiscout-Dashboard-server

GitHub: Kazim68/logiscout-Dashboard-server

一个基于 FastAPI 的 DevOps 事件响应平台后端,通过 AI 和 RAG 技术分析日志与历史事故以加速根因定位。

Stars: 0 | Forks: 0

# LogiScout 后端 API 一个专业、模块化的 FastAPI 后端,专为 LogiScout AI 驱动的事件解决平台构建。 ## 功能 - 🔐 **JWT 身份验证** - 安全的基于 token 的身份验证 - 📧 **邮箱/密码验证** - 传统的注册和登录方式 - 🔑 **Google OAuth 2.0** - 使用 Google 登录 - 🐙 **GitHub OAuth 2.0** - 使用 GitHub 登录 - 📊 **受保护的 Dashboard API** - 需身份验证的端点 - 🗄️ **MongoDB** - 使用 Motor 驱动程序的异步数据库 - 📝 **适配 Redux 的响应** - 统一的响应格式 ## 项目结构 ``` backend/ ├── app/ │ ├── main.py # FastAPI application entry point │ ├── __init__.py │ │ │ ├── core/ # Core configuration and utilities │ │ ├── config.py # Environment settings │ │ ├── security.py # JWT and password utilities │ │ ├── database.py # MongoDB connection │ │ └── __init__.py │ │ │ ├── models/ # Database models │ │ ├── user_model.py # User document model │ │ └── __init__.py │ │ │ ├── schemas/ # Pydantic schemas │ │ ├── user_schema.py # Request/response schemas │ │ └── __init__.py │ │ │ ├── services/ # Business logic layer │ │ ├── auth_service.py # Authentication logic │ │ ├── google_oauth_service.py │ │ ├── github_oauth_service.py │ │ └── __init__.py │ │ │ ├── routes/ # API endpoints │ │ ├── auth_routes.py # Email auth endpoints │ │ ├── google_oauth_routes.py │ │ ├── github_oauth_routes.py │ │ ├── dashboard_routes.py # Protected endpoints │ │ └── __init__.py │ │ │ ├── utils/ # Utility functions │ │ ├── response_handler.py # Standardized responses │ │ └── __init__.py │ │ │ └── dependencies/ # FastAPI dependencies │ ├── auth_dependency.py # Token validation │ └── __init__.py │ ├── .env # Environment variables ├── requirements.txt # Python dependencies └── README.md # This file ``` ## 快速开始 ### 前置条件 - Python 3.10+ - MongoDB(本地或 Atlas) - Google OAuth 凭据(用于 Google 登录) - GitHub OAuth 凭据(用于 GitHub 登录) ### 安装 1. **导航至 backend 目录** cd backend 2. **创建虚拟环境** python -m venv venv # Windows venv\Scripts\activate # Linux/Mac source venv/bin/activate 3. **安装依赖项** pip install -r requirements.txt 4. **配置环境变量** 使用您的凭据编辑 `.env` 文件: MONGO_URI=mongodb://localhost:27017 DATABASE_NAME=logiscout JWT_SECRET_KEY=your-secret-key GOOGLE_CLIENT_ID=your-google-client-id GOOGLE_CLIENT_SECRET=your-google-client-secret GITHUB_CLIENT_ID=your-github-client-id GITHUB_CLIENT_SECRET=your-github-client-secret 5. **运行服务器** uvicorn app.main:app --reload --port 8000 6. **访问 API** - API: http://localhost:8000 - Swagger 文档: http://localhost:8000/docs - ReDoc: http://localhost:8000/redoc ## API 端点 ### 身份验证 | Method | Endpoint | Description | |--------|----------|-------------| | POST | `/api/auth/signup` | 注册新用户 | | POST | `/api/auth/login` | 用户登录 | | GET | `/api/auth/google` | 发起 Google OAuth | | GET | `/api/auth/google/callback` | Google OAuth 回调 | | GET | `/api/auth/github` | 发起 GitHub OAuth | | GET | `/api/auth/github/callback` | GitHub OAuth 回调 | ### 受保护的路由 | Method | Endpoint | Description | |--------|----------|-------------| | GET | `/api/dashboard` | 获取 Dashboard 数据 | | GET | `/api/profile` | 获取用户资料 | | GET | `/api/health` | 健康检查 | ## 响应格式 所有 API 响应均遵循此结构,以便于集成 Redux: ``` { "success": true, "message": "Operation completed", "data": { "user": {...}, "stats": {...} }, "token": "eyJhbGciOiJIUzI1NiIs..." } ``` ## 身份验证流程 ### 邮箱/密码 1. **注册**:POST `/api/auth/signup` { "name": "John Doe", "email": "john@example.com", "password": "securePassword123" } 2. **登录**:POST `/api/auth/login` { "email": "john@example.com", "password": "securePassword123" } ### OAuth 流程 1. 前端调用 `GET /api/auth/google` 或 `GET /api/auth/github` 2. 响应包含 `redirect_url` 3. 前端将用户重定向至 OAuth 提供商 4. 用户授权应用 5. 提供商重定向至回调 URL 6. 后端创建/登录用户并携带 token 重定向至前端 ## JWT Token 在请求受保护的端点时请包含 token: ``` Authorization: Bearer ``` Token payload: ``` { "user_id": "507f1f77bcf86cd799439011", "email": "user@example.com", "provider": "email", "exp": 1704067200 } ``` ## OAuth 设置 ### Google OAuth 1. 前往 [Google Cloud Console](https://console.cloud.google.com) 2. 创建一个新项目 3. 启用 Google+ API 4. 创建 OAuth 2.0 凭据 5. 添加授权的重定向 URI:`http://localhost:8000/api/auth/google/callback` 6. 将 Client ID 和 Client Secret 复制到 `.env` ### GitHub OAuth 1. 前往 [GitHub 开发者设置](https://github.com/settings/developers) 2. 创建一个新的 OAuth App 3. 设置 Homepage URL:`https://logiscout-frontend.vercel.app` 4. 设置 Authorization callback URL:`http://localhost:8000/api/auth/github/callback` 5. 将 Client ID 和 Client Secret 复制到 `.env` ## MongoDB 数据结构 ### 用户文档 ``` { "_id": "ObjectId", "name": "John Doe", "email": "john@example.com", "password": "hashed_password (nullable for OAuth)", "provider": "email | google | github", "provider_id": "external_provider_user_id", "created_at": "2024-01-15T10:30:00Z" } ``` ## 开发 ### 在开发模式下运行 ``` uvicorn app.main:app --reload --port 8000 ``` ### 带调试器运行 ``` python -m app.main ``` ## 安全性 - 密码使用 bcrypt 进行哈希处理 - JWT token 在 1 天后过期(可配置) - CORS 已为前端集成进行配置 - 使用 OAuth state 参数防止 CSRF 攻击 - 响应中排除了敏感数据 ## 前端集成 前端 (Next.js) 需要: - 后端位于 `http://localhost:8000` - 采用标准格式的响应 - 用于身份验证的 JWT token - OAuth 重定向至 `/dashboard?token=...` ## 许可证 MIT 许可证
标签:AIOps, AV绕过, CISA项目, FastAPI, GitHub OAuth, Google OAuth, JWT认证, MongoDB, Motor驱动, OAuth 2.0, Pydantic, Python, RAG, Redux, RESTful API, SaaS, 人工智能, 后端开发, 安全防护, 异步数据库, 提示词优化, 故障排查, 无后门, 智能副驾驶, 根因分析, 检索增强生成, 用户模式Hook绕过, 运维自动化, 逆向工具, 降低MTTR