geqintan commited on
Commit
0fee08c
·
1 Parent(s): 4ebce54
.clinerules ADDED
@@ -0,0 +1,150 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 自定义指令
2
+ CUSTOM_INSTRUCTIONS = """
3
+ # 必须遵守的约束
4
+ - 所有交流均采用中文
5
+ - 当你有需求不清楚时,可以向用户进行调研,调研时,每一次只提一个问题,方便用户进行沟通
6
+
7
+ # 实现目标
8
+ - 使用 fastapi 实现 ai-edu-h5 的后端 API
9
+ - fastapi 后端接口根据前端发起的申请,生成相应的接口
10
+ - 使用 sqlite 实现数据库
11
+
12
+ # 知识参考
13
+
14
+ """
15
+
16
+
17
+ ---
18
+ description: Describes Cline's Memory Bank system, its structure, and workflows for maintaining project knowledge across sessions.
19
+ author: https://github.com/nickbaumann98
20
+ version: 1.0
21
+ tags: ["memory-bank", "knowledge-base", "core-behavior", "documentation-protocol"]
22
+ globs: ["memory-bank/**/*.md", "*"]
23
+ ---
24
+ # Cline's Memory Bank
25
+
26
+ I am Cline, an expert software engineer with a unique characteristic: my memory resets completely between sessions. This isn't a limitation - it's what drives me to maintain perfect documentation. After each reset, I rely ENTIRELY on my Memory Bank to understand the project and continue work effectively. I MUST read ALL memory bank files at the start of EVERY task - this is not optional.
27
+
28
+ ## Memory Bank Structure
29
+
30
+ The Memory Bank consists of core files and optional context files, all in Markdown format. Files build upon each other in a clear hierarchy:
31
+
32
+ ```mermaid
33
+ flowchart TD
34
+ PB[projectbrief.md] --> PC[productContext.md]
35
+ PB --> SP[systemPatterns.md]
36
+ PB --> TC[techContext.md]
37
+
38
+ PC --> AC[activeContext.md]
39
+ SP --> AC
40
+ TC --> AC
41
+
42
+ AC --> P[progress.md]
43
+ ```
44
+
45
+ ### Core Files (Required)
46
+ 1. `projectbrief.md`
47
+ - Foundation document that shapes all other files
48
+ - Created at project start if it doesn't exist
49
+ - Defines core requirements and goals
50
+ - Source of truth for project scope
51
+
52
+ 2. `productContext.md`
53
+ - Why this project exists
54
+ - Problems it solves
55
+ - How it should work
56
+ - User experience goals
57
+
58
+ 3. `activeContext.md`
59
+ - Current work focus
60
+ - Recent changes
61
+ - Next steps
62
+ - Active decisions and considerations
63
+ - Important patterns and preferences
64
+ - Learnings and project insights
65
+
66
+ 4. `systemPatterns.md`
67
+ - System architecture
68
+ - Key technical decisions
69
+ - Design patterns in use
70
+ - Component relationships
71
+ - Critical implementation paths
72
+
73
+ 5. `techContext.md`
74
+ - Technologies used
75
+ - Development setup
76
+ - Technical constraints
77
+ - Dependencies
78
+ - Tool usage patterns
79
+
80
+ 6. `progress.md`
81
+ - What works
82
+ - What's left to build
83
+ - Current status
84
+ - Known issues
85
+ - Evolution of project decisions
86
+
87
+ ### Additional Context
88
+ Create additional files/folders within memory-bank/ when they help organize:
89
+ - Complex feature documentation
90
+ - Integration specifications
91
+ - API documentation
92
+ - Testing strategies
93
+ - Deployment procedures
94
+
95
+ ## Core Workflows
96
+
97
+ ### Plan Mode
98
+ ```mermaid
99
+ flowchart TD
100
+ Start[Start] --> ReadFiles[Read Memory Bank]
101
+ ReadFiles --> CheckFiles{Files Complete?}
102
+
103
+ CheckFiles -->|No| Plan[Create Plan]
104
+ Plan --> Document[Document in Chat]
105
+
106
+ CheckFiles -->|Yes| Verify[Verify Context]
107
+ Verify --> Strategy[Develop Strategy]
108
+ Strategy --> Present[Present Approach]
109
+ ```
110
+
111
+ ### Act Mode
112
+ ```mermaid
113
+ flowchart TD
114
+ Start[Start] --> Context[Check Memory Bank]
115
+ Context --> Update[Update Documentation]
116
+ Update --> Execute[Execute Task]
117
+ Execute --> Document[Document Changes]
118
+ ```
119
+
120
+ ## Documentation Updates
121
+
122
+ Memory Bank updates occur when:
123
+ 1. Discovering new project patterns
124
+ 2. After implementing significant changes
125
+ 3. When user requests with **update memory bank** (MUST review ALL files)
126
+ 4. When context needs clarification
127
+
128
+ ```mermaid
129
+ flowchart TD
130
+ Start[Update Process]
131
+
132
+ subgraph Process
133
+ P1[Review ALL Files]
134
+ P2[Document Current State]
135
+ P3[Clarify Next Steps]
136
+ P4[Document Insights & Patterns]
137
+
138
+ P1 --> P2 --> P3 --> P4
139
+ end
140
+
141
+ Start --> Process
142
+ ```
143
+
144
+ Note: When triggered by **update memory bank**, I MUST review every memory bank file, even if some don't require updates. Focus particularly on activeContext.md and progress.md as they track current state.
145
+
146
+ REMEMBER: After every memory reset, I begin completely fresh. The Memory Bank is my only link to previous work. It must be maintained with precision and clarity, as my effectiveness depends entirely on its accuracy.
147
+
148
+ """
149
+ # 自动批准规则
150
+ AUTO_APPROVE = true
Dockerfile ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Read the doc: https://huggingface.co/docs/hub/spaces-sdks-docker
2
+ # you will also find guides on how best to write your Dockerfile
3
+
4
+ FROM python:3.9
5
+
6
+ RUN useradd -m -u 1000 user
7
+ USER user
8
+ ENV PATH="/home/user/.local/bin:$PATH"
9
+
10
+ WORKDIR /app
11
+
12
+ COPY --chown=user ./requirements.txt requirements.txt
13
+ RUN pip install --no-cache-dir --upgrade -r requirements.txt
14
+
15
+ COPY --chown=user . /app
16
+ CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
__pycache__/app.cpython-311.pyc ADDED
Binary file (12.8 kB). View file
 
ai_edu.db ADDED
Binary file (32.8 kB). View file
 
app.py ADDED
@@ -0,0 +1,246 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # conda activate rzwl && uvicorn app:app --host 0.0.0.0 --port 7860 --reload
2
+ from fastapi import FastAPI, APIRouter, HTTPException, status
3
+ from fastapi.middleware.cors import CORSMiddleware
4
+ from pydantic import BaseModel
5
+ import uuid
6
+ import sqlite3 # 导入 sqlite3 模块
7
+
8
+ app = FastAPI()
9
+
10
+ DATABASE_URL = "ai_edu.db"
11
+
12
+ def init_db():
13
+ with sqlite3.connect(DATABASE_URL) as conn:
14
+ cursor = conn.cursor()
15
+ cursor.execute("""
16
+ CREATE TABLE IF NOT EXISTS users (
17
+ user_id TEXT PRIMARY KEY,
18
+ username TEXT UNIQUE NOT NULL,
19
+ password TEXT NOT NULL
20
+ )
21
+ """)
22
+ cursor.execute("""
23
+ CREATE TABLE IF NOT EXISTS enrollments (
24
+ enrollment_id TEXT PRIMARY KEY,
25
+ name TEXT NOT NULL,
26
+ phone TEXT NOT NULL,
27
+ company TEXT,
28
+ position TEXT,
29
+ email TEXT
30
+ )
31
+ """)
32
+ cursor.execute("""
33
+ CREATE TABLE IF NOT EXISTS enterprise_orders (
34
+ order_id TEXT PRIMARY KEY,
35
+ company_name TEXT NOT NULL,
36
+ contact_person TEXT NOT NULL,
37
+ contact_phone TEXT NOT NULL,
38
+ course_name TEXT NOT NULL,
39
+ quantity INTEGER NOT NULL,
40
+ order_date TEXT NOT NULL
41
+ )
42
+ """)
43
+ conn.commit()
44
+
45
+ # 在应用启动时初始化数据库
46
+ @app.on_event("startup")
47
+ async def startup_event():
48
+ init_db()
49
+
50
+ # 配置 CORS 中间件
51
+ origins = [
52
+ "*" # 允许所有来源,开发环境方便,生产环境应限制为特定域名
53
+ ]
54
+
55
+ app.add_middleware(
56
+ CORSMiddleware,
57
+ allow_origins=origins,
58
+ allow_credentials=True,
59
+ allow_methods=["*"], # 允许所有方法,包括 OPTIONS
60
+ allow_headers=["*"], # 允许所有头部
61
+ )
62
+
63
+ api_router = APIRouter(prefix="/api")
64
+
65
+ class RegisterRequest(BaseModel):
66
+ username: str
67
+ password: str
68
+
69
+ class LoginRequest(BaseModel):
70
+ username: str
71
+ password: str
72
+
73
+ class EnrollmentIndividualRequest(BaseModel):
74
+ name: str
75
+ phone: str
76
+ company: str
77
+ position: str
78
+ email: str
79
+
80
+ @api_router.post("/auth/register")
81
+ async def register_user(request: RegisterRequest):
82
+ user_id = str(uuid.uuid4())
83
+ try:
84
+ with sqlite3.connect(DATABASE_URL) as conn:
85
+ cursor = conn.cursor()
86
+ cursor.execute(
87
+ "INSERT INTO users (user_id, username, password) VALUES (?, ?, ?)",
88
+ (user_id, request.username, request.password)
89
+ )
90
+ conn.commit()
91
+ return {
92
+ "code": 200,
93
+ "message": "注册成功",
94
+ "data": {
95
+ "user_id": user_id,
96
+ "username": request.username
97
+ }
98
+ }
99
+ except sqlite3.IntegrityError:
100
+ raise HTTPException(
101
+ status_code=status.HTTP_400_BAD_REQUEST,
102
+ detail="用户名已存在"
103
+ )
104
+ except Exception as e:
105
+ raise HTTPException(
106
+ status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
107
+ detail=f"注册失败: {str(e)}"
108
+ )
109
+
110
+ @api_router.post("/auth/login")
111
+ async def login_user(request: LoginRequest):
112
+ try:
113
+ with sqlite3.connect(DATABASE_URL) as conn:
114
+ cursor = conn.cursor()
115
+ cursor.execute(
116
+ "SELECT user_id, username FROM users WHERE username = ? AND password = ?",
117
+ (request.username, request.password)
118
+ )
119
+ user = cursor.fetchone()
120
+ if user:
121
+ user_id, username = user
122
+ return {
123
+ "code": 200,
124
+ "message": "登录成功",
125
+ "data": {
126
+ "user_id": user_id,
127
+ "username": username
128
+ }
129
+ }
130
+ else:
131
+ # 用户名或密码错误时,返回自定义的错误结构
132
+ return {
133
+ "code": 401, # 业务错误码
134
+ "message": "用户名或密码错误",
135
+ "data": None
136
+ }
137
+ except Exception as e:
138
+ # 捕获所有异常,返回 500 错误结构
139
+ print(f"登录失败未知错误: {type(e).__name__}: {e}")
140
+ return {
141
+ "code": 500,
142
+ "message": f"登录失败: {str(e)}",
143
+ "data": None
144
+ }
145
+
146
+ @api_router.post("/enrollment/individual")
147
+ async def enroll_individual(request: EnrollmentIndividualRequest):
148
+ enrollment_id = str(uuid.uuid4())
149
+ try:
150
+ with sqlite3.connect(DATABASE_URL) as conn:
151
+ cursor = conn.cursor()
152
+ cursor.execute(
153
+ "INSERT INTO enrollments (enrollment_id, name, phone, company, position, email) VALUES (?, ?, ?, ?, ?, ?)",
154
+ (enrollment_id, request.name, request.phone, request.company, request.position, request.email)
155
+ )
156
+ conn.commit()
157
+ return {
158
+ "code": 200,
159
+ "message": "个人报名成功",
160
+ "data": {
161
+ "enrollment_id": enrollment_id,
162
+ "name": request.name,
163
+ "phone": request.phone,
164
+ "company": request.company,
165
+ "position": request.position,
166
+ "email": request.email
167
+ }
168
+ }
169
+ except Exception as e:
170
+ raise HTTPException(
171
+ status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
172
+ detail=f"报名失败: {str(e)}"
173
+ )
174
+
175
+ @api_router.get("/orders/enterprise")
176
+ async def get_enterprise_orders():
177
+ try:
178
+ with sqlite3.connect(DATABASE_URL) as conn:
179
+ conn.row_factory = sqlite3.Row # 允许通过列名访问数据
180
+ cursor = conn.cursor()
181
+ cursor.execute("SELECT * FROM enterprise_orders")
182
+ orders = cursor.fetchall()
183
+ # 将 Row 对象转换为字典列表
184
+ orders_list = [dict(order) for order in orders]
185
+ return {
186
+ "code": 200,
187
+ "message": "获取企业订单成功",
188
+ "data": orders_list
189
+ }
190
+ except Exception as e:
191
+ raise HTTPException(
192
+ status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
193
+ detail=f"获取企业订单失败: {str(e)}"
194
+ )
195
+
196
+ @api_router.get("/orders/individual")
197
+ async def get_individual_orders():
198
+ try:
199
+ with sqlite3.connect(DATABASE_URL) as conn:
200
+ conn.row_factory = sqlite3.Row # 允许通过列名访问数据
201
+ cursor = conn.cursor()
202
+ cursor.execute("SELECT enrollment_id, name, phone, company, position, email FROM enrollments")
203
+ enrollments = cursor.fetchall()
204
+ # 将 Row 对象转换为字典列表
205
+ enrollments_list = [dict(enrollment) for enrollment in enrollments]
206
+ return {
207
+ "code": 200,
208
+ "message": "获取个人报名订单成功",
209
+ "data": enrollments_list
210
+ }
211
+ except Exception as e:
212
+ raise HTTPException(
213
+ status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
214
+ detail=f"获取个人报名订单失败: {str(e)}"
215
+ )
216
+
217
+ @api_router.delete("/enrollment/individual/{enrollment_id}")
218
+ async def delete_individual_enrollment(enrollment_id: str):
219
+ try:
220
+ with sqlite3.connect(DATABASE_URL) as conn:
221
+ cursor = conn.cursor()
222
+ cursor.execute("DELETE FROM enrollments WHERE enrollment_id = ?", (enrollment_id,))
223
+ conn.commit()
224
+ if cursor.rowcount == 0:
225
+ raise HTTPException(
226
+ status_code=status.HTTP_404_NOT_FOUND,
227
+ detail="未找到该报名数据"
228
+ )
229
+ return {
230
+ "code": 200,
231
+ "message": "报名数据删除成功",
232
+ "data": {"enrollment_id": enrollment_id}
233
+ }
234
+ except HTTPException as e:
235
+ raise e
236
+ except Exception as e:
237
+ raise HTTPException(
238
+ status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
239
+ detail=f"删除报名数据失败: {str(e)}"
240
+ )
241
+
242
+ @app.get("/")
243
+ def greet_json():
244
+ return {"Hello": "World!"}
245
+
246
+ app.include_router(api_router)
memory-bank/activeContext.md ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 活跃背景 (activeContext.md)
2
+
3
+ ## 当前工作重点
4
+ - 实现了 FastAPI 后端应用的基本框架。
5
+ - 集成了 SQLite 数据库用于用户数据存储。
6
+ - 实现了用户注册 (`/api/auth/register`) 和登录 (`/api/auth/login`) 接口。
7
+ - 配置了 CORS 中间件以处理跨域请求。
8
+ - 优化了登录接口的错误响应,使其返回自定义的业务错误码和信息。
9
+
10
+ ## 最近的更改
11
+ - `app.py`:
12
+ - 导入 `sqlite3` 和 `uuid` 模块。
13
+ - 添加 `DATABASE_URL` 常量。
14
+ - 添加 `init_db()` 函数用于创建 `users` 表。
15
+ - 在应用启动时调用 `init_db()`。
16
+ - 为 `RegisterRequest` 和 `LoginRequest` 定义 Pydantic 模型。
17
+ - 修改 `/api/auth/register` 接口,将用户数据写入 SQLite,并使用 UUID 生成 `user_id`。
18
+ - 添加 `/api/auth/login` 接口,实现用户登录验证和错误响应。
19
+ - 添加 `CORSMiddleware` 处理跨域请求。
20
+
21
+ ## 下一步计划
22
+ - **密码加密**: 引入密码哈希(如 `passlib`)来加密存储用户密码,提高安全性。
23
+ - **会话管理/JWT**: 实现用户登录后的会话管理或 JWT 认证机制。
24
+ - **更多业务接口**: 根据 AI-EDU H5 的需求,逐步实现其他后端 API。
25
+
26
+ ## 活跃决策和考虑
27
+ - 数据库选择:目前使用 SQLite 方便开发和演示,未来可考虑升级到 PostgreSQL 或 MySQL。
28
+ - 错误响应格式:统一了 `code`, `message`, `data` 的响应格式,便于前端处理。
29
+ - 密码安全:已识别为待改进项,优先级较高。
30
+
31
+ ## 学习和项目洞察
32
+ - FastAPI 提供了简洁高效的 API 开发体验。
33
+ - Pydantic 模型在请求体验证方面非常强大。
34
+ - SQLite 适合快速原型开发和轻量级应用。
memory-bank/productContext.md ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 产品背景 (productContext.md)
2
+
3
+ ## 项目存在的原因
4
+ 为 AI-EDU H5 前端应用提供必要的后端支持,特别是用户认证和数据管理功能。
5
+
6
+ ## 解决的问题
7
+ - 用户注册和登录:允许用户创建账户并登录到应用。
8
+ - 数据存储:持久化用户数据,为后续功能提供基础。
9
+ - API 接口:提供前端与后端交互的标准接口。
10
+
11
+ ## 工作方式
12
+ 前端通过 HTTP 请求调用后端 API,后端处理业务逻辑并与 SQLite 数据库交互,然后返回结构化的 JSON 响应。
13
+
14
+ ## 用户体验目标
15
+ - 注册和登录流程顺畅,响应及时。
16
+ - 错误信息清晰,便于用户理解和操作。
memory-bank/progress.md ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 进度 (progress.md)
2
+
3
+ ## 已完成的工作
4
+ - **FastAPI 应用启动**: 成功运行 FastAPI 应用。
5
+ - **CORS 配置**: 解决了跨域请求问题。
6
+ - **用户注册接口**:
7
+ - 实现了 `/api/auth/register` 接口。
8
+ - 用户 ID 使用 UUID 生成。
9
+ - 用户名、密码和用户 ID 存储到 SQLite 数据库的 `users` 表。
10
+ - 处理了用户名重复的错误。
11
+ - 响应数据结构符合要求。
12
+ - **用户登录接口**:
13
+ - 实现了 `/api/auth/login` 接口。
14
+ - 验证用户名和密码。
15
+ - 登录成功返回用户 ID 和用户名。
16
+ - 登录失败返回自定义错误码和信息。
17
+ - 优化了错误捕获和日志输出。
18
+
19
+ ## 待完成的工作
20
+ - **安全性增强**:
21
+ - **密码哈希**: 实现用户密码的哈希存储,而不是明文存储。
22
+ - **会话管理/JWT**: 实现用户登录后的认证机制,确保后续请求的安全性。
23
+ - **更多业务接口**: 根据 AI-EDU H5 的具体需求,逐步添加其他 API 接口。
24
+ - **数据库抽象**: 考虑引入 ORM(如 SQLAlchemy)来更好地管理数据库操作。
25
+
26
+ ## 当前状态
27
+ - 基本的用户认证功能已实现并可运行。
28
+ - 数据库集成已完成。
29
+ - 接口响应格式已统一。
30
+
31
+ ## 已知问题
32
+ - 密码明文存储,存在安全风险。
33
+
34
+ ## 项目决策演变
35
+ - 从简单的 `{"Hello": "World!"}` 接口逐步扩展到完整的用户认证流程。
36
+ - 从模拟 `user_id` 到使用 `uuid` 确保唯一性。
37
+ - 从默认的 404 错误到自定义的业务错误响应。
38
+ - 从未处理 CORS 到添加 `CORSMiddleware`。
memory-bank/projectbrief.md ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 项目简报 (projectbrief.md)
2
+
3
+ ## 项目名称
4
+ AI-EDU 后端 API
5
+
6
+ ## 项目目标
7
+ 使用 FastAPI 实现 AI-EDU H5 应用的后端 API,提供用户认证(注册、登录)等功能,并使用 SQLite 数据库进行数据存储。
8
+
9
+ ## 核心需求
10
+ - 实现用户注册接口 (`/api/auth/register`)
11
+ - 实现用户登录接口 (`/api/auth/login`)
12
+ - 使用 SQLite 数据库存储用户数据 (user_id, username, password)
13
+ - 接口响应遵循统一的数据结构(code, message, data)
14
+ - 处理跨域请求 (CORS)
15
+
16
+ ## 预期成果
17
+ 一个稳定、可扩展的 FastAPI 后端服务,能够支持 AI-EDU H5 应用的基本用户认证功能。
memory-bank/systemPatterns.md ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 系统模式 (systemPatterns.md)
2
+
3
+ ## 系统架构
4
+ - **后端框架**: FastAPI
5
+ - **数据库**: SQLite
6
+ - **API 路由**: 使用 `APIRouter` 进行模块化管理,所有 API 路径前缀为 `/api`。
7
+ - **CORS**: 使用 `CORSMiddleware` 处理跨域请求。
8
+
9
+ ## 关键技术决策
10
+ - **用户 ID 生成**: 采用 `uuid.uuid4()` 生成全局唯一的用户 ID。
11
+ - **密码存储**: 当前直接存储明文密码(**待改进:未来应使用哈希加密存储**)。
12
+ - **错误处理**:
13
+ - 注册时用户名重复:返回 `400 Bad Request`。
14
+ - 登录时用户名或密码错误:返回自定义业务错误码 `401`。
15
+ - 其他内部服务器错误:返回 `500 Internal Server Error`。
16
+ - **数据库初始化**: 在 FastAPI 应用启动时通过 `@app.on_event("startup")` 钩子函数初始化 SQLite 数据库和 `users` 表。
17
+
18
+ ## 组件关系
19
+ - `app.py`: 包含 FastAPI 应用实例、CORS 配置、数据库初始化逻辑、API 路由定义。
20
+ - `api_router`: 负责定义 `/api/auth/register` 和 `/api/auth/login` 接口。
21
+ - `sqlite3`: 用于与 `ai_edu.db` 数据库文件进行交互。
22
+ - `pydantic.BaseModel`: 用于定义请求体的模型(`RegisterRequest`, `LoginRequest`)。
23
+
24
+ ## 关键实现路径
25
+ - **用户注册**: 接收 `username` 和 `password` -> 生成 `user_id` (UUID) -> 插入 `users` 表 -> 返回成功响应或错误。
26
+ - **用户登录**: 接收 `username` 和 `password` -> 查询 `users` 表验证凭据 -> 返回成功响应(包含 `user_id` 和 `username`)或错误。
memory-bank/techContext.md ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 技术背景 (techContext.md)
2
+
3
+ ## 使用的技术
4
+ - **Python**: 编程语言
5
+ - **FastAPI**: Web 框架
6
+ - **Uvicorn**: ASGI 服务器,用于运行 FastAPI 应用
7
+ - **Pydantic**: 数据验证和设置管理库,用于定义请求体模型
8
+ - **SQLite3**: Python 内置的轻量级数据库
9
+ - **uuid**: Python 标准库,用于生成通用唯一标识符
10
+
11
+ ## 开发环境设置
12
+ - **Conda**: 用于管理 Python 虚拟环境 (`conda activate rzwl`)
13
+ - **依赖安装**: `pip install -r requirements.txt` (包含 `fastapi`, `uvicorn[standard]`)
14
+
15
+ ## 运行命令
16
+ `conda activate rzwl && uvicorn app:app --host 0.0.0.0 --port 7860 --reload`
17
+
18
+ ## 技术约束
19
+ - SQLite 数据库适用于小型项目或开发环境,不适合高并发、大规模生产环境。
20
+ - 密码当前未加密存储,存在安全风险,未来需要引入密码哈希库(如 `passlib`)。
push.sh ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ git add .
2
+ git commit -m "update"
3
+ git push
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ fastapi
2
+ uvicorn[standard]