Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
|
@@ -30,10 +30,91 @@ class WriteFileRequest(BaseModel):
|
|
| 30 |
path: str
|
| 31 |
content: str
|
| 32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
@app.get("/")
|
| 34 |
def read_root():
|
| 35 |
return {"status": "online", "workspace": WORKSPACE, "auth_enabled": True}
|
| 36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
@app.post("/execute", dependencies=[Depends(verify_token)])
|
| 38 |
def execute(req: ExecuteRequest):
|
| 39 |
try:
|
|
|
|
| 30 |
path: str
|
| 31 |
content: str
|
| 32 |
|
| 33 |
+
class RunCodeRequest(BaseModel):
|
| 34 |
+
language: str = "python" # python, bash, node, etc.
|
| 35 |
+
code: str
|
| 36 |
+
timeout: int = 30
|
| 37 |
+
|
| 38 |
@app.get("/")
|
| 39 |
def read_root():
|
| 40 |
return {"status": "online", "workspace": WORKSPACE, "auth_enabled": True}
|
| 41 |
|
| 42 |
+
# 语言到执行命令的映射
|
| 43 |
+
LANGUAGE_RUNNERS = {
|
| 44 |
+
"python": "python3",
|
| 45 |
+
"python3": "python3",
|
| 46 |
+
"py": "python3",
|
| 47 |
+
"bash": "bash",
|
| 48 |
+
"sh": "sh",
|
| 49 |
+
"node": "node",
|
| 50 |
+
"javascript": "node",
|
| 51 |
+
"js": "node",
|
| 52 |
+
}
|
| 53 |
+
|
| 54 |
+
# 语言到文件扩展名的映射
|
| 55 |
+
LANGUAGE_EXTENSIONS = {
|
| 56 |
+
"python": ".py",
|
| 57 |
+
"python3": ".py",
|
| 58 |
+
"py": ".py",
|
| 59 |
+
"bash": ".sh",
|
| 60 |
+
"sh": ".sh",
|
| 61 |
+
"node": ".js",
|
| 62 |
+
"javascript": ".js",
|
| 63 |
+
"js": ".js",
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
@app.post("/run_code", dependencies=[Depends(verify_token)])
|
| 67 |
+
def run_code(req: RunCodeRequest):
|
| 68 |
+
"""一站式代码执行:自动写入临时文件并执行"""
|
| 69 |
+
lang = req.language.lower()
|
| 70 |
+
|
| 71 |
+
if lang not in LANGUAGE_RUNNERS:
|
| 72 |
+
raise HTTPException(
|
| 73 |
+
status_code=400,
|
| 74 |
+
detail=f"Unsupported language: {req.language}. Supported: {list(LANGUAGE_RUNNERS.keys())}"
|
| 75 |
+
)
|
| 76 |
+
|
| 77 |
+
runner = LANGUAGE_RUNNERS[lang]
|
| 78 |
+
ext = LANGUAGE_EXTENSIONS[lang]
|
| 79 |
+
|
| 80 |
+
# 创建临时文件
|
| 81 |
+
import uuid
|
| 82 |
+
temp_filename = f"_run_{uuid.uuid4().hex[:8]}{ext}"
|
| 83 |
+
temp_path = os.path.join(WORKSPACE, temp_filename)
|
| 84 |
+
|
| 85 |
+
try:
|
| 86 |
+
# 写入代码
|
| 87 |
+
with open(temp_path, "w", encoding="utf-8") as f:
|
| 88 |
+
f.write(req.code)
|
| 89 |
+
|
| 90 |
+
# 执行代码
|
| 91 |
+
result = subprocess.run(
|
| 92 |
+
f"{runner} {temp_filename}",
|
| 93 |
+
shell=True,
|
| 94 |
+
cwd=WORKSPACE,
|
| 95 |
+
capture_output=True,
|
| 96 |
+
text=True,
|
| 97 |
+
timeout=req.timeout,
|
| 98 |
+
encoding='utf-8',
|
| 99 |
+
errors='replace'
|
| 100 |
+
)
|
| 101 |
+
|
| 102 |
+
return {
|
| 103 |
+
"stdout": result.stdout,
|
| 104 |
+
"stderr": result.stderr,
|
| 105 |
+
"exit_code": result.returncode,
|
| 106 |
+
"temp_file": temp_filename
|
| 107 |
+
}
|
| 108 |
+
except subprocess.TimeoutExpired:
|
| 109 |
+
raise HTTPException(status_code=408, detail="Code execution timed out")
|
| 110 |
+
except Exception as e:
|
| 111 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 112 |
+
finally:
|
| 113 |
+
# 清理临时文件(可选,保留以便调试)
|
| 114 |
+
# if os.path.exists(temp_path):
|
| 115 |
+
# os.remove(temp_path)
|
| 116 |
+
pass
|
| 117 |
+
|
| 118 |
@app.post("/execute", dependencies=[Depends(verify_token)])
|
| 119 |
def execute(req: ExecuteRequest):
|
| 120 |
try:
|