from fastapi import APIRouter, Depends, HTTPException from core_domain import CodeExecutionRequestContract, CodeExecutionResponseContract, ServiceHealth from app.application.services import CodeRunnerApplicationService from app.bootstrap.settings import CodeRunnerServiceSettings from app.infrastructure.runner import CodeRunnerError, PythonCodeRunner router = APIRouter() def get_code_runner_settings() -> CodeRunnerServiceSettings: return CodeRunnerServiceSettings() def get_code_runner_application_service( settings: CodeRunnerServiceSettings = Depends(get_code_runner_settings), ) -> CodeRunnerApplicationService: return CodeRunnerApplicationService( runner=PythonCodeRunner(settings=settings), settings=settings, ) @router.get("/health", response_model=ServiceHealth) def health_check( settings: CodeRunnerServiceSettings = Depends(get_code_runner_settings), ) -> ServiceHealth: return ServiceHealth(service="code-runner-service", status="ok", database=settings.python_bin) @router.post("/execute", response_model=CodeExecutionResponseContract) def execute_code( payload: CodeExecutionRequestContract, service: CodeRunnerApplicationService = Depends(get_code_runner_application_service), ) -> CodeExecutionResponseContract: try: return service.execute_code(payload) except CodeRunnerError as exc: raise HTTPException(status_code=422, detail=str(exc)) from exc