routes.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435
  1. from core_domain import CodeExecutionRequestContract, CodeExecutionResponseContract, ServiceHealth
  2. from fastapi import APIRouter, Depends, HTTPException
  3. from app.application.services import CodeRunnerApplicationService
  4. from app.bootstrap.settings import CodeRunnerServiceSettings
  5. from app.infrastructure.runner import CodeRunnerError, PythonCodeRunner
  6. router = APIRouter()
  7. def get_code_runner_settings() -> CodeRunnerServiceSettings:
  8. return CodeRunnerServiceSettings()
  9. def get_code_runner_application_service(
  10. settings: CodeRunnerServiceSettings = Depends(get_code_runner_settings)) -> CodeRunnerApplicationService:
  11. return CodeRunnerApplicationService(
  12. runner=PythonCodeRunner(settings=settings),
  13. settings=settings)
  14. @router.get("/health", response_model=ServiceHealth)
  15. def health_check(
  16. settings: CodeRunnerServiceSettings = Depends(get_code_runner_settings)) -> ServiceHealth:
  17. return ServiceHealth(service="code-runner-service", status="ok", database=settings.python_bin)
  18. @router.post("/execute", response_model=CodeExecutionResponseContract)
  19. def execute_code(
  20. payload: CodeExecutionRequestContract,
  21. service: CodeRunnerApplicationService = Depends(get_code_runner_application_service)) -> CodeExecutionResponseContract:
  22. try:
  23. return service.execute_code(payload)
  24. except CodeRunnerError as exc:
  25. raise HTTPException(status_code=422, detail=str(exc)) from exc