2026-03-05 11:50:15 +08:00
|
|
|
from fastapi import FastAPI
|
|
|
|
|
from fastapi.middleware.cors import CORSMiddleware
|
2026-03-06 15:52:34 +08:00
|
|
|
import threading
|
2026-03-05 11:50:15 +08:00
|
|
|
|
2026-03-06 15:52:34 +08:00
|
|
|
from backend.database import Base, engine, SessionLocal
|
2026-03-05 11:50:15 +08:00
|
|
|
from backend.routers import auth, categories, exports, imports, practice, questions, stats
|
2026-03-06 15:52:34 +08:00
|
|
|
from backend.services.import_queue_service import reset_stale_running_jobs, run_worker_loop
|
2026-03-05 11:50:15 +08:00
|
|
|
|
|
|
|
|
app = FastAPI(title="Problem Bank API", version="1.0.0")
|
|
|
|
|
|
|
|
|
|
app.add_middleware(
|
|
|
|
|
CORSMiddleware,
|
|
|
|
|
allow_origins=["*"],
|
|
|
|
|
allow_credentials=True,
|
|
|
|
|
allow_methods=["*"],
|
|
|
|
|
allow_headers=["*"],
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
Base.metadata.create_all(bind=engine)
|
|
|
|
|
|
2026-03-06 15:52:34 +08:00
|
|
|
|
|
|
|
|
@app.on_event("startup")
|
|
|
|
|
def startup() -> None:
|
|
|
|
|
db = SessionLocal()
|
|
|
|
|
try:
|
|
|
|
|
reset_stale_running_jobs(db)
|
|
|
|
|
finally:
|
|
|
|
|
db.close()
|
|
|
|
|
thread = threading.Thread(target=run_worker_loop, daemon=True)
|
|
|
|
|
thread.start()
|
|
|
|
|
|
2026-03-05 11:50:15 +08:00
|
|
|
app.include_router(auth.router, prefix="/api/auth", tags=["auth"])
|
|
|
|
|
app.include_router(questions.router, prefix="/api/questions", tags=["questions"])
|
|
|
|
|
app.include_router(imports.router, prefix="/api/import", tags=["imports"])
|
|
|
|
|
app.include_router(exports.router, prefix="/api/export", tags=["exports"])
|
|
|
|
|
app.include_router(categories.router, prefix="/api/categories", tags=["categories"])
|
|
|
|
|
app.include_router(practice.router, prefix="/api/practice", tags=["practice"])
|
|
|
|
|
app.include_router(stats.router, prefix="/api/stats", tags=["stats"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.get("/api/health")
|
|
|
|
|
def health() -> dict:
|
|
|
|
|
return {"status": "ok"}
|