Update app.py
Browse files
app.py
CHANGED
|
@@ -1,43 +1,30 @@
|
|
| 1 |
from fastapi import FastAPI, UploadFile, File, Request
|
| 2 |
-
from fastapi.responses import HTMLResponse
|
| 3 |
from fastapi.staticfiles import StaticFiles
|
| 4 |
-
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
# Inisialisasi FastAPI
|
| 7 |
app = FastAPI()
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
|
| 17 |
-
|
| 18 |
os.makedirs(UPLOAD_DIR, exist_ok=True)
|
| 19 |
-
os.makedirs(STATIC_DIR, exist_ok=True)
|
| 20 |
|
| 21 |
-
#
|
| 22 |
-
app.mount("/uploads", StaticFiles(directory=UPLOAD_DIR), name="uploads")
|
| 23 |
-
app.mount("/static", StaticFiles(directory=STATIC_DIR), name="static")
|
| 24 |
-
|
| 25 |
-
# =============================
|
| 26 |
-
# 🏠 ROUTES
|
| 27 |
-
# =============================
|
| 28 |
-
|
| 29 |
-
# Load index.html sebagai UI awal
|
| 30 |
@app.get("/", response_class=HTMLResponse)
|
| 31 |
async def root():
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
return f.read()
|
| 35 |
-
return HTMLResponse("<h1>index.html not found</h1>", status_code=404)
|
| 36 |
-
|
| 37 |
-
# =============================
|
| 38 |
-
# 📁 FILE MANAGER ENDPOINTS
|
| 39 |
-
# =============================
|
| 40 |
|
|
|
|
| 41 |
@app.post("/upload")
|
| 42 |
async def upload(file: UploadFile = File(...)):
|
| 43 |
dest = os.path.join(UPLOAD_DIR, file.filename)
|
|
@@ -45,61 +32,40 @@ async def upload(file: UploadFile = File(...)):
|
|
| 45 |
shutil.copyfileobj(file.file, f)
|
| 46 |
return {"filename": file.filename}
|
| 47 |
|
|
|
|
| 48 |
@app.get("/files")
|
| 49 |
async def files():
|
| 50 |
return os.listdir(UPLOAD_DIR)
|
| 51 |
|
| 52 |
-
|
| 53 |
-
async def rename_file(request: Request):
|
| 54 |
-
data = await request.json()
|
| 55 |
-
src = os.path.join(UPLOAD_DIR, data["old"])
|
| 56 |
-
dst = os.path.join(UPLOAD_DIR, data["new"])
|
| 57 |
-
if os.path.exists(src):
|
| 58 |
-
os.rename(src, dst)
|
| 59 |
-
return {"status": "renamed"}
|
| 60 |
-
return {"status": "error", "detail": "file not found"}
|
| 61 |
-
|
| 62 |
-
@app.post("/delete")
|
| 63 |
-
async def delete_file(request: Request):
|
| 64 |
-
data = await request.json()
|
| 65 |
-
path = os.path.join(UPLOAD_DIR, data["filename"])
|
| 66 |
-
if os.path.exists(path):
|
| 67 |
-
if os.path.isfile(path):
|
| 68 |
-
os.remove(path)
|
| 69 |
-
elif os.path.isdir(path):
|
| 70 |
-
shutil.rmtree(path)
|
| 71 |
-
return {"status": "deleted"}
|
| 72 |
-
return {"status": "error", "detail": "file not found"}
|
| 73 |
-
|
| 74 |
-
@app.post("/create_file")
|
| 75 |
-
async def create_file(request: Request):
|
| 76 |
-
data = await request.json()
|
| 77 |
-
path = os.path.join(UPLOAD_DIR, data["filename"])
|
| 78 |
-
with open(path, "w") as f:
|
| 79 |
-
f.write("")
|
| 80 |
-
return {"status": "created"}
|
| 81 |
-
|
| 82 |
-
@app.post("/create_folder")
|
| 83 |
-
async def create_folder(request: Request):
|
| 84 |
-
data = await request.json()
|
| 85 |
-
path = os.path.join(UPLOAD_DIR, data["foldername"])
|
| 86 |
-
os.makedirs(path, exist_ok=True)
|
| 87 |
-
return {"status": "created"}
|
| 88 |
-
|
| 89 |
-
# =============================
|
| 90 |
-
# ⚙️ SETUP CONFIG ENDPOINT
|
| 91 |
-
# =============================
|
| 92 |
-
|
| 93 |
@app.post("/setup")
|
| 94 |
async def setup_config(request: Request):
|
| 95 |
data = await request.json()
|
| 96 |
-
with open(
|
| 97 |
json.dump(data, f, indent=2)
|
| 98 |
return {"status": "ok", "received": data}
|
| 99 |
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from fastapi import FastAPI, UploadFile, File, Request
|
| 2 |
+
from fastapi.responses import HTMLResponse, JSONResponse
|
| 3 |
from fastapi.staticfiles import StaticFiles
|
| 4 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 5 |
+
from pydantic import BaseModel
|
| 6 |
+
import shutil, os, json, io, sys
|
| 7 |
|
|
|
|
| 8 |
app = FastAPI()
|
| 9 |
|
| 10 |
+
# CORS biar console jalan di frontend lokal atau hosted
|
| 11 |
+
app.add_middleware(
|
| 12 |
+
CORSMiddleware,
|
| 13 |
+
allow_origins=["*"],
|
| 14 |
+
allow_methods=["*"],
|
| 15 |
+
allow_headers=["*"],
|
| 16 |
+
)
|
| 17 |
|
| 18 |
+
UPLOAD_DIR = "uploads"
|
| 19 |
os.makedirs(UPLOAD_DIR, exist_ok=True)
|
|
|
|
| 20 |
|
| 21 |
+
# Serve halaman HTML utama
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
@app.get("/", response_class=HTMLResponse)
|
| 23 |
async def root():
|
| 24 |
+
with open("index.html", encoding="utf-8") as f:
|
| 25 |
+
return f.read()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
+
# Upload file ke folder uploads/
|
| 28 |
@app.post("/upload")
|
| 29 |
async def upload(file: UploadFile = File(...)):
|
| 30 |
dest = os.path.join(UPLOAD_DIR, file.filename)
|
|
|
|
| 32 |
shutil.copyfileobj(file.file, f)
|
| 33 |
return {"filename": file.filename}
|
| 34 |
|
| 35 |
+
# List semua file yang sudah di-upload
|
| 36 |
@app.get("/files")
|
| 37 |
async def files():
|
| 38 |
return os.listdir(UPLOAD_DIR)
|
| 39 |
|
| 40 |
+
# Simpan konfigurasi dari halaman Setup
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
@app.post("/setup")
|
| 42 |
async def setup_config(request: Request):
|
| 43 |
data = await request.json()
|
| 44 |
+
with open("setup.json", "w") as f:
|
| 45 |
json.dump(data, f, indent=2)
|
| 46 |
return {"status": "ok", "received": data}
|
| 47 |
|
| 48 |
+
# Mount folder uploads ke /uploads biar bisa dibuka via browser
|
| 49 |
+
app.mount("/uploads", StaticFiles(directory=UPLOAD_DIR), name="uploads")
|
| 50 |
+
|
| 51 |
+
# ================= CONSOLE ===================
|
| 52 |
+
|
| 53 |
+
class InputData(BaseModel):
|
| 54 |
+
input: str
|
| 55 |
+
|
| 56 |
+
# Tempat simpan environment REPL
|
| 57 |
+
exec_env = {}
|
| 58 |
+
|
| 59 |
+
@app.post("/console/input")
|
| 60 |
+
def run_console(data: InputData):
|
| 61 |
+
code = data.input
|
| 62 |
+
buffer = io.StringIO()
|
| 63 |
+
sys.stdout = buffer
|
| 64 |
+
try:
|
| 65 |
+
exec(code, exec_env)
|
| 66 |
+
output = buffer.getvalue()
|
| 67 |
+
except Exception as e:
|
| 68 |
+
output = str(e)
|
| 69 |
+
finally:
|
| 70 |
+
sys.stdout = sys.__stdout__
|
| 71 |
+
return {"output": output}
|