-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfastApp.py
More file actions
32 lines (25 loc) · 809 Bytes
/
fastApp.py
File metadata and controls
32 lines (25 loc) · 809 Bytes
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
import time
from fastapi import FastAPI
from fastapi.responses import JSONResponse
app = FastAPI()
START = time.time()
health_status = True
def elapsed():
running = time.time() - START
minutes, seconds = divmod(running, 60)
hours, minutes = divmod(minutes, 60)
return f"{int(hours)}:{int(minutes):02d}:{int(seconds):02d}"
@app.get("/toggle")
async def toggle():
global health_status
health_status = not health_status
return JSONResponse(content={"health_value": health_status})
@app.get("/health")
async def health():
if health_status:
return JSONResponse(content={"health": "healthy"})
else:
return JSONResponse(content={"health": "unhealthy"}, status_code=500)
@app.get("/")
async def root():
return f"Hello World (Python)! (up {elapsed()})\n"