Files
FileCodeBox/main.py
T

103 lines
3.3 KiB
Python
Raw 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-11-28 10:27:34 +08:00
import re
2023-08-15 17:23:30 +08:00
2023-08-14 05:03:07 +08:00
from fastapi import FastAPI
2023-08-11 19:26:49 +08:00
from starlette.middleware.cors import CORSMiddleware
2023-11-28 10:27:34 +08:00
from starlette.responses import HTMLResponse, FileResponse
2023-08-14 01:56:46 +08:00
from tortoise.contrib.fastapi import register_tortoise
2023-08-15 17:23:30 +08:00
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-01-18 19:00:30 +08:00
from core.settings import data_root, settings, BASE_DIR
2023-08-15 22:37:54 +08:00
from core.tasks import delete_expire_files
2023-11-28 10:27:34 +08:00
from core.utils import max_save_times_desc
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-11-30 23:10:42 +08:00
2023-11-28 10:27:34 +08:00
@app.get('/assets/{file_path:path}')
async def assets(file_path: str):
if settings.max_save_seconds > 0:
if re.match(r'SendView-[\d|a-f|A-F]+\.js', file_path):
2024-05-06 13:02:00 +08:00
with open(BASE_DIR / f'fcb-fronted/dist/assets/{file_path}', 'r', encoding='utf-8') as f:
2023-11-28 10:27:34 +08:00
# 删除永久保存选项
content = f.read()
content = content.replace('_(c,{label:e(r)("send.expireData.forever"),value:"forever"},null,8,["label"]),', '')
return HTMLResponse(content=content, media_type='text/javascript')
if re.match(r'index-[\d|a-f|A-F]+\.js', file_path):
2024-05-06 13:02:00 +08:00
with open(BASE_DIR / f'fcb-fronted/dist/assets/{file_path}', 'r', encoding='utf-8') as f:
2023-11-28 10:27:34 +08:00
# 更改本文描述
desc_zh, desc_en = await max_save_times_desc(settings.max_save_seconds)
content = f.read()
content = content.replace('天数<7', desc_zh)
content = content.replace('Days <7', desc_en)
return HTMLResponse(content=content, media_type='text/javascript')
2024-05-06 13:02:00 +08:00
return FileResponse(f'fcb-fronted/dist/assets/{file_path}')
2023-08-14 13:30:10 +08:00
2023-11-30 23:10:42 +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())
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,
})
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)