Skip to content

Commit

Permalink
chore: 完善settings检验
Browse files Browse the repository at this point in the history
  • Loading branch information
lczmx committed Mar 5, 2022
1 parent 69d37ea commit e2f91c4
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 28 deletions.
7 changes: 7 additions & 0 deletions app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.idea/
__pycache__/
node_modules/
logs/*
*.sqlite3
test.*
local_settings.py
1 change: 1 addition & 0 deletions app/exceptions/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from exceptions.configExcetion import MissingRequireConfig
6 changes: 6 additions & 0 deletions app/exceptions/configExcetion.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class MissingRequireConfig(Exception):
def __init__(self, config):
self.config = config

def __str__(self):
return f"缺失必须的配置{self.config}!\n请查看README: https://github.com/lczmx/MemoryCard"
5 changes: 1 addition & 4 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import logging
from fastapi import FastAPI
from fastapi.responses import JSONResponse
from fastapi.exceptions import RequestValidationError
Expand All @@ -10,8 +9,6 @@
from logger import CustomizeLogger
import uvicorn

logger = logging.getLogger(__name__)

# 许可信息数据
license_info = {
"name": "GPLv3.0",
Expand Down Expand Up @@ -95,4 +92,4 @@ async def shutdown() -> None:


if __name__ == '__main__':
uvicorn.run(app, port=8000, host="192.168.0.110")
uvicorn.run(app, port=8366, host="0.0.0.0")
63 changes: 39 additions & 24 deletions app/settings.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,60 @@
import os
from fastapi.security import OAuth2PasswordBearer
from passlib.context import CryptContext
import os
from loguru import logger

# ####### 数据库链接
from exceptions import MissingRequireConfig

# #### 日志路径
BASE_DIR = os.path.dirname(os.path.abspath(__file__))

# 形如 mysql+pymysql://root:passwd@127.0.0.1:3306/MemoryCard?charset=utf8mb4
LOG_FILE_PATH = os.path.join(BASE_DIR, "logs")
LOG_FILE_FORMATE = "MemoryCard_{time}.log"

# 日志配置
LOGGING_CONFIG = {
"logger": {
"path": LOG_FILE_PATH,
"filename": LOG_FILE_FORMATE,
"level": "info",
"rotation": "1 MB",
"retention": "1 months",
"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>"

}
}

# ####### 数据库链接
ASYNC_SQLALCHEMY_DATABASE_URL = ''

# ######### jwt
SECRET_KEY = "09d25e094faa6ca2556c818166b7a9563b93f7099f6f0f4caa6cf63b88e8d3e7"
SECRET_KEY = ""
ALGORITHM = "HS256" # jwt加密算法
ACCESS_TOKEN_EXPIRE_MINUTES = 60 * 6 # 访问令牌过期分钟, 默认6小时

oauth2_schema = OAuth2PasswordBearer(tokenUrl="/user/token")

pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")

# ###### 检验配置
if os.path.isfile("local_settings.py"):
from local_settings import *
else:
f = open("local_settings.py", mode="w", encoding="utf-8")
f.close()
raise Exception("请先配置local_settings, 详情见README")

if not SECRET_KEY:
f = open("local_settings.py", mode="a", encoding="utf-8")
# 动态 生成 SECRET_KEY
import secrets

SECRET_KEY = secrets.token_hex(32)
f.write(f"\nSECRET_KEY = '{SECRET_KEY}'\n")
f.close()
logger.add(os.path.join(LOG_FILE_PATH, "init_settings.log"), rotation="1 MB",
format=LOGGING_CONFIG["logger"]["format"]) # 滚动大日志文件
logger.warning("初始化SECRET_KEY")

if not ASYNC_SQLALCHEMY_DATABASE_URL:
raise MissingRequireConfig("ASYNC_SQLALCHEMY_DATABASE_URL")

# ######### 一些初始数据

Expand All @@ -35,20 +67,3 @@
"delete_plan": 6,
"create_plan": 7,
}
# #### 日志路径
BASE_DIR = os.path.dirname(os.path.abspath(__file__))

LOG_FILE_PATH = os.path.join(BASE_DIR, "logs")
LOG_FILE_FORMATE = "MemoryCard_{time}.log"
# 日志配置
LOGGING_CONFIG = {
"logger": {
"path": LOG_FILE_PATH,
"filename": LOG_FILE_FORMATE,
"level": "info",
"rotation": "1 MB",
"retention": "1 months",
"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>"

}
}

0 comments on commit e2f91c4

Please sign in to comment.