Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

handlers.py 1.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #
  2. # Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. import base64
  17. from core.logger import logger
  18. from fastapi import Request
  19. from models.enums import ResultStatus
  20. from models.schemas import CodeExecutionRequest, CodeExecutionResult
  21. from services.execution import execute_code
  22. from services.limiter import limiter
  23. from services.security import analyze_code_security
  24. from core.container import _CONTAINER_EXECUTION_SEMAPHORES
  25. async def healthz_handler():
  26. return {"status": "ok"}
  27. @limiter.limit("5/second")
  28. async def run_code_handler(req: CodeExecutionRequest, request: Request):
  29. logger.info("🟢 Received /run request")
  30. async with _CONTAINER_EXECUTION_SEMAPHORES[req.language]:
  31. code = base64.b64decode(req.code_b64).decode("utf-8")
  32. is_safe, issues = analyze_code_security(code, language=req.language)
  33. if not is_safe:
  34. issue_details = "\n".join([f"Line {lineno}: {issue}" for issue, lineno in issues])
  35. return CodeExecutionResult(status=ResultStatus.PROGRAM_RUNNER_ERROR, stdout="", stderr=issue_details, exit_code=-999, detail="Code is unsafe")
  36. try:
  37. return await execute_code(req)
  38. except Exception as e:
  39. return CodeExecutionResult(status=ResultStatus.PROGRAM_RUNNER_ERROR, stdout="", stderr=str(e), exit_code=-999, detail="unhandled_exception")