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. 配置环境变量
cp .env.example .env
# 编辑 .env 文件,设置 DATABASE_URL
2. 安装依赖
python -m venv venv
source venv/bin/activate
pip install -r requirements.txt
3. 启动服务
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: 环境变量管理