Update app.py
Browse files
app.py
CHANGED
|
@@ -1,40 +1,21 @@
|
|
| 1 |
from fastapi import FastAPI, UploadFile, File, Request
|
| 2 |
-
from fastapi.responses import HTMLResponse
|
| 3 |
from fastapi.staticfiles import StaticFiles
|
| 4 |
-
import
|
| 5 |
|
| 6 |
app = FastAPI()
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
|
| 10 |
-
# =============================
|
| 11 |
-
BASE_DIR = "/tmp" # WAJIB pake /tmp biar gak kena PermissionError
|
| 12 |
-
UPLOAD_DIR = os.path.join(BASE_DIR, "uploads")
|
| 13 |
-
STATIC_DIR = os.path.join(BASE_DIR, "static")
|
| 14 |
-
SETUP_FILE = os.path.join(BASE_DIR, "setup.json")
|
| 15 |
-
|
| 16 |
-
# Bikin folder kalau belum ada
|
| 17 |
os.makedirs(UPLOAD_DIR, exist_ok=True)
|
| 18 |
-
os.makedirs(STATIC_DIR, exist_ok=True)
|
| 19 |
-
|
| 20 |
-
# Mount file statis
|
| 21 |
-
app.mount("/uploads", StaticFiles(directory=UPLOAD_DIR), name="uploads")
|
| 22 |
-
app.mount("/static", StaticFiles(directory=STATIC_DIR), name="static")
|
| 23 |
|
| 24 |
-
#
|
| 25 |
-
# 🏠 MAIN PAGE
|
| 26 |
-
# =============================
|
| 27 |
@app.get("/", response_class=HTMLResponse)
|
| 28 |
async def root():
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
return f.read()
|
| 32 |
-
return HTMLResponse("<h1>index.html not found</h1>", status_code=404)
|
| 33 |
-
|
| 34 |
-
# =============================
|
| 35 |
-
# 📁 FILE MANAGER
|
| 36 |
-
# =============================
|
| 37 |
|
|
|
|
| 38 |
@app.post("/upload")
|
| 39 |
async def upload(file: UploadFile = File(...)):
|
| 40 |
dest = os.path.join(UPLOAD_DIR, file.filename)
|
|
@@ -42,60 +23,42 @@ async def upload(file: UploadFile = File(...)):
|
|
| 42 |
shutil.copyfileobj(file.file, f)
|
| 43 |
return {"filename": file.filename}
|
| 44 |
|
|
|
|
| 45 |
@app.get("/files")
|
| 46 |
-
async def
|
| 47 |
return os.listdir(UPLOAD_DIR)
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
|
|
|
| 51 |
data = await request.json()
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
if os.path.exists(src):
|
| 55 |
-
os.rename(src, dst)
|
| 56 |
-
return {"status": "renamed"}
|
| 57 |
-
return {"status": "error", "detail": "file not found"}
|
| 58 |
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
data = await request.json()
|
| 62 |
-
path = os.path.join(UPLOAD_DIR, data["filename"])
|
| 63 |
-
if os.path.exists(path):
|
| 64 |
-
if os.path.isfile(path):
|
| 65 |
-
os.remove(path)
|
| 66 |
-
elif os.path.isdir(path):
|
| 67 |
-
shutil.rmtree(path)
|
| 68 |
-
return {"status": "deleted"}
|
| 69 |
-
return {"status": "error", "detail": "file not found"}
|
| 70 |
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
return {"status": "created"}
|
| 85 |
|
| 86 |
-
#
|
| 87 |
-
# ⚙️ SETUP ENDPOINT
|
| 88 |
-
# =============================
|
| 89 |
@app.post("/setup")
|
| 90 |
-
async def
|
| 91 |
data = await request.json()
|
| 92 |
-
with open(
|
| 93 |
json.dump(data, f, indent=2)
|
| 94 |
return {"status": "ok", "received": data}
|
| 95 |
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
if os.path.exists(SETUP_FILE):
|
| 99 |
-
with open(SETUP_FILE, "r") as f:
|
| 100 |
-
return json.load(f)
|
| 101 |
-
return {"username": "", "password": "", "start_cmd": ""}
|
|
|
|
| 1 |
from fastapi import FastAPI, UploadFile, File, Request
|
| 2 |
+
from fastapi.responses import HTMLResponse, JSONResponse
|
| 3 |
from fastapi.staticfiles import StaticFiles
|
| 4 |
+
import os, shutil, json, subprocess
|
| 5 |
|
| 6 |
app = FastAPI()
|
| 7 |
|
| 8 |
+
# Folder upload (pakai /tmp biar aman & gak permission denied)
|
| 9 |
+
UPLOAD_DIR = "/tmp/uploads"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
os.makedirs(UPLOAD_DIR, exist_ok=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
# Serve file index.html
|
|
|
|
|
|
|
| 13 |
@app.get("/", response_class=HTMLResponse)
|
| 14 |
async def root():
|
| 15 |
+
with open("index.html", encoding="utf-8") as f:
|
| 16 |
+
return f.read()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
# Upload file
|
| 19 |
@app.post("/upload")
|
| 20 |
async def upload(file: UploadFile = File(...)):
|
| 21 |
dest = os.path.join(UPLOAD_DIR, file.filename)
|
|
|
|
| 23 |
shutil.copyfileobj(file.file, f)
|
| 24 |
return {"filename": file.filename}
|
| 25 |
|
| 26 |
+
# List file yang sudah diupload
|
| 27 |
@app.get("/files")
|
| 28 |
+
async def files():
|
| 29 |
return os.listdir(UPLOAD_DIR)
|
| 30 |
|
| 31 |
+
# Jalankan file Python (buat panel console)
|
| 32 |
+
@app.post("/run")
|
| 33 |
+
async def run_code(request: Request):
|
| 34 |
data = await request.json()
|
| 35 |
+
filename = data.get("filename")
|
| 36 |
+
filepath = os.path.join(UPLOAD_DIR, filename)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
+
if not os.path.exists(filepath):
|
| 39 |
+
return {"output": f"File '{filename}' not found."}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
+
try:
|
| 42 |
+
result = subprocess.run(
|
| 43 |
+
["python3", filepath],
|
| 44 |
+
capture_output=True,
|
| 45 |
+
text=True,
|
| 46 |
+
timeout=10
|
| 47 |
+
)
|
| 48 |
+
output = result.stdout + result.stderr
|
| 49 |
+
return {"output": output}
|
| 50 |
+
except subprocess.TimeoutExpired:
|
| 51 |
+
return {"output": "Execution timed out."}
|
| 52 |
+
except Exception as e:
|
| 53 |
+
return {"output": f"Error: {str(e)}"}
|
|
|
|
| 54 |
|
| 55 |
+
# Simpan konfigurasi Setup Panel
|
|
|
|
|
|
|
| 56 |
@app.post("/setup")
|
| 57 |
+
async def setup_config(request: Request):
|
| 58 |
data = await request.json()
|
| 59 |
+
with open("/tmp/setup.json", "w") as f:
|
| 60 |
json.dump(data, f, indent=2)
|
| 61 |
return {"status": "ok", "received": data}
|
| 62 |
|
| 63 |
+
# Mount /uploads biar bisa diakses langsung kalau mau
|
| 64 |
+
app.mount("/uploads", StaticFiles(directory=UPLOAD_DIR), name="uploads")
|
|
|
|
|
|
|
|
|
|
|
|