from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware from fastapi.responses import HTMLResponse import shutil import uvicorn app = FastAPI(title="Disk Space Monitor", description="A simple API to monitor disk space") # Add CORS middleware app.add_middleware( CORSMiddleware, allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) def get_disk_space(): """Get disk space information""" total, used, free = shutil.disk_usage("/") return { "total_gb": round(total / (1024**3), 2), "used_gb": round(used / (1024**3), 2), "free_gb": round(free / (1024**3), 2), "used_percentage": round((used / total) * 100, 2) } @app.get("/") async def root(): """Root endpoint with HTML interface""" disk_info = get_disk_space() html_content = f""" Disk Space Monitor

🖥️ Disk Space Monitor

Total Space: {disk_info['total_gb']} GB
Used Space: {disk_info['used_gb']} GB
Free Space: {disk_info['free_gb']} GB
Usage: {disk_info['used_percentage']}%

API Endpoints: /api/disk-space | /docs

""" return HTMLResponse(content=html_content) @app.get("/api/disk-space") async def get_disk_space_api(): """API endpoint to get disk space information in JSON format""" return get_disk_space() @app.get("/health") async def health_check(): """Health check endpoint""" return {"status": "healthy", "service": "disk-space-monitor"} if __name__ == "__main__": uvicorn.run(app, host="0.0.0.0", port=8000)