Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

handlers.py 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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.container import _CONTAINER_EXECUTION_SEMAPHORES
  18. from core.logger import logger
  19. from fastapi import Request
  20. from models.enums import ResultStatus, SupportLanguage
  21. from models.schemas import CodeExecutionRequest, CodeExecutionResult
  22. from services.execution import execute_code
  23. from services.limiter import limiter
  24. from services.security import analyze_code_security
  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. if req.language == SupportLanguage.NODEJS:
  33. code += "\n\nmodule.exports = { main };"
  34. req.code_b64 = base64.b64encode(code.encode("utf-8")).decode("utf-8")
  35. is_safe, issues = analyze_code_security(code, language=req.language)
  36. if not is_safe:
  37. issue_details = "\n".join([f"Line {lineno}: {issue}" for issue, lineno in issues])
  38. return CodeExecutionResult(status=ResultStatus.PROGRAM_RUNNER_ERROR, stdout="", stderr=issue_details, exit_code=-999, detail="Code is unsafe")
  39. try:
  40. return await execute_code(req)
  41. except Exception as e:
  42. return CodeExecutionResult(status=ResultStatus.PROGRAM_RUNNER_ERROR, stdout="", stderr=str(e), exit_code=-999, detail="unhandled_exception")