modify: jwt 验证
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
[default]
|
||||
app.name = "{{ cookiecutter.project_name }}"
|
||||
app.version = "{{ cookiecutter.project_name }}"
|
||||
app.description = "基于 FastAPI 和 Tortoise-ORM 的 chongming 打包脚手架"
|
||||
app.description = "基于 FastAPI 和 Tortoise-ORM 的 {{ cookiecutter.project_name }} 打包脚手架"
|
||||
app.debug = false
|
||||
env = "production"
|
||||
|
||||
@@ -29,6 +29,13 @@ readonly = false
|
||||
secret_key = "your-secret-key-change-in-production"
|
||||
algorithm = "HS256"
|
||||
access_token_expire_minutes = 30
|
||||
refresh_token_expire_days = 7
|
||||
|
||||
[jwt_cache]
|
||||
enabled = true
|
||||
db_path = "./cache"
|
||||
cleanup_interval_hours = 24
|
||||
max_sessions_per_user = 5
|
||||
|
||||
[development]
|
||||
env = "development"
|
||||
|
||||
@@ -1,36 +1,36 @@
|
||||
# main.py - 修复 reload 问题
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
FastAPI + Tortoise-ORM + Chongming 示例
|
||||
主入口文件,适配 chongming 打包系统
|
||||
FastAPI + Tortoise-ORM + {{ cookiecutter.project_name }} 示例
|
||||
主入口文件,适配 {{ cookiecutter.project_name }} 打包系统
|
||||
"""
|
||||
import sys
|
||||
import os
|
||||
from utils.launch import launch
|
||||
from multiprocessing import freeze_support
|
||||
|
||||
|
||||
def app_run(app_config: dict, default_config: dict):
|
||||
"""
|
||||
应用运行函数 - 适配模块银行环境
|
||||
|
||||
|
||||
Args:
|
||||
app_config: 应用配置
|
||||
default_config: 默认配置
|
||||
"""
|
||||
import uvicorn
|
||||
from app.logger import setup_logging, get_logger # type: ignore
|
||||
from app.logger import setup_logging, get_logger # type: ignore
|
||||
|
||||
setup_logging(app_config["logging"])
|
||||
logger = get_logger("app")
|
||||
|
||||
|
||||
# 获取服务器配置
|
||||
server_config = app_config.get("server", {})
|
||||
host = server_config.get("host", "0.0.0.0")
|
||||
port = server_config.get("port", 8000)
|
||||
|
||||
|
||||
# 打包环境强制禁用重载
|
||||
is_frozen = getattr(sys, 'frozen', False)
|
||||
is_frozen = getattr(sys, "frozen", False)
|
||||
reload = False if is_frozen else server_config.get("reload", False)
|
||||
workers = server_config.get("workers", 4)
|
||||
|
||||
@@ -46,7 +46,7 @@ def app_run(app_config: dict, default_config: dict):
|
||||
logger.info(f"🔄 重载: {'启用' if reload else '禁用'}")
|
||||
logger.info(f"👥 Workers: {workers}")
|
||||
logger.info("=" * 50)
|
||||
|
||||
|
||||
# 关键修改:根据 reload 模式选择不同的启动方式
|
||||
|
||||
uvicorn.run(
|
||||
@@ -58,17 +58,19 @@ def app_run(app_config: dict, default_config: dict):
|
||||
log_level="debug" if app_config.get("debug", False) else "info",
|
||||
access_log=True,
|
||||
)
|
||||
|
||||
|
||||
|
||||
def main():
|
||||
freeze_support()
|
||||
# 如果是打包后的可执行文件,调整路径
|
||||
if getattr(sys, 'frozen', False):
|
||||
if getattr(sys, "frozen", False):
|
||||
exe_dir = os.path.dirname(sys.executable)
|
||||
if exe_dir not in sys.path:
|
||||
sys.path.insert(0, exe_dir)
|
||||
|
||||
|
||||
# 启动应用
|
||||
launch(app_run, "development")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
main()
|
||||
|
||||
@@ -5,29 +5,34 @@ import os
|
||||
from module_bank import PythonToSQLite
|
||||
from typing import Callable
|
||||
|
||||
|
||||
# 检查是否在 PyInstaller 子进程中
|
||||
def is_pyinstaller_child():
|
||||
return len(sys.argv) > 1 and ('_child.py' in sys.argv[0] or sys.argv[1].isdigit())
|
||||
return len(sys.argv) > 1 and ("_child.py" in sys.argv[0] or sys.argv[1].isdigit())
|
||||
|
||||
|
||||
def parse_args():
|
||||
"""安全的参数解析函数"""
|
||||
if is_pyinstaller_child():
|
||||
# PyInstaller 子进程,使用默认参数
|
||||
return argparse.Namespace(config="config.toml")
|
||||
|
||||
parser = argparse.ArgumentParser(description="chongming 应用启动脚本")
|
||||
|
||||
parser = argparse.ArgumentParser(
|
||||
description="{{ cookiecutter.project_name }} 应用启动脚本"
|
||||
)
|
||||
parser.add_argument("--config", default="config.toml", help="配置文件路径")
|
||||
return parser.parse_args()
|
||||
|
||||
|
||||
args = parse_args()
|
||||
|
||||
|
||||
def init_module_bank(config):
|
||||
MODULE_BANK_PATH_LIST = config["production"]["module_system"]["path"]
|
||||
for module_path in MODULE_BANK_PATH_LIST:
|
||||
if os.path.exists(module_path) == False:
|
||||
raise ValueError(f"模块路径不存在: {module_path}")
|
||||
|
||||
|
||||
packer_list = [PythonToSQLite(module_path) for module_path in MODULE_BANK_PATH_LIST]
|
||||
for packer in packer_list:
|
||||
modules = packer.list_modules()
|
||||
@@ -35,15 +40,16 @@ def init_module_bank(config):
|
||||
print(f"{module['module_name']} {'[包]' if module['is_package'] else ''}")
|
||||
packer.install_importer()
|
||||
|
||||
|
||||
def launch(run_fun: Callable[[dict, dict], None], run_mode: str):
|
||||
|
||||
config = ConfigLoader(args.config)
|
||||
|
||||
|
||||
# 获取对应运行模式的配置
|
||||
default_config = config.get("default", {})
|
||||
run_mode = default_config.get("env", "development")
|
||||
app_config = config.get(run_mode, {})
|
||||
if run_mode == "production":
|
||||
init_module_bank(config)
|
||||
|
||||
|
||||
run_fun(app_config, default_config)
|
||||
|
||||
Reference in New Issue
Block a user