57 行
1.9 KiB
Python
57 行
1.9 KiB
Python
"""FastAPI 入口:装配路由、静态、CORS、种子数据。"""
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
from contextlib import asynccontextmanager
|
|
from pathlib import Path
|
|
|
|
from fastapi import FastAPI
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
from fastapi.staticfiles import StaticFiles
|
|
|
|
from app.api.routes import admin, auth, chat, health, judge, latex, problems, progress, usage
|
|
from app.core.config import settings
|
|
from app.core.db import SessionLocal, init_db
|
|
|
|
logger = logging.getLogger("fluent.main")
|
|
|
|
# 种子题目:M1 内置 P1021(服务重启即存在,供前端直接使用)
|
|
def _seed():
|
|
from app.repositories.problem_repo import ProblemRepo
|
|
|
|
with SessionLocal() as session:
|
|
ProblemRepo(session).seed_defaults()
|
|
session.close()
|
|
|
|
|
|
@asynccontextmanager
|
|
async def lifespan(app: FastAPI):
|
|
init_db()
|
|
_seed()
|
|
logger.info("fluent-learn backend 启动: judge=%s ai=%s", settings.judge_engine, settings.ai_provider)
|
|
yield
|
|
|
|
|
|
app = FastAPI(title=settings.app_name, version="0.1.0", lifespan=lifespan)
|
|
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=["*"], # 原型/开发期放开;生产由同域部署收敛
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
app.include_router(health.router, prefix="/api")
|
|
app.include_router(auth.router, prefix="/api")
|
|
app.include_router(admin.router, prefix="/api")
|
|
app.include_router(problems.router, prefix="/api")
|
|
app.include_router(judge.router, prefix="/api")
|
|
app.include_router(chat.router, prefix="/api")
|
|
app.include_router(progress.router, prefix="/api")
|
|
app.include_router(usage.router, prefix="/api")
|
|
app.include_router(latex.router, prefix="/api")
|
|
|
|
# 前端静态(部署时 Alias 由 Apache 处理,此处仅本地联调用)
|
|
if settings.frontend_dir and Path(settings.frontend_dir).exists():
|
|
app.mount("/", StaticFiles(directory=settings.frontend_dir, html=True), name="frontend")
|