Files
FileCodeBox/main.py
T

88 lines
2.5 KiB
Python
Raw Permalink Normal View History

2023-08-11 19:26:49 +08:00
# @Time : 2023/8/9 23:23
# @Author : Lan
# @File : main.py
# @Software: PyCharm
2023-08-15 22:37:54 +08:00
import asyncio
2023-08-15 17:23:30 +08:00
2023-08-14 05:03:07 +08:00
from fastapi import FastAPI
2024-06-15 20:04:48 +08:00
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import HTMLResponse
2023-08-14 01:56:46 +08:00
from tortoise.contrib.fastapi import register_tortoise
2023-08-15 17:23:30 +08:00
2024-06-15 20:04:48 +08:00
from apps.base.models import KeyValue
2023-08-14 05:03:07 +08:00
from apps.base.views import share_api
2023-08-14 17:52:53 +08:00
from apps.admin.views import admin_api
from core.response import APIResponse
2024-06-15 20:04:48 +08:00
from core.settings import data_root, settings, BASE_DIR, DEFAULT_CONFIG
2023-08-15 22:37:54 +08:00
from core.tasks import delete_expire_files
2023-08-11 19:26:49 +08:00
app = FastAPI()
2023-08-15 17:23:30 +08:00
2023-08-11 19:26:49 +08:00
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
2023-08-15 17:23:30 +08:00
2023-08-14 01:56:46 +08:00
register_tortoise(
app,
generate_schemas=True,
add_exception_handlers=True,
config={
'connections': {
2023-08-15 10:05:50 +08:00
'default': f'sqlite://{data_root}/filecodebox.db'
2023-08-14 01:56:46 +08:00
},
'apps': {
'models': {
"models": ["apps.base.models"],
'default_connection': 'default',
}
},
"use_tz": False,
"timezone": "Asia/Shanghai",
}
)
2023-08-11 19:26:49 +08:00
2023-08-14 17:52:53 +08:00
app.include_router(share_api)
app.include_router(admin_api)
2023-08-14 13:30:10 +08:00
2023-08-15 22:37:54 +08:00
@app.on_event("startup")
async def startup_event():
# 启动后台任务,不定时删除过期文件
asyncio.create_task(delete_expire_files())
# 读取用户配置
2024-06-15 20:04:48 +08:00
user_config, created = await KeyValue.get_or_create(key='settings', defaults={'value': DEFAULT_CONFIG})
settings.user_config = user_config.value
2023-08-15 22:37:54 +08:00
2023-08-14 13:30:10 +08:00
@app.get('/')
async def index():
2023-08-15 17:23:30 +08:00
return HTMLResponse(
2024-05-06 13:02:00 +08:00
content=open(BASE_DIR / 'fcb-fronted/dist/index.html', 'r', encoding='utf-8').read()
2023-11-30 23:09:48 +08:00
.replace('{{title}}', str(settings.name))
.replace('{{description}}', str(settings.description))
.replace('{{keywords}}', str(settings.keywords))
.replace('{{opacity}}', str(settings.opacity))
.replace('{{background}}', str(settings.background))
2023-08-15 17:23:30 +08:00
, media_type='text/html', headers={'Cache-Control': 'no-cache'})
2023-08-14 18:27:39 +08:00
@app.post('/')
async def get_config():
return APIResponse(detail={
'explain': settings.page_explain,
'uploadSize': settings.uploadSize,
2024-06-15 20:04:48 +08:00
'expireStyle': settings.expireStyle,
2024-06-15 20:28:12 +08:00
'openUpload': settings.openUpload,
'notify_title': settings.notify_title,
'notify_content': settings.notify_content,
})
2023-08-14 18:27:39 +08:00
if __name__ == '__main__':
import uvicorn
uvicorn.run(app='main:app', host="0.0.0.0", port=settings.port, reload=False, workers=1)