Skip to content

Commit dd3e9d4

Browse files
committed
feat: add base code.
0 parents  commit dd3e9d4

21 files changed

+587
-0
lines changed

.dockerignore

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
conf/*.prod.yml
2+
conf/*.test.yml
3+
__pycache__
4+
.DS_Store
5+
log/
6+
doc/

.gitignore

+143
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
6+
# C extensions
7+
*.so
8+
9+
# Distribution / packaging
10+
.Python
11+
build/
12+
develop-eggs/
13+
dist/
14+
downloads/
15+
eggs/
16+
.eggs/
17+
lib/
18+
lib64/
19+
parts/
20+
sdist/
21+
var/
22+
wheels/
23+
share/python-wheels/
24+
*.egg-info/
25+
.installed.cfg
26+
*.egg
27+
MANIFEST
28+
29+
# PyInstaller
30+
# Usually these files are written by a python script from a template
31+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
32+
*.manifest
33+
*.spec
34+
35+
# Installer logs
36+
pip-log.txt
37+
pip-delete-this-directory.txt
38+
39+
# Unit test / coverage reports
40+
htmlcov/
41+
.tox/
42+
.nox/
43+
.coverage
44+
.coverage.*
45+
.cache
46+
nosetests.xml
47+
coverage.xml
48+
*.cover
49+
*.py,cover
50+
.hypothesis/
51+
.pytest_cache/
52+
cover/
53+
54+
# Translations
55+
*.mo
56+
*.pot
57+
58+
# Django stuff:
59+
*.log
60+
local_settings.py
61+
db.sqlite3
62+
db.sqlite3-journal
63+
64+
# Flask stuff:
65+
instance/
66+
.webassets-cache
67+
68+
# Scrapy stuff:
69+
.scrapy
70+
71+
# Sphinx documentation
72+
docs/_build/
73+
74+
# PyBuilder
75+
.pybuilder/
76+
target/
77+
78+
# Jupyter Notebook
79+
.ipynb_checkpoints
80+
81+
# IPython
82+
profile_default/
83+
ipython_config.py
84+
85+
# pyenv
86+
# For a library or package, you might want to ignore these files since the code is
87+
# intended to run in multiple environments; otherwise, check them in:
88+
# .python-version
89+
90+
# pipenv
91+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
92+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
93+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
94+
# install all needed dependencies.
95+
#Pipfile.lock
96+
97+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
98+
__pypackages__/
99+
100+
# Celery stuff
101+
celerybeat-schedule
102+
celerybeat.pid
103+
104+
# SageMath parsed files
105+
*.sage.py
106+
107+
# Environments
108+
.env
109+
.venv
110+
env/
111+
venv/
112+
ENV/
113+
env.bak/
114+
venv.bak/
115+
116+
# Spyder project settings
117+
.spyderproject
118+
.spyproject
119+
120+
# Rope project settings
121+
.ropeproject
122+
123+
# mkdocs documentation
124+
/site
125+
126+
# mypy
127+
.mypy_cache/
128+
.dmypy.json
129+
dmypy.json
130+
131+
# Pyre type checker
132+
.pyre/
133+
134+
# pytype static type analyzer
135+
.pytype/
136+
137+
# Cython debug symbols
138+
cython_debug/
139+
140+
# my
141+
.vscode/
142+
.idea/
143+
.DS_Store

Dockerfile

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
FROM python:3.8-slim
2+
3+
# 设置时间为上海时间
4+
ENV TZ=Asia/Shanghai DEBIAN_FRONTEND=noninteractive
5+
6+
RUN sed -i s/deb.debian.org/mirrors.ustc.edu.cn/g /etc/apt/sources.list
7+
8+
RUN apt update \
9+
&& apt install -y tzdata \
10+
&& ln -fs /usr/share/zoneinfo/${TZ} /etc/localtime \
11+
&& echo ${TZ} > /etc/timezone \
12+
&& dpkg-reconfigure --frontend noninteractive tzdata \
13+
&& rm -rf /var/lib/apt/lists/*
14+
15+
WORKDIR /app
16+
17+
COPY ./requirements.txt /app/requirements.txt
18+
19+
RUN pip install -r requirements.txt -i https://pypi.douban.com/simple
20+
21+
COPY ./ /app/
22+
23+
CMD python -m server.main

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2022 Ailln
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.md

+167
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
# Python API Template
2+
3+
🐍 一个**快速高性能**的「Python API 模板」
4+
5+
## 1 目录简介
6+
7+
```
8+
.
9+
├── README.md # 简介
10+
├── server # 部署
11+
│ └── app.py
12+
├── conf # 配置
13+
│ ├── default.prod.yml
14+
│ ├── default.test.yml
15+
│ └── default.yml
16+
├── util # 辅助
17+
│ ├── arg.py
18+
│ ├── conf.py
19+
│ ├── log.py
20+
│ ├── data_model.py
21+
│ └── db.py
22+
├── test # 测试(可选)
23+
│ └── api_test.py
24+
├── doc # 文档(可选)
25+
├── log # 日志
26+
├── Dockerfile # Docker 打包配置(可选)
27+
├── .dockerignore # Docker 忽略配置(可选)
28+
├── .gitignore # Git 忽略配置
29+
├── requirements.txt # 依赖(可选)
30+
└── LICENSE # 许可
31+
```
32+
33+
### 1.1 README.md(文档)
34+
35+
**一个详尽的文档比什么都重要!** 任何人都可以通过文档快速上手。也许你会说,我的代码就我自己看,不写文档也知道。可是你能保证三个月以后,你还记得你当初写的什么吗?!
36+
37+
一个好的深度学习项目文档应该是怎样的?
38+
39+
1. 环境依赖说明
40+
2. 快速运行脚本
41+
3. 类似项目比较
42+
4. 性能测试结果
43+
5. 细致版本记录
44+
6. 相关参考资料
45+
46+
参考:
47+
48+
1. [如何写好Github中的readme?](https://www.zhihu.com/question/29100816)
49+
2. [如何为你的开源项目编写实用的文档](https://zhuanlan.zhihu.com/p/120399648)
50+
51+
### 1.2 util(辅助工具)
52+
53+
一些常见的辅助函数,比如:
54+
55+
1. conf 配置
56+
2. arg 参数
57+
3. data_model 数据模型
58+
4. log 日志
59+
5. db 数据库
60+
61+
### 1.3 conf(配置中心)
62+
63+
所有的配置都是用 `YAML`,它比 `json` 更好用,可以在配置中添加注释,并且呈现方式也更为直观!
64+
65+
配置文件的中间名称是「环境变量」,比如 ENV 为 `test` 时,就会读取 `default.test.yml` 文件(ENV 默认为 `dev`,会读取 `default.yml`)。
66+
67+
如何使用环境变量 ENV 呢?只需要在执行时,把 `-e test` 添加到命令的后面。
68+
69+
```shell
70+
python -m server.main -e test
71+
# or
72+
python -m server.main --env test
73+
```
74+
75+
### 1.4 server(部署)
76+
77+
使用 Web 服务部署到线上环境,推荐使用 [Sanic](https://sanic.dev/zh/)
78+
79+
```shell
80+
python -m server.main
81+
```
82+
83+
### 1.5 test(测试)
84+
85+
`api_test.py` 是对 API 接口进行压力测试,得到 QPS(每秒请求数)。
86+
87+
`Locust` 是一个非常好用的测试工具,它附带一个 WEB 界面,非常方便地在浏览器中进行压测。
88+
89+
```shell
90+
locust -f test/api_test.py -u 10 -r 1
91+
```
92+
93+
在浏览器中打开 `http://127.0.0.1:8089`,点击 `Start swarming` 按钮,就可以开始压测了!
94+
95+
参考:
96+
1. [Python unittest: 单元测试框架](https://docs.python.org/zh-cn/3/library/unittest.html)
97+
2. [Locust: An open source load testing tool.](https://locust.io)
98+
99+
### 1.6 doc(文档|可选)
100+
101+
如果项目比较复杂,可以将文档整理归纳到这里。
102+
103+
### 1.7 log(日志)
104+
105+
`util/log.py` 会将日志按天记录到这里。
106+
107+
### 1.8 requirements.txt (依赖)
108+
109+
项目所需要的依赖,方便一键安装。
110+
111+
```shell
112+
pip install -r requirements.txt -i https://pypi.douban.com/simple
113+
```
114+
115+
参考:
116+
117+
1. [有关 pip 的使用](https://www.v2ai.cn/2019/12/20/python/7-pip/)
118+
119+
### 1.9 LICENSE(许可|可选)
120+
121+
如果是开源项目,需要添加许可证。
122+
123+
参考:
124+
125+
1. [一文看懂开源许可证丨开源知识科普](https://pingcap.com/zh/blog/introduction-of-open-source-license)
126+
2. [如何选择开源许可证?](https://www.ruanyifeng.com/blog/2011/05/how_to_choose_free_software_licenses.html)
127+
128+
## 2 环境准备
129+
130+
- Ubuntu 18.04 LTS+
131+
- Python 3.7+
132+
- Anaconda 3
133+
134+
> 人生苦短,快用 `*NIX`
135+
136+
## 3 容器化部署
137+
138+
```shell
139+
cd python-api-template
140+
# 打包
141+
docker build -t python-api:0.1.0 .
142+
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
147+
148+
# 查看
149+
docker logs -f python-api
150+
151+
# 调用 API
152+
curl --location --request POST 'http://127.0.0.1:9999/test' \
153+
--header 'Content-Type: application/json' \
154+
--data-raw '{
155+
"name": "test"
156+
}'
157+
```
158+
159+
## 4 参考
160+
161+
- [Ubuntu 系统镜像下载](https://cn.ubuntu.com/download)
162+
- [Anaconda 个人版](https://www.anaconda.com/products/individual#)
163+
- [TUNA 清华大学开源软件镜像站](https://mirrors.tuna.tsinghua.edu.cn/)
164+
165+
## 5 许可证
166+
167+
[![](https://award.dovolopor.com?lt=License&rt=MIT&rbc=green)](./LICENSE)

conf/default.prod.yaml

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
version: 0.1.0
2+
3+
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"

conf/default.test.yaml

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
version: 0.1.0
2+
3+
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"

0 commit comments

Comments
 (0)