|
5 | 5 | import asyncio |
6 | 6 |
|
7 | 7 | from fastapi import FastAPI |
| 8 | + |
8 | 9 | from fastapi.middleware.cors import CORSMiddleware |
9 | 10 | from fastapi.responses import HTMLResponse |
10 | 11 | from fastapi.staticfiles import StaticFiles |
|
18 | 19 | from core.settings import data_root, settings, BASE_DIR, DEFAULT_CONFIG |
19 | 20 | from core.tasks import delete_expire_files |
20 | 21 |
|
21 | | -app = FastAPI() |
| 22 | +from contextlib import asynccontextmanager |
| 23 | +from tortoise import Tortoise |
| 24 | + |
| 25 | + |
| 26 | +async def init_db(): |
| 27 | + await Tortoise.init( |
| 28 | + db_url=f'sqlite://{data_root}/filecodebox.db', |
| 29 | + modules={'models': ['apps.base.models']}, |
| 30 | + use_tz=False, |
| 31 | + timezone="Asia/Shanghai" |
| 32 | + ) |
| 33 | + await Tortoise.generate_schemas() |
| 34 | + |
| 35 | + |
| 36 | +@asynccontextmanager |
| 37 | +async def lifespan(app: FastAPI): |
| 38 | + # 初始化数据库 |
| 39 | + await init_db() |
| 40 | + |
| 41 | + # 启动后台任务,不定时删除过期文件 |
| 42 | + task = asyncio.create_task(delete_expire_files()) |
| 43 | + # 读取用户配置 |
| 44 | + user_config, created = await KeyValue.get_or_create(key='settings', defaults={'value': DEFAULT_CONFIG}) |
| 45 | + settings.user_config = user_config.value |
| 46 | + ip_limit['error'].minutes = settings.errorMinute |
| 47 | + ip_limit['error'].count = settings.errorCount |
| 48 | + ip_limit['upload'].minutes = settings.uploadMinute |
| 49 | + ip_limit['upload'].count = settings.uploadCount |
| 50 | + |
| 51 | + yield |
| 52 | + |
| 53 | + # 清理操作 |
| 54 | + task.cancel() |
| 55 | + try: |
| 56 | + await task |
| 57 | + except asyncio.CancelledError: |
| 58 | + pass |
| 59 | + |
| 60 | + # 关闭数据库连接 |
| 61 | + await Tortoise.close_connections() |
| 62 | + |
| 63 | + |
| 64 | +app = FastAPI(lifespan=lifespan) |
22 | 65 |
|
23 | 66 | app.add_middleware( |
24 | 67 | CORSMiddleware, |
|
30 | 73 |
|
31 | 74 | app.mount('/assets', StaticFiles(directory='./fcb-fronted/dist/assets'), name="assets") |
32 | 75 |
|
| 76 | +# 使用 register_tortoise 来添加异常处理器 |
33 | 77 | register_tortoise( |
34 | 78 | app, |
35 | | - generate_schemas=True, |
36 | | - add_exception_handlers=True, |
37 | 79 | config={ |
38 | | - 'connections': { |
39 | | - 'default': f'sqlite://{data_root}/filecodebox.db' |
40 | | - }, |
| 80 | + 'connections': {'default': f'sqlite://{data_root}/filecodebox.db'}, |
41 | 81 | 'apps': { |
42 | 82 | 'models': { |
43 | | - "models": ["apps.base.models"], |
| 83 | + 'models': ['apps.base.models'], |
44 | 84 | 'default_connection': 'default', |
45 | | - } |
| 85 | + }, |
46 | 86 | }, |
47 | | - "use_tz": False, |
48 | | - "timezone": "Asia/Shanghai", |
49 | | - } |
| 87 | + }, |
| 88 | + generate_schemas=False, # 我们已经在 init_db 中生成了 schema |
| 89 | + add_exception_handlers=True, |
50 | 90 | ) |
51 | 91 |
|
52 | 92 | app.include_router(share_api) |
53 | 93 | app.include_router(admin_api) |
54 | 94 |
|
55 | 95 |
|
56 | | -@app.on_event("startup") |
57 | | -async def startup_event(): |
58 | | - # 启动后台任务,不定时删除过期文件 |
59 | | - asyncio.create_task(delete_expire_files()) |
60 | | - # 读取用户配置 |
61 | | - user_config, created = await KeyValue.get_or_create(key='settings', defaults={'value': DEFAULT_CONFIG}) |
62 | | - settings.user_config = user_config.value |
63 | | - ip_limit['error'].minutes = settings.errorMinute |
64 | | - ip_limit['error'].count = settings.errorCount |
65 | | - ip_limit['upload'].minutes = settings.uploadMinute |
66 | | - ip_limit['upload'].count = settings.uploadCount |
67 | | - |
68 | | - |
69 | 96 | @app.get('/') |
70 | 97 | async def index(): |
71 | 98 | return HTMLResponse( |
|
0 commit comments