modify 更换orm框架
This commit is contained in:
@@ -1,40 +1,31 @@
|
||||
[default]
|
||||
app.name = "{{ cookiecutter.project_name }}"
|
||||
app.version = "{{ cookiecutter.project_name }}"
|
||||
app.description = "基于 FastAPI 和 Tortoise-ORM 的 {{ cookiecutter.project_name }} 打包脚手架"
|
||||
app.version = "0.1.0"
|
||||
app.description = "基于 FastAPI 的 chongming 打包脚手架"
|
||||
app.debug = false
|
||||
env = "production"
|
||||
|
||||
[database]
|
||||
type = "sqlite"
|
||||
url = "sqlite+aiosqlite:///./database.db"
|
||||
generate_schemas = true # 生产环境使用迁移工具
|
||||
|
||||
[database.sqlite]
|
||||
engine = "tortoise.backends.sqlite"
|
||||
file_path = "db.sqlite3"
|
||||
journal_mode = "WAL"
|
||||
busy_timeout = 10000
|
||||
maxsize = 10
|
||||
minsize = 1
|
||||
cache_size = -2000
|
||||
synchronous = "NORMAL"
|
||||
temp_store = "MEMORY"
|
||||
mmap_size = 1073741824
|
||||
foreign_keys = true
|
||||
auto_vacuum = "INCREMENTAL"
|
||||
max_cached_statements = 100
|
||||
readonly = false
|
||||
echo = true
|
||||
pool_size = 20
|
||||
max_overflow = 10
|
||||
pool_timeout = 30
|
||||
pool_recycle = 300
|
||||
pool_pre_ping = true
|
||||
connect_args.timeout = 15
|
||||
connect_args.check_same_thread = false
|
||||
|
||||
[security]
|
||||
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
|
||||
jwt_cache_db_path = "jwt_cache"
|
||||
max_sessions_per_user = 5
|
||||
|
||||
[development]
|
||||
@@ -43,13 +34,13 @@ debug = true
|
||||
|
||||
[development.server]
|
||||
host = "127.0.0.1"
|
||||
port = 8000
|
||||
port = 8001
|
||||
reload = true
|
||||
# workers = 4
|
||||
|
||||
[development.logging]
|
||||
level = "DEBUG"
|
||||
file = "logs/dev.log"
|
||||
format = "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
|
||||
|
||||
[development.cors]
|
||||
allow_origins = ["*"]
|
||||
@@ -65,14 +56,15 @@ debug = false
|
||||
host = "0.0.0.0"
|
||||
port = 8000
|
||||
reload = false
|
||||
workers = 1
|
||||
workers = 4
|
||||
|
||||
[production.logging]
|
||||
level = "INFO"
|
||||
file = "logs/prod.log"
|
||||
format = "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
|
||||
|
||||
[production.cors]
|
||||
allow_origins = ["https://yourdomain.com", "http://localhos:8000", "http://127.0.0.1:8000"]
|
||||
allow_origins = ["https://yourdomain.com", "http://localhost:8000", "http://127.0.0.1:8000"]
|
||||
allow_credentials = true
|
||||
allow_methods = ["GET", "POST", "PUT", "DELETE"]
|
||||
allow_headers = ["Authorization", "Content-Type"]
|
||||
@@ -84,6 +76,3 @@ path = ["resources/app.mbank", "resources/plugins.mbank"]
|
||||
[production.file_system]
|
||||
type = "svfs"
|
||||
path = "resources/application.vfs"
|
||||
|
||||
[cache]
|
||||
default_ttl = 300
|
||||
|
||||
@@ -5,9 +5,9 @@ FastAPI + Tortoise-ORM + {{ cookiecutter.project_name }} 示例
|
||||
主入口文件,适配 {{ cookiecutter.project_name }} 打包系统
|
||||
"""
|
||||
import sys
|
||||
import os
|
||||
from utils.launch import launch
|
||||
from multiprocessing import freeze_support
|
||||
import server
|
||||
|
||||
|
||||
def app_run(app_config: dict, default_config: dict):
|
||||
@@ -19,7 +19,7 @@ def app_run(app_config: dict, default_config: dict):
|
||||
default_config: 默认配置
|
||||
"""
|
||||
import uvicorn
|
||||
from app.logger import setup_logging, get_logger # type: ignore
|
||||
from app.core.logger import setup_logging, get_logger # type: ignore
|
||||
|
||||
setup_logging(app_config["logging"])
|
||||
logger = get_logger("app")
|
||||
@@ -47,10 +47,8 @@ def app_run(app_config: dict, default_config: dict):
|
||||
logger.info(f"👥 Workers: {workers}")
|
||||
logger.info("=" * 50)
|
||||
|
||||
# 关键修改:根据 reload 模式选择不同的启动方式
|
||||
|
||||
uvicorn.run(
|
||||
"app:app",
|
||||
"server:app",
|
||||
host=host,
|
||||
port=port,
|
||||
reload=reload,
|
||||
@@ -62,14 +60,7 @@ def app_run(app_config: dict, default_config: dict):
|
||||
|
||||
def main():
|
||||
freeze_support()
|
||||
# 如果是打包后的可执行文件,调整路径
|
||||
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")
|
||||
launch(app_run, "production")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
# server.py
|
||||
import sys
|
||||
from utils.launch import init_module_bank
|
||||
from utils.config import ConfigLoader
|
||||
|
||||
config = ConfigLoader("config.toml")
|
||||
init_module_bank(config)
|
||||
|
||||
# 从数据库导入真正的 app 模块
|
||||
try:
|
||||
import app # type: ignore # 此时会从 SQLite 数据库加载
|
||||
|
||||
app_instance = app.app # 获取 FastAPI 实例
|
||||
except ImportError as e:
|
||||
print(f"无法从数据库导入 app 模块: {e}")
|
||||
sys.exit(1)
|
||||
|
||||
# 暴露 ASGI 应用变量,供 uvicorn 使用
|
||||
app = app_instance
|
||||
Reference in New Issue
Block a user