Spaces:
Sleeping
Sleeping
Arghya Ghosh commited on
Rename app.py to main.py
Browse files
app.py
DELETED
|
@@ -1,96 +0,0 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import re
|
| 3 |
-
import shutil
|
| 4 |
-
import subprocess
|
| 5 |
-
import tempfile
|
| 6 |
-
import uuid
|
| 7 |
-
import time
|
| 8 |
-
import resource # Only works on Unix-like systems
|
| 9 |
-
from fastapi import FastAPI, Request
|
| 10 |
-
from fastapi.middleware.cors import CORSMiddleware
|
| 11 |
-
|
| 12 |
-
app = FastAPI()
|
| 13 |
-
|
| 14 |
-
# Allow CORS
|
| 15 |
-
app.add_middleware(
|
| 16 |
-
CORSMiddleware,
|
| 17 |
-
allow_origins=["*"],
|
| 18 |
-
allow_credentials=True,
|
| 19 |
-
allow_methods=["*"],
|
| 20 |
-
allow_headers=["*"],
|
| 21 |
-
)
|
| 22 |
-
|
| 23 |
-
def extract_class_name(code: str) -> str:
|
| 24 |
-
match = re.search(r'public\s+class\s+(\w+)', code)
|
| 25 |
-
return match.group(1) if match else "Main"
|
| 26 |
-
|
| 27 |
-
@app.post("/run")
|
| 28 |
-
async def run_java(request: Request):
|
| 29 |
-
data = await request.json()
|
| 30 |
-
code = data.get("code", "")
|
| 31 |
-
|
| 32 |
-
if not code.strip():
|
| 33 |
-
return {"status": "error", "message": "No code provided"}
|
| 34 |
-
|
| 35 |
-
run_id = str(uuid.uuid4())
|
| 36 |
-
workdir = os.path.join(tempfile.gettempdir(), f"runner_{run_id}")
|
| 37 |
-
os.makedirs(workdir, exist_ok=True)
|
| 38 |
-
|
| 39 |
-
try:
|
| 40 |
-
class_name = extract_class_name(code)
|
| 41 |
-
java_file = os.path.join(workdir, f"{class_name}.java")
|
| 42 |
-
|
| 43 |
-
with open(java_file, "w") as f:
|
| 44 |
-
f.write(code)
|
| 45 |
-
|
| 46 |
-
# --- Compile phase ---
|
| 47 |
-
start_compile = time.time()
|
| 48 |
-
compile_proc = subprocess.run(
|
| 49 |
-
["javac", java_file],
|
| 50 |
-
capture_output=True,
|
| 51 |
-
text=True,
|
| 52 |
-
cwd=workdir
|
| 53 |
-
)
|
| 54 |
-
end_compile = time.time()
|
| 55 |
-
compile_time = (end_compile - start_compile) * 1000 # ms
|
| 56 |
-
|
| 57 |
-
if compile_proc.returncode != 0:
|
| 58 |
-
return {
|
| 59 |
-
"status": "error",
|
| 60 |
-
"stage": "compile",
|
| 61 |
-
"compile_time_ms": round(compile_time, 2),
|
| 62 |
-
"exit_code": compile_proc.returncode,
|
| 63 |
-
"stdout": compile_proc.stdout,
|
| 64 |
-
"stderr": compile_proc.stderr,
|
| 65 |
-
}
|
| 66 |
-
|
| 67 |
-
# --- Run phase ---
|
| 68 |
-
start_run = time.time()
|
| 69 |
-
run_proc = subprocess.run(
|
| 70 |
-
["java", "-cp", workdir, class_name],
|
| 71 |
-
capture_output=True,
|
| 72 |
-
text=True,
|
| 73 |
-
cwd=workdir
|
| 74 |
-
)
|
| 75 |
-
end_run = time.time()
|
| 76 |
-
run_time = (end_run - start_run) * 1000 # ms
|
| 77 |
-
|
| 78 |
-
# Memory usage (only works on Linux/macOS, returns KB)
|
| 79 |
-
usage = resource.getrusage(resource.RUSAGE_CHILDREN)
|
| 80 |
-
memory_kb = usage.ru_maxrss
|
| 81 |
-
|
| 82 |
-
result = {
|
| 83 |
-
"status": "success" if run_proc.returncode == 0 else "error",
|
| 84 |
-
"stage": "run",
|
| 85 |
-
"compile_time_ms": round(compile_time, 2),
|
| 86 |
-
"execution_time_ms": round(run_time, 2),
|
| 87 |
-
"exit_code": run_proc.returncode,
|
| 88 |
-
"stdout": run_proc.stdout,
|
| 89 |
-
"stderr": run_proc.stderr,
|
| 90 |
-
"memory_kb": memory_kb
|
| 91 |
-
}
|
| 92 |
-
|
| 93 |
-
return result
|
| 94 |
-
|
| 95 |
-
finally:
|
| 96 |
-
shutil.rmtree(workdir, ignore_errors=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
main.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Request
|
| 2 |
+
from redis import Redis
|
| 3 |
+
from rq import Queue
|
| 4 |
+
from runner import run_java
|
| 5 |
+
|
| 6 |
+
app = FastAPI(title="Safe Java Runner")
|
| 7 |
+
|
| 8 |
+
# Connect to Redis
|
| 9 |
+
redis_conn = Redis(host="localhost", port=6379)
|
| 10 |
+
queue = Queue(connection=redis_conn)
|
| 11 |
+
|
| 12 |
+
@app.post("/run")
|
| 13 |
+
async def submit_code(request: Request):
|
| 14 |
+
data = await request.json()
|
| 15 |
+
code = data.get("code", "")
|
| 16 |
+
|
| 17 |
+
if not code.strip():
|
| 18 |
+
return {"status": "error", "message": "No code provided"}
|
| 19 |
+
|
| 20 |
+
job = queue.enqueue(run_java, code)
|
| 21 |
+
return {"status": "queued", "job_id": job.id}
|
| 22 |
+
|
| 23 |
+
@app.get("/result/{job_id}")
|
| 24 |
+
async def get_result(job_id: str):
|
| 25 |
+
from rq.job import Job
|
| 26 |
+
try:
|
| 27 |
+
job = Job.fetch(job_id, connection=redis_conn)
|
| 28 |
+
except Exception:
|
| 29 |
+
return {"status": "error", "message": "Job not found"}
|
| 30 |
+
|
| 31 |
+
if job.is_finished:
|
| 32 |
+
return {"status": "finished", "result": job.result}
|
| 33 |
+
elif job.is_failed:
|
| 34 |
+
return {"status": "failed", "error": str(job.exc_info)}
|
| 35 |
+
else:
|
| 36 |
+
return {"status": "running"}
|