File size: 1,961 Bytes
16da590 48b9358 677599b 48b9358 677599b 48b9358 dfa7514 8c8a485 48b9358 677599b dfa7514 48b9358 677599b 48b9358 dfa7514 677599b 48b9358 677599b 48b9358 dfa7514 677599b 48b9358 dfa7514 48b9358 dfa7514 48b9358 bee0e8f 48b9358 bee0e8f 48b9358 8c8a485 48b9358 8c8a485 48b9358 8c8a485 bee0e8f 48b9358 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | from fastapi import FastAPI, UploadFile, File, Request
from fastapi.responses import HTMLResponse, JSONResponse
from fastapi.staticfiles import StaticFiles
import os, shutil, json, subprocess
app = FastAPI()
# Folder upload (pakai /tmp biar aman & gak permission denied)
UPLOAD_DIR = "/tmp/uploads"
os.makedirs(UPLOAD_DIR, exist_ok=True)
# Serve file index.html
@app.get("/", response_class=HTMLResponse)
async def root():
with open("index.html", encoding="utf-8") as f:
return f.read()
# Upload file
@app.post("/upload")
async def upload(file: UploadFile = File(...)):
dest = os.path.join(UPLOAD_DIR, file.filename)
with open(dest, "wb") as f:
shutil.copyfileobj(file.file, f)
return {"filename": file.filename}
# List file yang sudah diupload
@app.get("/files")
async def files():
return os.listdir(UPLOAD_DIR)
# Jalankan file Python (buat panel console)
@app.post("/run")
async def run_code(request: Request):
data = await request.json()
filename = data.get("filename")
filepath = os.path.join(UPLOAD_DIR, filename)
if not os.path.exists(filepath):
return {"output": f"File '{filename}' not found."}
try:
result = subprocess.run(
["python3", filepath],
capture_output=True,
text=True,
timeout=10
)
output = result.stdout + result.stderr
return {"output": output}
except subprocess.TimeoutExpired:
return {"output": "Execution timed out."}
except Exception as e:
return {"output": f"Error: {str(e)}"}
# Simpan konfigurasi Setup Panel
@app.post("/setup")
async def setup_config(request: Request):
data = await request.json()
with open("/tmp/setup.json", "w") as f:
json.dump(data, f, indent=2)
return {"status": "ok", "received": data}
# Mount /uploads biar bisa diakses langsung kalau mau
app.mount("/uploads", StaticFiles(directory=UPLOAD_DIR), name="uploads") |