| 123456789101112131415161718192021222324252627282930313233343536 |
- from core_domain import CodeExecutionRequestContract, CodeExecutionResponseContract, ServiceHealth
- from core_shared import error_detail
- from fastapi import APIRouter, Depends, HTTPException
- 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=error_detail("error.code_runner.execution_failed", message=str(exc))) from exc
|