Skip to content

Commit 2232aff

Browse files
committed
feat(server): use fastapi framework.
1 parent dd3e9d4 commit 2232aff

17 files changed

+227
-162
lines changed

.dockerignore

+4-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,7 @@ conf/*.test.yml
33
__pycache__
44
.DS_Store
55
log/
6-
doc/
6+
doc/
7+
.git/
8+
script/
9+
.idea/

.syncignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.git/
2+
log/
3+
__pycache__
4+
.DS_Store
5+
.idea/

Dockerfile

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM python:3.8-slim
1+
FROM python:3.8-slim-bullseye
22

33
# 设置时间为上海时间
44
ENV TZ=Asia/Shanghai DEBIAN_FRONTEND=noninteractive
@@ -16,7 +16,7 @@ WORKDIR /app
1616

1717
COPY ./requirements.txt /app/requirements.txt
1818

19-
RUN pip install -r requirements.txt -i https://pypi.douban.com/simple
19+
RUN pip install -r requirements.txt -i https://pypi.mirrors.ustc.edu.cn/simple
2020

2121
COPY ./ /app/
2222

README.md

+59-28
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22

33
🐍 一个**快速高性能**的「Python API 模板」
44

5+
## 0 快速启动
6+
7+
```shell
8+
pip install -r requirements.txt
9+
10+
python -m server.main
11+
```
12+
513
## 1 目录简介
614

715
```
@@ -10,15 +18,17 @@
1018
├── server # 部署
1119
│ └── app.py
1220
├── conf # 配置
13-
│ ├── default.prod.yml
14-
│ ├── default.test.yml
15-
│ └── default.yml
21+
│ ├── default.prod.yaml # 生产配置
22+
│ ├── default.test.yaml # 测试配置
23+
│ └── default.yaml # 开发配置
1624
├── util # 辅助
17-
│ ├── arg.py
1825
│ ├── conf.py
1926
│ ├── log.py
20-
│ ├── data_model.py
21-
│ └── db.py
27+
│ └── data_model.py # 类型定义
28+
├── script # 脚本
29+
│ ├── sync.sh # 同步到服务器
30+
│ ├── build.sh # 打包镜像
31+
│ └── run.sh # 运行容器
2232
├── test # 测试(可选)
2333
│ └── api_test.py
2434
├── doc # 文档(可选)
@@ -34,7 +44,7 @@
3444

3545
**一个详尽的文档比什么都重要!** 任何人都可以通过文档快速上手。也许你会说,我的代码就我自己看,不写文档也知道。可是你能保证三个月以后,你还记得你当初写的什么吗?!
3646

37-
一个好的深度学习项目文档应该是怎样的
47+
一个好的项目文档应该是怎样的
3848

3949
1. 环境依赖说明
4050
2. 快速运行脚本
@@ -53,31 +63,43 @@
5363
一些常见的辅助函数,比如:
5464

5565
1. conf 配置
56-
2. arg 参数
57-
3. data_model 数据模型
58-
4. log 日志
59-
5. db 数据库
66+
2. data_model 类型注解
67+
3. log 日志
6068

6169
### 1.3 conf(配置中心)
6270

6371
所有的配置都是用 `YAML`,它比 `json` 更好用,可以在配置中添加注释,并且呈现方式也更为直观!
6472

65-
配置文件的中间名称是「环境变量」,比如 ENV 为 `test` 时,就会读取 `default.test.yml` 文件(ENV 默认为 `dev`,会读取 `default.yml`)。
73+
配置文件的中间名称是「环境变量」,比如 ENV 为 `test` 时,就会读取 `default.test.yaml` 文件(ENV 默认为 `dev`,会读取 `default.yaml`)。
6674

67-
如何使用环境变量 ENV 呢?只需要在执行时,把 `-e test` 添加到命令的后面
75+
如何使用环境变量 ENV 呢?只需要在执行时,把 `ENV=test` 添加到命令的最前面,比如 `ENV=test python -m server.main`
6876

6977
```shell
70-
python -m server.main -e test
78+
# 默认是 ENV 是 dev
79+
python -m server.main
7180
# or
72-
python -m server.main --env test
81+
ENV=dev python -m server.main
7382
```
7483

7584
### 1.4 server(部署)
7685

77-
使用 Web 服务部署到线上环境,推荐使用 [Sanic](https://sanic.dev/zh/)
86+
使用 Web 服务部署到线上环境,推荐使用 [FastAPI](https://fastapi.tiangolo.com/zh/)
87+
88+
1. **高性能**:FastAPI 是基于 Starlette 和 Pydantic 构建的,因此它具有非常高的性能。与其他 Python Web 框架相比,它的速度更快,甚至可以与 Node.js 和 Go 这样的高性能语言竞争。
89+
2. **易用性和开发效率**:FastAPI 使用了现代 Python 特性,如类型提示,使得代码易于阅读和编写。这有助于提高开发者的生产力以及减少错误。另外,它自动生成 API 文档(使用 OpenAPI 和 JSON Schema),方便开发者查看和测试 API 接口。
90+
3. **丰富地插件和社区支持**:FastAPI 提供了许多内置组件,如身份验证、安全、数据验证等,可帮助您快速构建 Web 应用。同时,由于 FastAPI 的普及,其拥有一个庞大的活跃社区,为开发者提供大量第三方库和技术支持。
91+
92+
> 在过去本项目推荐使用 [Sanic](https://sanic.dev/zh/),你可以在这个分支 [branch/sanic](https://github.com/Ailln/python-api-template/tree/sanic) 找到过去的代码。
7893
7994
```shell
95+
# 开发环境运行
8096
python -m server.main
97+
98+
# 测试环境运行
99+
ENV=test python -m server.main
100+
101+
# 生产环境运行
102+
ENV=prod python -m server.main
81103
```
82104

83105
### 1.5 test(测试)
@@ -102,14 +124,15 @@ locust -f test/api_test.py -u 10 -r 1
102124

103125
### 1.7 log(日志)
104126

105-
`util/log.py` 会将日志按天记录到这里
127+
`util/log.py` 会将日志按天记录到 log 目录下
106128

107129
### 1.8 requirements.txt (依赖)
108130

109131
项目所需要的依赖,方便一键安装。
110132

111133
```shell
112-
pip install -r requirements.txt -i https://pypi.douban.com/simple
134+
# 直接安装
135+
pip install -r requirements.txt -i https://pypi.mirrors.ustc.edu.cn/simple
113136
```
114137

115138
参考:
@@ -128,31 +151,39 @@ pip install -r requirements.txt -i https://pypi.douban.com/simple
128151
## 2 环境准备
129152

130153
- Ubuntu 18.04 LTS+
131-
- Python 3.7+
132-
- Anaconda 3
154+
- Python 3.8+
155+
- Docker 21+
133156

134157
> 人生苦短,快用 `*NIX`
135158
136159
## 3 容器化部署
137160

138161
```shell
139162
cd python-api-template
140-
# 打包
141-
docker build -t python-api:0.1.0 .
142163

143-
# 运行
144-
docker run -d --name python-api -p 9999:9999 \
145-
-v $PWD/conf/default.test.yaml:/app/conf/default.yaml \
146-
python-api:0.1.0
164+
# 直接打包
165+
docker build -t python-api-template:1.0.0 .
166+
# or 使用脚本打包
167+
bash script/build.sh
168+
169+
# 直接运行
170+
docker run -d --restart=always \
171+
--name python-api \
172+
-p 9999:9999 \
173+
-v $PWD/conf/:/app/conf \
174+
-e ENV=prod \
175+
python-api-template:1.0.0
176+
# or 使用脚本运行
177+
bash script/run.sh
147178

148179
# 查看
149-
docker logs -f python-api
180+
docker logs -f python-api-template
150181

151182
# 调用 API
152183
curl --location --request POST 'http://127.0.0.1:9999/test' \
153184
--header 'Content-Type: application/json' \
154185
--data-raw '{
155-
"name": "test"
186+
"data": "test"
156187
}'
157188
```
158189

conf/default.prod.yaml

+6-12
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
1-
version: 0.1.0
2-
31
server:
4-
name: python-api
5-
version: 0.1.0
6-
host: 0.0.0.0
7-
port: 9999
8-
debug: false
9-
auto_reload: false
10-
11-
db:
12-
mongo:
13-
url: "mongodb://127.0.0.1:27017"
2+
name: python-api-template
3+
version: 1.0.0
4+
run:
5+
host: 0.0.0.0
6+
port: 9999
7+
reload: False

conf/default.test.yaml

+6-12
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
1-
version: 0.1.0
2-
31
server:
4-
name: python-api
5-
version: 0.1.0
6-
host: 0.0.0.0
7-
port: 9999
8-
debug: false
9-
auto_reload: false
10-
11-
db:
12-
mongo:
13-
url: "mongodb://127.0.0.1:27017"
2+
name: python-api-template
3+
version: 1.0.0
4+
run:
5+
host: 0.0.0.0
6+
port: 9999
7+
reload: True

conf/default.yaml

+6-12
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
1-
version: 0.1.0
2-
31
server:
4-
name: python-api
5-
version: 0.1.0
6-
host: 0.0.0.0
7-
port: 9999
8-
debug: true
9-
auto_reload: true
10-
11-
db:
12-
mongo:
13-
url: "mongodb://127.0.0.1:27017"
2+
name: python-api-template
3+
version: 1.0.0
4+
run:
5+
host: 0.0.0.0
6+
port: 9999
7+
reload: True

requirements.txt

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
fastapi>=0.103.1
2+
uvicorn>=0.23.2
13
ruamel.yaml>=0.17.21
2-
sanic>=21.3.4
34
pydantic>=1.9.0
45
locust>=2.2.3
5-
pymongo>=3.10.1
6+
pymongo>=3.10.1
7+
colorlog>=6.7.0

script/build.sh

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export PROJECT_NAME="python-api-template"
2+
3+
echo "${PROJECT_NAME} building..."
4+
docker build -t $PROJECT_NAME .
5+
echo "${PROJECT_NAME} build success"

script/run.sh

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export PROJECT_NAME="python-api-template"
2+
export PORT=9999
3+
4+
echo "${PROJECT_NAME} deploying..."
5+
6+
docker run -d --restart=always \
7+
--name $PROJECT_NAME \
8+
-p 9999:9999 \
9+
-v ./conf/:/app/conf \
10+
-e ENV=prod \
11+
$PROJECT_NAME
12+
13+
echo "${PROJECT_NAME} deployed"

script/sync.sh

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
rsync -e ssh -av \
2+
--exclude-from=.syncignore --progress \
3+
./ root@127.0.0.1:/root/project/python-api-template

0 commit comments

Comments
 (0)