|
|
from fastapi import FastAPI, File, UploadFile |
|
|
from fastapi.middleware.gzip import GZipMiddleware |
|
|
from fastapi.responses import FileResponse |
|
|
from fastapi.staticfiles import StaticFiles |
|
|
import os |
|
|
import shutil |
|
|
import psutil |
|
|
|
|
|
app = FastAPI() |
|
|
|
|
|
|
|
|
app.add_middleware(GZipMiddleware, minimum_size=1000) |
|
|
|
|
|
|
|
|
UPLOAD_DIR = "/app/uploads" |
|
|
os.makedirs(UPLOAD_DIR, exist_ok=True) |
|
|
|
|
|
|
|
|
app.mount("/static", StaticFiles(directory="static"), name="static") |
|
|
|
|
|
@app.post("/upload/") |
|
|
async def upload_file(file: UploadFile = File(...)): |
|
|
file_path = os.path.join(UPLOAD_DIR, file.filename) |
|
|
with open(file_path, "wb") as buffer: |
|
|
shutil.copyfileobj(file.file, buffer) |
|
|
return {"filename": file.filename} |
|
|
|
|
|
@app.get("/download/{filename}") |
|
|
async def download_file(filename: str): |
|
|
file_path = os.path.join(UPLOAD_DIR, filename) |
|
|
if os.path.exists(file_path): |
|
|
return FileResponse(file_path, media_type='application/octet-stream', filename=filename) |
|
|
return {"error": "File not found"} |
|
|
|
|
|
@app.get("/system/metrics") |
|
|
async def get_system_metrics(): |
|
|
cpu_load = psutil.cpu_percent(interval=1) |
|
|
memory = psutil.virtual_memory() |
|
|
disk_usage = psutil.disk_usage('/') |
|
|
|
|
|
metrics = { |
|
|
"cpu_load": cpu_load, |
|
|
"memory": { |
|
|
"total": memory.total, |
|
|
"available": memory.available, |
|
|
"used": memory.used, |
|
|
"percent": memory.percent |
|
|
}, |
|
|
"disk_usage": { |
|
|
"total": disk_usage.total, |
|
|
"used": disk_usage.used, |
|
|
"free": disk_usage.free, |
|
|
"percent": disk_usage.percent |
|
|
} |
|
|
} |
|
|
return metrics |
|
|
|
|
|
@app.get("/") |
|
|
async def read_index(): |
|
|
return FileResponse('static/index.html') |
|
|
|