Spaces:
Running
Running
File size: 1,792 Bytes
7aee092 7ab93cb 7aee092 96f6f96 7aee092 d445ba5 ce74c54 7aee092 7ab93cb ce74c54 7aee092 96f6f96 ce74c54 7aee092 d445ba5 96f6f96 |
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 |
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()
# Enable gzip compression
app.add_middleware(GZipMiddleware, minimum_size=1000)
# Directory to store uploaded files
UPLOAD_DIR = "/app/uploads"
os.makedirs(UPLOAD_DIR, exist_ok=True)
# Mount static files directory
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')
|