routes.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. from fastapi import APIRouter, Depends, HTTPException
  2. from core_domain import CodeExecutionRequestContract, CodeExecutionResponseContract, ServiceHealth
  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),
  11. ) -> CodeRunnerApplicationService:
  12. return CodeRunnerApplicationService(
  13. runner=PythonCodeRunner(settings=settings),
  14. settings=settings,
  15. )
  16. @router.get("/health", response_model=ServiceHealth)
  17. def health_check(
  18. settings: CodeRunnerServiceSettings = Depends(get_code_runner_settings),
  19. ) -> ServiceHealth:
  20. return ServiceHealth(service="code-runner-service", status="ok", database=settings.python_bin)
  21. @router.post("/execute", response_model=CodeExecutionResponseContract)
  22. def execute_code(
  23. payload: CodeExecutionRequestContract,
  24. service: CodeRunnerApplicationService = Depends(get_code_runner_application_service),
  25. ) -> CodeExecutionResponseContract:
  26. try:
  27. return service.execute_code(payload)
  28. except CodeRunnerError as exc:
  29. raise HTTPException(status_code=422, detail=str(exc)) from exc