Refactor config, logging, and startup structure

Introduced pkg modules for environment config, logging, and startup initialization. Replaced direct config and logging setup in main.py with modularized functions. Updated database and migration modules to use environment variables and improved DEBUG handling. Removed tool.py and migrated password utilities to pkg. Cleaned up legacy comments and unused code in models and routes.
This commit is contained in:
2025-10-03 15:00:45 +08:00
parent 815e709339
commit c1c36c606f
16 changed files with 214 additions and 185 deletions

40
main.py
View File

@@ -1,25 +1,35 @@
# 初始化数据库
import asyncio
from model.database import Database
asyncio.run(Database().init_db())
from loguru import logger
# 导入
# 导入配置模块
from pkg.env import load_config
from pkg.logger import setup_logging
from pkg.startup import startup
# 加载配置
host, port, debug = load_config()
# 配置日志
setup_logging(debug)
# 记录启动信息
logger.info(f"Debug mode: {'enabled' if debug else 'disabled'}")
logger.info(f"Starting Findreve on http://{host}:{port}")
# 导入应用实例
from app import app
from fastapi.staticfiles import StaticFiles
import logging
# 添加静态文件目录
try:
# 挂载静态文件目录
app.mount("/dist", StaticFiles(directory="dist"), name="dist")
except RuntimeError as e:
logging.warning(f'无法挂载静态目录: {str(e)}, 将启动纯后端模式')
# 执行启动流程
startup(app)
# 作为主程序启动时
if __name__ == '__main__':
import uvicorn
# 启动服务器
uvicorn.run(
'app:app',
host='0.0.0.0',
port=8080
host=host,
port=port,
log_config=None, # 禁用 uvicorn 默认的日志配置,使用 loguru
reload=debug, # 调试模式下启用热重载
)