Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ dist/
*.local

# TestSprite 运行时(含可能敏感配置)
testsprite_tests/tmp/

# IDE / OS
.idea/
Expand Down
Empty file added 111
Empty file.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# 简易前后端项目

## 结构

- **后端**:`backend/main.py`(单文件 FastAPI,内存存储)
- **前端**:`frontend/src/App.tsx` + `main.tsx`(单页待办)

## 启动

**后端**(终端 1):
```bash
cd backend
pip install -r requirements.txt
uvicorn main:app --reload --host 127.0.0.1 --port 8000
```

**前端**(终端 2):
```bash
cd frontend
npm install
npm run dev
```

浏览器打开 http://localhost:3000 ,前端会通过 Vite 代理访问 http://localhost:8000 的 `/api`。
89 changes: 5 additions & 84 deletions backend/README.md
Original file line number Diff line number Diff line change
@@ -1,89 +1,10 @@
# 示例后端项目 (FastAPI)

基于 **FastAPI** 的异步后端,包含用户、分类、商品、订单模块与 JWT 认证,代码量较多,适合学习或二次开发。

## 技术栈

- **框架**: FastAPI
- **数据库**: SQLAlchemy 2.0(异步),默认 SQLite
- **认证**: JWT (python-jose + passlib/bcrypt)
- **校验**: Pydantic v2

## 项目结构

```
backend/
├── app/
│ ├── __init__.py
│ ├── config.py # 配置(环境变量)
│ ├── database.py # 异步数据库与会话
│ ├── main.py # 应用入口
│ ├── exceptions.py # 自定义异常与全局处理
│ ├── api/
│ │ └── routes/ # 路由:health, auth, users, items, categories, orders
│ ├── models/ # SQLAlchemy 模型:User, Category, Item, Order, OrderItem, AuditLog
│ ├── schemas/ # Pydantic 请求/响应模型
│ ├── services/ # 业务逻辑层
│ ├── utils/ # 安全、依赖注入等
│ └── middleware/ # 日志、耗时中间件
├── requirements.txt
└── README.md
```

## 快速开始

### 1. 安装依赖
# 简易后端(单文件)

```bash
cd backend
pip install -r requirements.txt
uvicorn main:app --reload --host 127.0.0.1 --port 8000
```

### 2. 启动服务

```bash
uvicorn app.main:app --reload --host 0.0.0.0 --port 8000
```

- 接口文档: http://127.0.0.1:8000/docs
- ReDoc: http://127.0.0.1:8000/redoc

### 3. 环境变量(可选)

在项目根目录创建 `.env`,例如:

```env
SECRET_KEY=your-secret-key
DATABASE_URL=sqlite+aiosqlite:///./app.db
DEBUG=false
ACCESS_TOKEN_EXPIRE_MINUTES=30
```

## API 概览

| 模块 | 前缀 | 说明 |
|------------|--------------------|----------------|
| 健康检查 | `/api/v1/health` | 基础 / DB 检查 |
| 认证 | `/api/v1/auth` | 登录、刷新、me |
| 用户 | `/api/v1/users` | 注册、列表、CRUD |
| 分类 | `/api/v1/categories` | 分类 CRUD、分页 |
| 商品 | `/api/v1/items` | 商品 CRUD、分页、按分类 |
| 订单 | `/api/v1/orders` | 下单、我的订单、管理员列表 |

除注册、登录、健康检查、商品列表/详情外,其余接口需在请求头携带:

```http
Authorization: Bearer <access_token>
```

## 测试(示例)

```bash
pytest tests/ -v
```

(需先编写 `tests/` 下用例;当前项目已预留依赖 `pytest`、`pytest-asyncio`、`httpx`。)

## 许可证

MIT
- `GET /api/health` 健康检查
- `GET /api/items` 列表
- `POST /api/items` 添加(body: `{"title": "xxx"}`)
4 changes: 0 additions & 4 deletions backend/app/__init__.py

This file was deleted.

3 changes: 0 additions & 3 deletions backend/app/api/__init__.py

This file was deleted.

15 changes: 0 additions & 15 deletions backend/app/api/routes/__init__.py

This file was deleted.

66 changes: 0 additions & 66 deletions backend/app/api/routes/auth.py

This file was deleted.

99 changes: 0 additions & 99 deletions backend/app/api/routes/categories.py

This file was deleted.

36 changes: 0 additions & 36 deletions backend/app/api/routes/health.py

This file was deleted.

Loading