You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
43 lines
1.1 KiB
43 lines
1.1 KiB
from fastapi import FastAPI
|
|
from contextlib import asynccontextmanager
|
|
import uvicorn
|
|
from pathlib import Path
|
|
from downloader import router as downloader_router
|
|
|
|
# 检查并创建 downloads 目录
|
|
def ensure_downloads_dir():
|
|
downloads_dir = Path("downloads")
|
|
downloads_dir.mkdir(exist_ok=True)
|
|
print(f"确保 downloads 目录存在: {downloads_dir.absolute()}")
|
|
|
|
# lifespan 事件处理器
|
|
@asynccontextmanager
|
|
async def lifespan(app: FastAPI):
|
|
# 启动时执行
|
|
ensure_downloads_dir()
|
|
print("应用启动完成!")
|
|
yield
|
|
# 关闭时执行(可选)
|
|
print("应用正在关闭...")
|
|
|
|
app = FastAPI(
|
|
title="下载器API",
|
|
description="一个基于FastAPI的异步下载器服务",
|
|
version="1.0.0",
|
|
lifespan=lifespan
|
|
)
|
|
|
|
# 注册路由
|
|
app.include_router(downloader_router)
|
|
|
|
@app.get("/")
|
|
async def root():
|
|
return {"message": "下载器服务运行中", "status": "healthy"}
|
|
|
|
if __name__ == "__main__":
|
|
uvicorn.run(
|
|
"main:app",
|
|
host="0.0.0.0",
|
|
port=5100,
|
|
reload=True # 开发时自动重载
|
|
)
|
|
|