from __future__ import annotations import os from pathlib import Path # Automatically load .env file from project root without external dependencies _env_file = Path(__file__).resolve().parents[2] / ".env" if _env_file.exists(): for _line in _env_file.read_text(encoding="utf-8").splitlines(): _line = _line.strip() if _line and not _line.startswith("#") and "=" in _line: _k, _v = _line.split("=", 1) _k = _k.strip() _v = _v.strip().strip("'\"") if _k and _k not in os.environ: os.environ[_k] = _v from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware from fastapi.middleware.gzip import GZipMiddleware from codestar.problems.loader import repo from codestar.server.routes.judge import router as judge_router from codestar.server.routes.problems import router as problems_router from codestar.server.routes.submissions import router as submissions_router from codestar.server.routes.auth import router as auth_router from codestar.server.routes.payment import router as payment_router app = FastAPI( title="CodeStar Judge API", description="ML/DL/LLM 手撕代码判题平台 API", version="0.1.0", ) # Enable CORS for Next.js frontend with regex matching to comply with allow_credentials=True spec app.add_middleware( CORSMiddleware, allow_origin_regex=r"https?://.*", allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) # Enable automatic GZip response compression for responses >= 1KB (compressing 80KB problem list to ~12KB) app.add_middleware(GZipMiddleware, minimum_size=1000) app.include_router(auth_router) app.include_router(judge_router) app.include_router(problems_router) app.include_router(submissions_router) app.include_router(payment_router) @app.get("/api/health", tags=["System"]) def health_check(): return { "status": "ok", "service": "codestar-judge-api", "version": "0.1.0", "loaded_problems_count": len(repo.list()), } @app.get("/", tags=["System"]) def root_info(): return { "status": "ok", "service": "codestar-judge-api", "version": "0.1.0", "docs": "/docs", "health": "/api/health", "loaded_problems_count": len(repo.list()), }