carcost/api/README.md
2026-04-10 23:24:51 +08:00

69 lines
1.5 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.

# CarCost API
车辆费用追踪后端 API使用 FastAPI + SQLAlchemy + PostgreSQL 构建。
## 项目结构
```
api/
├── main.py # FastAPI 入口
├── config.py # 配置管理
├── database.py # 数据库连接
├── models.py # SQLAlchemy 模型
├── schemas.py # Pydantic 数据模型
├── requirements.txt # 依赖
├── routers/ # API 路由
│ ├── vehicles.py # 车辆管理
│ ├── fuel_records.py # 加油记录
│ ├── costs.py # 费用记录
│ └── dashboard.py # 仪表板
└── .env # 环境变量(不提交)
```
## 快速开始
### 1. 配置环境变量
```bash
cp .env.example .env
# 编辑 .env 文件,设置 DATABASE_URL
```
### 2. 安装依赖
```bash
python -m venv venv
source venv/bin/activate
pip install -r requirements.txt
```
### 3. 启动服务
```bash
python main.py
# 或
uvicorn main:app --host 0.0.0.0 --port 7030 --reload
```
### 4. 访问文档
- API 文档: http://localhost:7030/docs
- 健康检查: http://localhost:7030/health
## API 路由
所有路由前缀为 `/carcost`:
- `GET/POST /carcost/vehicles/*` - 车辆管理
- `GET/POST /carcost/fuel-records/*` - 加油记录
- `GET/POST /carcost/costs/*` - 费用记录
- `GET /carcost/dashboard/*` - 仪表板数据
## 技术栈
- **FastAPI**: Web 框架
- **SQLAlchemy 2.0**: ORM
- **PostgreSQL**: 数据库
- **Pydantic**: 数据验证
- **python-dotenv**: 环境变量管理