53 lines
1.4 KiB
Python
53 lines
1.4 KiB
Python
import os
|
|
from contextlib import asynccontextmanager
|
|
from pathlib import Path
|
|
|
|
from dotenv import load_dotenv
|
|
load_dotenv(override=True)
|
|
|
|
from fastapi import FastAPI
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
from fastapi.staticfiles import StaticFiles
|
|
|
|
from app.api.chat import router as chat_router
|
|
from app.api.auth import router as auth_router
|
|
from app.api.admin import router as admin_router
|
|
from app.db import create_db_and_tables, ensure_default_admin
|
|
|
|
|
|
@asynccontextmanager
|
|
async def lifespan(app: FastAPI):
|
|
create_db_and_tables()
|
|
ensure_default_admin()
|
|
yield
|
|
|
|
|
|
app = FastAPI(title="EPEEKit API", lifespan=lifespan)
|
|
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=["*"],
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
# 确保存储目录存在
|
|
UPLOADS_DIR = Path(__file__).parent.parent / "uploads"
|
|
GENERATED_DIR = Path(__file__).parent.parent / "generated"
|
|
UPLOADS_DIR.mkdir(exist_ok=True)
|
|
GENERATED_DIR.mkdir(exist_ok=True)
|
|
|
|
# 静态文件服务:提供上传的参考图和生成的图片
|
|
app.mount("/uploads", StaticFiles(directory=str(UPLOADS_DIR)), name="uploads")
|
|
app.mount("/generated", StaticFiles(directory=str(GENERATED_DIR)), name="generated")
|
|
|
|
app.include_router(auth_router, prefix="/api")
|
|
app.include_router(admin_router, prefix="/api")
|
|
app.include_router(chat_router, prefix="/api")
|
|
|
|
|
|
@app.get("/health")
|
|
async def health():
|
|
return {"status": "ok"}
|