31 أسطر
852 B
Python
31 أسطر
852 B
Python
import time
|
|
from fastapi import FastAPI, Request
|
|
from fastapi.responses import HTMLResponse
|
|
|
|
app = FastAPI()
|
|
|
|
# Global counter to track total requests
|
|
request_count = 0
|
|
|
|
@app.middleware("http")
|
|
async def count_requests(request: Request, call_next):
|
|
global request_count
|
|
request_count += 1
|
|
start_time = time.time()
|
|
|
|
response = await call_next(request)
|
|
|
|
process_time = (time.time() - start_time) * 1000
|
|
response.headers["X-Process-Time"] = f"{process_time:.2f}ms"
|
|
return response
|
|
|
|
# 3. Health Endpoint required by the task
|
|
@app.get("/health")
|
|
def health_check():
|
|
return {"status": "healthy", "requests": request_count}
|
|
|
|
# 5. Dashboard Endpoint serving the HTML file
|
|
@app.get("/", response_class=HTMLResponse)
|
|
def get_dashboard():
|
|
with open("dashboard.html", "r", encoding="utf-8") as f:
|
|
return f.read() |