Skip to content

Commit e2f91c4

Browse files
committed
chore: 完善settings检验
1 parent 69d37ea commit e2f91c4

5 files changed

Lines changed: 54 additions & 28 deletions

File tree

app/.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.idea/
2+
__pycache__/
3+
node_modules/
4+
logs/*
5+
*.sqlite3
6+
test.*
7+
local_settings.py

app/exceptions/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from exceptions.configExcetion import MissingRequireConfig

app/exceptions/configExcetion.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class MissingRequireConfig(Exception):
2+
def __init__(self, config):
3+
self.config = config
4+
5+
def __str__(self):
6+
return f"缺失必须的配置{self.config}!\n请查看README: https://github.com/lczmx/MemoryCard"

app/main.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import logging
21
from fastapi import FastAPI
32
from fastapi.responses import JSONResponse
43
from fastapi.exceptions import RequestValidationError
@@ -10,8 +9,6 @@
109
from logger import CustomizeLogger
1110
import uvicorn
1211

13-
logger = logging.getLogger(__name__)
14-
1512
# 许可信息数据
1613
license_info = {
1714
"name": "GPLv3.0",
@@ -95,4 +92,4 @@ async def shutdown() -> None:
9592

9693

9794
if __name__ == '__main__':
98-
uvicorn.run(app, port=8000, host="192.168.0.110")
95+
uvicorn.run(app, port=8366, host="0.0.0.0")

app/settings.py

Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,60 @@
1+
import os
12
from fastapi.security import OAuth2PasswordBearer
23
from passlib.context import CryptContext
3-
import os
44
from loguru import logger
55

6-
# ####### 数据库链接
6+
from exceptions import MissingRequireConfig
7+
8+
# #### 日志路径
9+
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
710

8-
# 形如 mysql+pymysql://root:passwd@127.0.0.1:3306/MemoryCard?charset=utf8mb4
11+
LOG_FILE_PATH = os.path.join(BASE_DIR, "logs")
12+
LOG_FILE_FORMATE = "MemoryCard_{time}.log"
13+
14+
# 日志配置
15+
LOGGING_CONFIG = {
16+
"logger": {
17+
"path": LOG_FILE_PATH,
18+
"filename": LOG_FILE_FORMATE,
19+
"level": "info",
20+
"rotation": "1 MB",
21+
"retention": "1 months",
22+
"format": "<level>{level: <8}</level> <green>{time:YYYY-MM-DD HH:mm:ss.SSS}</green> request id: {extra[request_id]} - <cyan>{name}</cyan>:<cyan>{function}</cyan> - <level>{message}</level>"
23+
24+
}
25+
}
26+
27+
# ####### 数据库链接
928
ASYNC_SQLALCHEMY_DATABASE_URL = ''
1029

1130
# ######### jwt
12-
SECRET_KEY = "09d25e094faa6ca2556c818166b7a9563b93f7099f6f0f4caa6cf63b88e8d3e7"
31+
SECRET_KEY = ""
1332
ALGORITHM = "HS256" # jwt加密算法
1433
ACCESS_TOKEN_EXPIRE_MINUTES = 60 * 6 # 访问令牌过期分钟, 默认6小时
15-
1634
oauth2_schema = OAuth2PasswordBearer(tokenUrl="/user/token")
17-
1835
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
1936

37+
# ###### 检验配置
2038
if os.path.isfile("local_settings.py"):
2139
from local_settings import *
2240
else:
2341
f = open("local_settings.py", mode="w", encoding="utf-8")
2442
f.close()
25-
raise Exception("请先配置local_settings, 详情见README")
43+
44+
if not SECRET_KEY:
45+
f = open("local_settings.py", mode="a", encoding="utf-8")
46+
# 动态 生成 SECRET_KEY
47+
import secrets
48+
49+
SECRET_KEY = secrets.token_hex(32)
50+
f.write(f"\nSECRET_KEY = '{SECRET_KEY}'\n")
51+
f.close()
52+
logger.add(os.path.join(LOG_FILE_PATH, "init_settings.log"), rotation="1 MB",
53+
format=LOGGING_CONFIG["logger"]["format"]) # 滚动大日志文件
54+
logger.warning("初始化SECRET_KEY")
55+
56+
if not ASYNC_SQLALCHEMY_DATABASE_URL:
57+
raise MissingRequireConfig("ASYNC_SQLALCHEMY_DATABASE_URL")
2658

2759
# ######### 一些初始数据
2860

@@ -35,20 +67,3 @@
3567
"delete_plan": 6,
3668
"create_plan": 7,
3769
}
38-
# #### 日志路径
39-
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
40-
41-
LOG_FILE_PATH = os.path.join(BASE_DIR, "logs")
42-
LOG_FILE_FORMATE = "MemoryCard_{time}.log"
43-
# 日志配置
44-
LOGGING_CONFIG = {
45-
"logger": {
46-
"path": LOG_FILE_PATH,
47-
"filename": LOG_FILE_FORMATE,
48-
"level": "info",
49-
"rotation": "1 MB",
50-
"retention": "1 months",
51-
"format": "<level>{level: <8}</level> <green>{time:YYYY-MM-DD HH:mm:ss.SSS}</green> request id: {extra[request_id]} - <cyan>{name}</cyan>:<cyan>{function}</cyan> - <level>{message}</level>"
52-
53-
}
54-
}

0 commit comments

Comments
 (0)