How do you write a minimal health-check service with Flask?
BeginnerConceptTesting, services & metrics
Answer
from flask import Flask, jsonify
app = Flask(__name__)
@app.get("/healthz")
def healthz():
return jsonify(status="ok")
@app.get("/readyz")
def readyz():
ok = db_ping()
return jsonify(db=ok), (200 if ok else 503)
Run it with a production server: gunicorn -w 2 -b 0.0.0.0:8000 app:app. flask run and app.run() start the Werkzeug development server, which prints a warning and is not meant for production.
More on Testing, services & metrics
Why should automation scripts have tests at all?BeginnerHow does pytest discover and run tests?BeginnerWhy does pytest use plain `assert` instead of `assertEqual`?BeginnerWhat are pytest fixtures, and what scopes exist?IntermediateWhat built-in fixtures do you use most?IntermediateHow does `@pytest.mark.parametrize` work?Beginner