2026-05-12 05:03:18 +00:00
|
|
|
|
# User Management Service - 用户管理服务
|
2026-05-12 04:57:40 +00:00
|
|
|
|
|
2026-05-12 05:03:18 +00:00
|
|
|
|
基于 FastAPI 构建的用户管理单体服务,采用内存假数据存储,无需外部依赖。
|
|
|
|
|
|
|
|
|
|
|
|
## 功能特性
|
|
|
|
|
|
|
|
|
|
|
|
- ✅ 用户注册(POST /api/users)
|
|
|
|
|
|
- ✅ 获取用户列表(GET /api/users)
|
|
|
|
|
|
- ✅ 获取单个用户(GET /api/users/{user_id})
|
|
|
|
|
|
- ✅ 更新用户信息(PUT /api/users/{user_id})
|
|
|
|
|
|
- ✅ 删除用户(DELETE /api/users/{user_id})
|
|
|
|
|
|
- ✅ 用户登录验证(POST /api/users/login)
|
|
|
|
|
|
- ✅ 健康检查(GET /health)
|
|
|
|
|
|
|
|
|
|
|
|
## 项目结构
|
|
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
project-files/
|
|
|
|
|
|
├── requirements.txt
|
|
|
|
|
|
├── README.md
|
|
|
|
|
|
├── app/
|
|
|
|
|
|
│ ├── __init__.py
|
|
|
|
|
|
│ ├── main.py # FastAPI 应用入口及路由
|
|
|
|
|
|
│ ├── models.py # 数据模型定义
|
|
|
|
|
|
│ ├── services.py # 业务逻辑层
|
|
|
|
|
|
│ └── database.py # 内存数据库(假数据)
|
|
|
|
|
|
└── tests/
|
|
|
|
|
|
└── test_basic.py # 基础单元测试
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
## 环境要求
|
|
|
|
|
|
|
|
|
|
|
|
- Python >= 3.11
|
|
|
|
|
|
|
|
|
|
|
|
## 安装依赖
|
|
|
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
|
cd codegen-runs/codegen_216c32ee7c414070891e6bd4259621a9
|
|
|
|
|
|
pip install -r requirements.txt
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
## 启动服务
|
|
|
|
|
|
|
|
|
|
|
|
方式一:直接启动
|
|
|
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
|
python app/main.py
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
方式二:使用 uvicorn
|
|
|
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
|
uvicorn app.main:app --reload --host 0.0.0.0 --port 8000
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
服务启动后访问:
|
|
|
|
|
|
- API 文档(Swagger UI):http://localhost:8000/docs
|
|
|
|
|
|
- API 文档(Redoc):http://localhost:8000/redoc
|
|
|
|
|
|
- 健康检查:http://localhost:8000/health
|
|
|
|
|
|
|
|
|
|
|
|
## 运行测试
|
|
|
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
|
cd codegen-runs/codegen_216c32ee7c414070891e6bd4259621a9
|
|
|
|
|
|
pytest -v
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
## API 接口说明
|
|
|
|
|
|
|
|
|
|
|
|
### 健康检查
|
|
|
|
|
|
```
|
|
|
|
|
|
GET /health
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
### 用户注册
|
|
|
|
|
|
```
|
|
|
|
|
|
POST /api/users
|
|
|
|
|
|
Content-Type: application/json
|
|
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
"username": "alice",
|
|
|
|
|
|
"email": "alice@example.com",
|
|
|
|
|
|
"password": "secret123"
|
|
|
|
|
|
}
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
### 获取用户列表
|
|
|
|
|
|
```
|
|
|
|
|
|
GET /api/users?skip=0&limit=10
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
### 获取单个用户
|
|
|
|
|
|
```
|
|
|
|
|
|
GET /api/users/{user_id}
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
### 更新用户
|
|
|
|
|
|
```
|
|
|
|
|
|
PUT /api/users/{user_id}
|
|
|
|
|
|
Content-Type: application/json
|
|
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
"username": "alice_new",
|
|
|
|
|
|
"email": "alice_new@example.com"
|
|
|
|
|
|
}
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
### 删除用户
|
|
|
|
|
|
```
|
|
|
|
|
|
DELETE /api/users/{user_id}
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
### 用户登录
|
|
|
|
|
|
```
|
|
|
|
|
|
POST /api/users/login
|
|
|
|
|
|
Content-Type: application/json
|
|
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
"username": "alice",
|
|
|
|
|
|
"password": "secret123"
|
|
|
|
|
|
}
|
|
|
|
|
|
```
|