File size: 2,320 Bytes
5f7afef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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()),
    }