Skip to content

Commit c013cda

Browse files
committed
重构后端架构(覆盖推送)
1 parent db700bc commit c013cda

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+4528
-61
lines changed

.github/FUNDING.yml

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
custom: https://wu-clan.github.io/sponsor/

.gitignore

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
__pycache__/
2+
.idea/
3+
.env
4+
.venv/
5+
venv/
6+
backend/log/
7+
backend/migrations/
8+
.ruff_cache/
9+
.pdm-python

.pre-commit-config.yaml

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
repos:
2+
- repo: https://github.com/pre-commit/pre-commit-hooks
3+
rev: v4.4.0
4+
hooks:
5+
- id: check-added-large-files
6+
- id: end-of-file-fixer
7+
- id: check-yaml
8+
- id: check-toml
9+
10+
- repo: https://github.com/charliermarsh/ruff-pre-commit
11+
rev: v0.4.5
12+
hooks:
13+
- id: ruff
14+
args:
15+
- '--config'
16+
- '.ruff.toml'
17+
- '--fix'
18+
- '--unsafe-fixes'
19+
- id: ruff-format
20+
21+
- repo: https://github.com/pdm-project/pdm
22+
rev: 2.12.4
23+
hooks:
24+
- id: pdm-export
25+
args:
26+
- '-o'
27+
- 'requirements.txt'
28+
- '--without-hashes'
29+
files: ^pdm.lock$
30+
- id: pdm-lock-check

.ruff.toml

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
line-length = 120
2+
target-version = "py310"
3+
cache-dir = "./.ruff_cache"
4+
5+
[lint]
6+
select = [
7+
"E",
8+
"F",
9+
"I",
10+
"W505",
11+
"PT018",
12+
"SIM101",
13+
"SIM114",
14+
"PGH004",
15+
"PLE1142",
16+
"RUF100",
17+
"F404",
18+
"TCH",
19+
"UP007"
20+
]
21+
22+
[lint.isort]
23+
lines-between-types = 1
24+
25+
[lint.per-file-ignores]
26+
"backend/app/api/v1/*.py" = ["TCH"]
27+
"backend/app/models/*.py" = ["TCH003"]
28+
"backend/app/**/__init__.py" = ["F401"]
29+
30+
[format]
31+
quote-style = "single"

Dockerfile

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
FROM python:3.10-slim
2+
3+
WORKDIR /ftm
4+
5+
COPY . .
6+
7+
RUN sed -i s@/deb.debian.org/@/mirrors.aliyun.com/@g /etc/apt/sources.list \
8+
&& sed -i s@/security.debian.org/@/mirrors.aliyun.com/@g /etc/apt/sources.list
9+
10+
RUN apt-get update \
11+
&& apt-get install -y --no-install-recommends gcc python3-dev \
12+
&& rm -rf /var/lib/apt/lists/*
13+
14+
RUN pip install --upgrade pip -i https://mirrors.aliyun.com/pypi/simple \
15+
&& pip install --no-cache-dir -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple
16+
17+
ENV TZ = Asia/Shanghai
18+
19+
RUN mkdir -p /var/log/fastapi_server
20+
21+
EXPOSE 8001
22+
23+
CMD ["uvicorn", "backend.main:app", "--host", "127.0.0.1", "--port", "8000"]

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2022 xiaowu
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.en.md

-36
This file was deleted.

README.md

+89-25
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,101 @@
1-
# FastAutoTest
1+
# FastAPI Tortoise Architecture
22

3-
#### 介绍
4-
基于 fastapi + tortoise + mysql8 自动化接口测试平台
3+
作为 FastAPI 框架的一个基础项目,基于 python3.10 开发
54

6-
#### 软件架构
7-
软件架构说明
5+
## 特征
86

7+
- [x] FastAPI > 0.100.0
8+
- [x] Async design
9+
- [x] Restful API
10+
- [x] Tortoise-orm > 0.20.0
11+
- [x] Pydantic 2.0
12+
- [x] Docker
13+
- [ ] ......
914

10-
#### 安装教程
15+
## 使用
1116

12-
1. xxxx
13-
2. xxxx
14-
3. xxxx
17+
> [!WARNING]
18+
> 此过程请格外注意端口占用情况, 特别是 8000, 3306, 6379...
1519
16-
#### 使用说明
20+
### 1: 传统
1721

18-
1. xxxx
19-
2. xxxx
20-
3. xxxx
22+
1. 安装依赖项
2123

22-
#### 参与贡献
24+
```shell
25+
pip install -r requirements.txt
26+
```
2327

24-
1. Fork 本仓库
25-
2. 新建 Feat_xxx 分支
26-
3. 提交代码
27-
4. 新建 Pull Request
28+
2. 创建一个数据库 `ftm`, 选择 `utf8mb4` 编码
29+
3. 安装启动 Redis
30+
4. 进入 backend 目录
2831

32+
```shell
33+
cd backend
34+
```
2935

30-
#### 特技
36+
5. 创建一个 `.env` 文件
3137

32-
1. 使用 Readme\_XXX.md 来支持不同的语言,例如 Readme\_en.md, Readme\_zh.md
33-
2. Gitee 官方博客 [blog.gitee.com](https://blog.gitee.com)
34-
3. 你可以 [https://gitee.com/explore](https://gitee.com/explore) 这个地址来了解 Gitee 上的优秀开源项目
35-
4. [GVP](https://gitee.com/gvp) 全称是 Gitee 最有价值开源项目,是综合评定出的优秀开源项目
36-
5. Gitee 官方提供的使用手册 [https://gitee.com/help](https://gitee.com/help)
37-
6. Gitee 封面人物是一档用来展示 Gitee 会员风采的栏目 [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/)
38+
```shell
39+
touch .env
40+
cp .env.example .env
41+
```
42+
43+
6. 按需修改配置文件 `core/conf.py``.env`
44+
7. 数据库迁移
45+
46+
```shell
47+
# 初始化数据库,生成迁移文件
48+
aerich init-db
49+
50+
# 执行迁移
51+
aerich upgrade
52+
53+
# 当更新数据库 model 后,执行下面两个命令进行迁移
54+
aerich migrate
55+
aerich upgrade
56+
```
57+
58+
8. 启动 fastapi 服务
59+
60+
```shell
61+
# 帮助
62+
fastapi --help
63+
64+
# 开发模式
65+
fastapi dev main.py
66+
```
67+
68+
9. 浏览器访问: http://127.0.0.1:8000/api/v1/docs
69+
70+
---
71+
72+
### 2: docker
73+
74+
1. 进入 `docker-compose.yml` 文件所在目录,创建环境变量文件 `.env`
75+
76+
```shell
77+
dcd deploy/docker-compose/
78+
79+
cp .env.server ../../../backend/.env
80+
```
81+
82+
2. 执行一键启动命令
83+
84+
```shell
85+
docker-compose up -d --build
86+
```
87+
88+
3. 等待命令自动完成
89+
4. 浏览器访问:http://127.0.0.1:8000/api/v1/docs
90+
91+
## 互动
92+
93+
[WeChat / QQ](https://github.com/wu-clan)
94+
95+
## 赞助
96+
97+
如果此项目能够帮助到你,你可以赞助作者一些咖啡豆表示鼓励:[:coffee: Sponsor :coffee:](https://wu-clan.github.io/sponsor/)
98+
99+
## 许可证
100+
101+
本项目根据 MIT 许可证的条款进行许可

backend/.env.example

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Env: dev、pro
2+
ENVIRONMENT='dev'
3+
# MySQL
4+
DB_HOST='127.0.0.1'
5+
DB_PORT=3306
6+
DB_USER='root'
7+
DB_PASSWORD='123456'
8+
# Redis
9+
REDIS_HOST='127.0.0.1'
10+
REDIS_PORT=6379
11+
REDIS_PASSWORD=''
12+
REDIS_DATABASE=0
13+
# Token
14+
TOKEN_SECRET_KEY='1VkVF75nsNABBjK_7-qz7GtzNy3AMvktc9TCPwKczCk'

backend/__init__.py

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-

backend/app/__init__.py

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-

backend/app/admin/__init__.py

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-

backend/app/admin/api/__init__.py

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-

backend/app/admin/api/router.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
from fastapi import APIRouter
4+
5+
from backend.app.admin.api.v1.auth import router as auth_router
6+
from backend.app.admin.api.v1.user import router as user_router
7+
from backend.core.conf import settings
8+
9+
v1 = APIRouter(prefix=settings.API_V1_STR)
10+
11+
v1.include_router(auth_router, prefix='/auth', tags=['认证'])
12+
v1.include_router(user_router, prefix='/users', tags=['用户'])

backend/app/admin/api/v1/__init__.py

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
from fastapi import APIRouter
4+
5+
from backend.app.admin.api.v1.auth.auth import router as auth_router
6+
from backend.app.admin.api.v1.auth.captcha import router as captcha_router
7+
8+
router = APIRouter()
9+
10+
router.include_router(auth_router)
11+
router.include_router(captcha_router)

backend/app/admin/api/v1/auth/auth.py

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
from fastapi import APIRouter, Depends, Request
4+
from fastapi.security import OAuth2PasswordRequestForm
5+
6+
from backend.app.admin.schemas.token import Token
7+
from backend.app.admin.schemas.user import Auth, Auth2
8+
from backend.app.admin.services.user_service import UserService
9+
from backend.common.jwt import DependsJwtUser
10+
from backend.common.response.response_schema import response_base
11+
12+
router = APIRouter()
13+
14+
15+
@router.post('/swagger_login', summary='swagger 表单登录', description='form 格式登录,仅用于 swagger 文档调试接口')
16+
async def login1(form_data: OAuth2PasswordRequestForm = Depends()) -> Token:
17+
token, user = await UserService.login_swagger(form_data)
18+
return Token(access_token=token, user=user)
19+
20+
21+
@router.post('/login', summary='json登录')
22+
async def login2(obj: Auth) -> Token:
23+
token, user = await UserService.login_json(obj)
24+
return Token(access_token=token, user=user)
25+
26+
27+
@router.post('/captcha_login', summary='验证码登录')
28+
async def login3(request: Request, obj: Auth2) -> Token:
29+
token, user = await UserService.login_captcha(obj=obj, request=request)
30+
return Token(access_token=token, user=user)
31+
32+
33+
@router.post('/logout', summary='登出', dependencies=[DependsJwtUser])
34+
async def user_logout():
35+
return await response_base.response_200()
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
from fast_captcha import img_captcha
4+
from fastapi import APIRouter, Depends, Request
5+
from fastapi_limiter.depends import RateLimiter
6+
from starlette.concurrency import run_in_threadpool
7+
from starlette.responses import StreamingResponse
8+
9+
from backend.common.redis import redis_client
10+
from backend.core.conf import settings
11+
from backend.utils.generate_string import get_uuid4_str
12+
13+
router = APIRouter()
14+
15+
16+
@router.get('/captcha', summary='获取验证码', dependencies=[Depends(RateLimiter(times=5, seconds=10))])
17+
async def get_captcha(request: Request):
18+
img, code = await run_in_threadpool(img_captcha)
19+
uuid = get_uuid4_str()
20+
request.app.state.captcha_uuid = uuid
21+
await redis_client.set(uuid, code, settings.CAPTCHA_EXPIRATION_TIME)
22+
return StreamingResponse(content=img, media_type='image/jpeg')

0 commit comments

Comments
 (0)