File size: 4,073 Bytes
115ce0e
 
 
3f2638c
115ce0e
 
 
 
 
 
 
 
 
 
 
 
3f2638c
 
115ce0e
3f2638c
115ce0e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3f2638c
 
115ce0e
 
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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"""
    <!DOCTYPE html>
    <html>
    <head>
        <title>Disk Space Monitor</title>
        <style>
            body {{ font-family: Arial, sans-serif; margin: 40px; background-color: #f5f5f5; }}
            .container {{ max-width: 600px; margin: 0 auto; background: white; padding: 30px; border-radius: 10px; box-shadow: 0 2px 10px rgba(0,0,0,0.1); }}
            h1 {{ color: #333; text-align: center; }}
            .disk-info {{ background: #f8f9fa; padding: 20px; border-radius: 8px; margin: 20px 0; }}
            .metric {{ display: flex; justify-content: space-between; margin: 10px 0; padding: 10px; background: white; border-radius: 5px; }}
            .metric-label {{ font-weight: bold; color: #555; }}
            .metric-value {{ color: #007bff; font-weight: bold; }}
            .progress-bar {{ width: 100%; height: 20px; background: #e9ecef; border-radius: 10px; overflow: hidden; margin: 10px 0; }}
            .progress-fill {{ height: 100%; background: linear-gradient(90deg, #28a745, #ffc107, #dc3545); transition: width 0.3s ease; }}
            .refresh-btn {{ background: #007bff; color: white; border: none; padding: 10px 20px; border-radius: 5px; cursor: pointer; margin: 10px 0; }}
            .refresh-btn:hover {{ background: #0056b3; }}
        </style>
    </head>
    <body>
        <div class="container">
            <h1>🖥️ Disk Space Monitor</h1>
            <div class="disk-info">
                <div class="metric">
                    <span class="metric-label">Total Space:</span>
                    <span class="metric-value">{disk_info['total_gb']} GB</span>
                </div>
                <div class="metric">
                    <span class="metric-label">Used Space:</span>
                    <span class="metric-value">{disk_info['used_gb']} GB</span>
                </div>
                <div class="metric">
                    <span class="metric-label">Free Space:</span>
                    <span class="metric-value">{disk_info['free_gb']} GB</span>
                </div>
                <div class="metric">
                    <span class="metric-label">Usage:</span>
                    <span class="metric-value">{disk_info['used_percentage']}%</span>
                </div>
                <div class="progress-bar">
                    <div class="progress-fill" style="width: {disk_info['used_percentage']}%"></div>
                </div>
                <button class="refresh-btn" onclick="location.reload()">🔄 Refresh</button>
            </div>
            <p style="text-align: center; color: #666; margin-top: 30px;">
                API Endpoints: <a href="/api/disk-space">/api/disk-space</a> | <a href="/docs">/docs</a>
            </p>
        </div>
    </body>
    </html>
    """
    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)