37 lines
971 B
Python
37 lines
971 B
Python
from fastapi import FastAPI, Response
|
|
from prometheus_client import generate_latest, CONTENT_TYPE_LATEST
|
|
|
|
from .service import get_all_coordinates, get_coordinates_for_city
|
|
from .metrics import RequestTimer
|
|
|
|
app = FastAPI(title="Open-Meteo Coordinates Service")
|
|
|
|
|
|
@app.get("/coordinates")
|
|
def coordinates():
|
|
with RequestTimer(endpoint="/coordinates", method="GET") as t:
|
|
try:
|
|
return get_all_coordinates()
|
|
except Exception:
|
|
t.set_status("500")
|
|
raise
|
|
|
|
|
|
@app.get("/coordinates/{city}")
|
|
def coordinates_for_city(city: str):
|
|
with RequestTimer(endpoint="/coordinates/{city}", method="GET") as t:
|
|
try:
|
|
return get_coordinates_for_city(city)
|
|
except Exception:
|
|
t.set_status("500")
|
|
raise
|
|
|
|
@app.get("/metrics")
|
|
def metrics():
|
|
return Response(generate_latest(), media_type=CONTENT_TYPE_LATEST)
|
|
|
|
|
|
@app.get("/healthz")
|
|
def healthz():
|
|
return {"status": "ok"}
|