Skip to content

Commit ade9276

Browse files
committed
use pdm
1 parent 4cd976a commit ade9276

11 files changed

+908
-29
lines changed

.dockerignore

+1
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ test
99
static
1010
uploads
1111
!./uploads/default.jpg
12+
build

.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,6 @@ static/openapi.json
4141

4242
data
4343
nginx/log
44-
packages
44+
packages.pdm-python
45+
.pdm-python
46+
build

Dockerfile

+5-3
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ FROM python:3.10-buster
22

33
WORKDIR /code
44

5-
COPY ./requirements.txt /code/requirements.txt
6-
7-
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
5+
# install PDM
6+
RUN pip install -U pip setuptools wheel
7+
RUN pip install pdm
88

99
COPY ./ /code/
1010

11+
RUN mkdir __pypackages__ && pdm install --prod --no-lock --no-editable
12+
1113
CMD python app.py

README.md

+13-14
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ docker部署nginx,mysql,python3.10,nginx反向代理/api路径(api都以/api开
5050
├─api //fastapi子路由,数据校验模型
5151
├─sql //数据库连接,crud代码,orm模型
5252
├─config //读取配置文件,环境变量
53+
| ├─config.yaml //主要是mysql信息,fasapi配置
5354
├─app.py //fastapi主文件
54-
├─config.yaml //主要是mysql信息,fasapi配置
5555
├─...如其名
5656
5757
```
@@ -60,35 +60,34 @@ docker部署nginx,mysql,python3.10,nginx反向代理/api路径(api都以/api开
6060

6161
## 后端
6262

63-
1. python>3.10 pip安装依赖
63+
1. 安装pdm
6464

65-
2. 最好使用虚拟环境
6665
```bash
67-
pip install -r requirements.txt
66+
pip install --user pdm
6867
```
6968

70-
导出
69+
2. 创建虚拟环境
7170

7271
```bash
73-
pip download -d packages/ -r requirements.txt
72+
pdm venv create 3.11
7473
```
7574

76-
离线安装导入
75+
3. 安装依赖库
7776

7877
```bash
79-
pip install --no-index --find-links=packages -r requirements.txt
78+
pdm install --prod --no-lock --no-editable
8079
```
8180

82-
83-
84-
2. 配置
81+
3. 配置
8582

8683
修改`config.yaml`
8784
> 修改数据库用户名密码,创建数据库,数据库名填入dbname
88-
>
89-
> 修改默认用户名密码
85+
>
86+
> 修改默认用户名密码,
87+
>
88+
> 环境变量的mysql 数据库信息优先级更高
9089
91-
3. 启动
90+
4. 启动
9291

9392
```bash
9493
python app.py

app.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,7 @@ async def custom_http_exception_handler(request: Request, exc):
5454
async def shutdown():
5555
await engine.dispose()
5656

57-
58-
if __name__ == '__main__':
57+
def main():
5958
uvicorn.run(app='app:app',
6059
host=Config["uvicorn"]["host"],
6160
port=Config["uvicorn"]["port"],
@@ -64,3 +63,7 @@ async def shutdown():
6463
headers=[("server", "fastapi")],
6564
proxy_headers=False
6665
)
66+
67+
if __name__ == '__main__':
68+
main()
69+

config.yaml config/config.yaml

File renamed without changes.

config/options.py

+21-8
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,16 @@
1515
from api.password import hash_password, verify_password
1616
from sql.dbModels import User, Base
1717

18-
ppath = os.path.dirname(__file__).replace('config', '')
19-
with open(os.path.join(ppath, 'config.yaml'), 'r', encoding='utf8') as f:
20-
Config = yaml.safe_load(f)
18+
ppath = os.path.dirname(__file__)
19+
Config = None
20+
try:
21+
with open(os.path.join(ppath, 'config.yaml'), 'r', encoding='utf8') as f:
22+
Config = yaml.safe_load(f)
23+
except FileNotFoundError as e:
24+
print(e)
25+
p = input('input config.yaml path:')
26+
with open(p, 'r', encoding='utf8') as f:
27+
Config = yaml.safe_load(f)
2128

2229
if 'MYSQL_HOST' in os.environ:
2330
print("use env mysql config")
@@ -30,11 +37,17 @@
3037
class sync_session:
3138
d = Config["databases"]
3239
_engine = create_engine(f'mysql+pymysql://{d["username"]}:{d["password"]}@{d["host"]}:{d["port"]}')
33-
_conn = _engine.connect()
34-
_conn.execute(text(f'CREATE DATABASE IF NOT EXISTS {d["dbname"]};'))
35-
_conn.close()
36-
_engine.dispose()
37-
40+
try:
41+
print(f'尝试连接mysql {d["host"]}:{d["port"]}')
42+
_conn = _engine.connect()
43+
print(f'execute CREATE DATABASE IF NOT EXISTS {d["dbname"]}')
44+
_conn.execute(text(f'CREATE DATABASE IF NOT EXISTS {d["dbname"]};'))
45+
_conn.close()
46+
_engine.dispose()
47+
except Exception as e:
48+
print('数据库连接失败\n',e)
49+
sys.exit()
50+
print(f'mysql连接成功')
3851
engine = create_engine(
3952
f'mysql+pymysql://{d["username"]}:{d["password"]}@{d["host"]}:{d["port"]}/{d["dbname"]}',
4053
echo=False,

pack.cmd

-1
This file was deleted.

0 commit comments

Comments
 (0)