-
-
Notifications
You must be signed in to change notification settings - Fork 574
/
Copy pathmain.py
73 lines (59 loc) · 1.88 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# @Time : 2023/8/9 23:23
# @Author : Lan
# @File : main.py
# @Software: PyCharm
import asyncio
import os
from fastapi import FastAPI
from starlette.middleware.cors import CORSMiddleware
from starlette.responses import HTMLResponse
from starlette.staticfiles import StaticFiles
from tortoise.contrib.fastapi import register_tortoise
from apps.base.views import share_api
from apps.admin.views import admin_api
from core.settings import data_root, settings
from core.tasks import delete_expire_files
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
app.mount('/assets', StaticFiles(directory='./fcb-fronted/dist/assets'), name="assets")
register_tortoise(
app,
generate_schemas=True,
add_exception_handlers=True,
config={
'connections': {
'default': f'sqlite://{data_root}/filecodebox.db'
},
'apps': {
'models': {
"models": ["apps.base.models"],
'default_connection': 'default',
}
},
"use_tz": False,
"timezone": "Asia/Shanghai",
}
)
app.include_router(share_api)
app.include_router(admin_api)
@app.on_event("startup")
async def startup_event():
# 启动后台任务,不定时删除过期文件
asyncio.create_task(delete_expire_files())
@app.get('/')
async def index():
return HTMLResponse(
content=open('./fcb-fronted/dist/index.html', 'r', encoding='utf-8').read()
.replace('{{title}}', settings.name)
.replace('{{description}}', settings.description)
.replace('{{keywords}}', settings.keywords)
, media_type='text/html', headers={'Cache-Control': 'no-cache'})
if __name__ == '__main__':
import uvicorn
uvicorn.run(app='main:app', host="0.0.0.0", port=settings.port, reload=False, workers=1)