82 lines
2.4 KiB
Python
82 lines
2.4 KiB
Python
from pathlib import Path
|
|
|
|
from fastapi import FastAPI, Request
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
from fastapi.responses import JSONResponse
|
|
from fastapi.staticfiles import StaticFiles
|
|
from starlette.middleware.base import BaseHTTPMiddleware
|
|
import traceback
|
|
|
|
from app.config import settings
|
|
from app.routers import auth, contests, problems, submissions, leaderboard, languages
|
|
|
|
# Ensure uploads directory exists
|
|
UPLOAD_DIR = Path("uploads")
|
|
UPLOAD_DIR.mkdir(parents=True, exist_ok=True)
|
|
(UPLOAD_DIR / "avatars").mkdir(parents=True, exist_ok=True)
|
|
|
|
|
|
class CatchAllMiddleware(BaseHTTPMiddleware):
|
|
async def dispatch(self, request: Request, call_next):
|
|
try:
|
|
return await call_next(request)
|
|
except Exception as exc:
|
|
traceback.print_exc()
|
|
origin = request.headers.get("origin", "http://localhost:3000")
|
|
return JSONResponse(
|
|
status_code=500,
|
|
content={"detail": str(exc)},
|
|
headers={
|
|
"Access-Control-Allow-Origin": origin,
|
|
"Access-Control-Allow-Credentials": "true",
|
|
},
|
|
)
|
|
|
|
|
|
app = FastAPI(
|
|
title="Sport Programming Platform",
|
|
description="Platform for competitive programming contests",
|
|
version="1.0.0",
|
|
)
|
|
|
|
# Add error catching middleware first
|
|
app.add_middleware(CatchAllMiddleware)
|
|
|
|
# CORS middleware
|
|
origins = [
|
|
"http://localhost:3000",
|
|
"http://127.0.0.1:3000",
|
|
"http://localhost:8000",
|
|
]
|
|
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=origins,
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
expose_headers=["*"],
|
|
)
|
|
|
|
# Include routers
|
|
app.include_router(auth.router, prefix="/api/auth", tags=["auth"])
|
|
app.include_router(contests.router, prefix="/api/contests", tags=["contests"])
|
|
app.include_router(problems.router, prefix="/api/problems", tags=["problems"])
|
|
app.include_router(submissions.router, prefix="/api/submissions", tags=["submissions"])
|
|
app.include_router(leaderboard.router, prefix="/api/leaderboard", tags=["leaderboard"])
|
|
app.include_router(languages.router, prefix="/api/languages", tags=["languages"])
|
|
|
|
|
|
# Mount static files for uploads
|
|
app.mount("/uploads", StaticFiles(directory="uploads"), name="uploads")
|
|
|
|
|
|
@app.get("/")
|
|
async def root():
|
|
return {"message": "Sport Programming Platform API", "version": "1.0.0"}
|
|
|
|
|
|
@app.get("/health")
|
|
async def health():
|
|
return {"status": "healthy"}
|