fastapi-demo/DEV_CONFIG.md

138 lines
3.1 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 开发环境配置总结
## 🐍 Python 环境
### Conda 环境配置
- **环境名称**: fastapi-demo (或您的自定义名称)
- **Python版本**: 3.11
- **激活方式**: `conda activate fastapi-demo`
### 环境准备
```bash
# 1. 激活 conda 环境(每次开发前必须执行)
conda activate fastapi-demo
# 2. 安装项目依赖
pip install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple/
# 3. 验证环境
python --version # 应显示 Python 3.11.x
pip list # 查看已安装的包
```
## 🎯 数据库配置
### 连接信息
- **主机**: localhost
- **端口**: 3306
- **用户名**: root
- **密码**: 123456
- **数据库名**: fast_demo
- **字符集**: utf8mb4
### 连接URL
```
mysql+pymysql://root:123456@loc alhost:3306/fast_demo?charset=utf8mb4
```
## 📁 配置文件
### 1. 应用配置 (`app/core/config.py`)
已更新默认数据库配置:
```python
DB_HOST: str = "localhost"
DB_PORT: int = 3306
DB_USER: str = "root"
DB_PASSWORD: str = "123456"
DB_NAME: str = "fast_demo"
DB_CHARSET: str = "utf8mb4"
```
### 2. Alembic配置 (`alembic.ini`)
已更新数据库URL
```ini
sqlalchemy.url = mysql+pymysql://root:123456@localhost:3306/fast_demo?charset=utf8mb4
```
## 🗄️ 数据库表
### 用户表 (users)
| 字段 | 类型 | 说明 |
|------|------|------|
| id | int(11) | 主键,自增 |
| username | varchar(50) | 用户名,唯一 |
| email | varchar(100) | 邮箱,唯一 |
| full_name | varchar(100) | 全名,可空 |
| is_active | tinyint(1) | 是否激活 |
| created_at | datetime | 创建时间 |
| updated_at | datetime | 更新时间 |
### 产品表 (products)
| 字段 | 类型 | 说明 |
|------|------|------|
| id | int(11) | 主键,自增 |
| name | varchar(200) | 产品名称 |
| description | text | 产品描述,可空 |
| price | decimal(10,2) | 价格 |
| stock | int(11) | 库存数量 |
| is_available | tinyint(1) | 是否可用 |
| created_at | datetime | 创建时间 |
| updated_at | datetime | 更新时间 |
## 🚀 快速开始
### 1. 激活环境并启动应用
```bash
# 激活 conda 环境
conda activate fastapi-demo
# 启动应用
python main.py
# 或指定环境启动
set ENVIRONMENT=development && python main.py
```
### 2. 访问API文档
- Swagger UI: http://127.0.0.1:8000/docs
- ReDoc: http://127.0.0.1:8000/redoc
### 3. 数据库管理
```bash
# 初始化数据库
python database_manager.py init
# 重置数据库
python database_manager.py reset
# 查看帮助
python database_manager.py help
```
## 🔧 环境变量
可以通过环境变量覆盖配置:
```bash
export DB_HOST=localhost
export DB_PORT=3306
export DB_USER=root
export DB_PASSWORD=123456
export DB_NAME=fast_demo
```
## ✅ 验证配置
数据库连接已成功验证:
- ✅ 数据库 'fast_demo' 已存在
- ✅ 数据库连接成功
- ✅ 表 'users' 和 'products' 已创建
- ✅ 示例数据已插入
## 📝 注意事项
1. 确保MySQL服务正在运行
2. 确保用户 'root' 有访问数据库的权限
3. 开发环境配置已优化,包含热重载等功能
4. 生产环境请修改默认密码和配置