-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
64 lines (53 loc) · 2.4 KB
/
app.py
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
"""
Module define fastapi server configuration
"""
from fastapi import FastAPI
from hypercorn.asyncio import serve
from hypercorn.config import Config as HyperCornConfig
from prometheus_client import Counter, generate_latest, CONTENT_TYPE_LATEST
from starlette.responses import Response
app = FastAPI()
REQUESTS = Counter('server_requests_total', 'Total number of requests to this webserver')
HEALTHCHECK_REQUESTS = Counter('healthcheck_requests_total', 'Total number of requests to healthcheck')
MAIN_ENDPOINT_REQUESTS = Counter('main_requests_total', 'Total number of requests to main endpoint')
BYE_ENDPOINT_REQUESTS = Counter('bye_request_total', 'Total number of requests to bye endpoint')
class SimpleServer:
"""
SimpleServer class define FastAPI configuration and implemented endpoints
"""
_hypercorn_config = None
def __init__(self):
self._hypercorn_config = HyperCornConfig()
async def run_server(self):
"""Starts the server with the config parameters"""
self._hypercorn_config.bind = ['0.0.0.0:8081']
self._hypercorn_config.keep_alive_timeout = 90
await serve(app, self._hypercorn_config)
@app.get("/health")
async def health_check():
"""Implement health check endpoint"""
# Increment counter used for register the total number of calls in the webserver
REQUESTS.inc()
# Increment counter used for register the requests to healtcheck endpoint
HEALTHCHECK_REQUESTS.inc()
return {"health": "ok"}
@app.get("/")
async def read_main():
"""Implement main endpoint"""
# Increment counter used for register the total number of calls in the webserver
REQUESTS.inc()
# Increment counter used for register the total number of calls in the main endpoint
MAIN_ENDPOINT_REQUESTS.inc()
return {"msg": "Hello World"}
@app.get("/bye")
async def read_bye():
"""Implement bye endpoint"""
# Increment counter used for register the total number of calls in the webserver
REQUESTS.inc()
# Increment counter used for register the total number of calls in the main endpoint
BYE_ENDPOINT_REQUESTS.inc()
return {"msg": "So long, and thanks for all the fish"}
@app.get("/metrics")
async def metrics():
"""Expose Prometheus metrics"""
return Response(generate_latest(), media_type=CONTENT_TYPE_LATEST)