42 lines
1.2 KiB
Python
42 lines
1.2 KiB
Python
from fastapi import APIRouter, HTTPException
|
|
from pydantic import BaseModel
|
|
|
|
from app.services.judge import judge_service
|
|
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
class LanguageResponse(BaseModel):
|
|
id: int
|
|
name: str
|
|
|
|
|
|
# Popular languages to show at the top (synced with LANGUAGE_MAP in judge.py)
|
|
POPULAR_LANGUAGE_IDS = [71, 54, 50, 62, 63, 73, 60, 78, 51, 74] # Python, C++, C, Java, JS, Rust, Go, Kotlin, C#, TS
|
|
|
|
|
|
@router.get("/", response_model=list[LanguageResponse])
|
|
async def get_languages():
|
|
try:
|
|
languages = await judge_service.get_languages()
|
|
|
|
# Sort: popular first, then alphabetically
|
|
def sort_key(lang):
|
|
lang_id = lang.get("id", 0)
|
|
if lang_id in POPULAR_LANGUAGE_IDS:
|
|
return (0, POPULAR_LANGUAGE_IDS.index(lang_id))
|
|
return (1, lang.get("name", ""))
|
|
|
|
sorted_languages = sorted(languages, key=sort_key)
|
|
|
|
return [
|
|
LanguageResponse(id=lang["id"], name=lang["name"])
|
|
for lang in sorted_languages
|
|
]
|
|
except Exception as e:
|
|
raise HTTPException(
|
|
status_code=503,
|
|
detail=f"Judge service unavailable: {str(e)}"
|
|
)
|