Skip to content

Commit 40a3e52

Browse files
authored
refactor for healthz endpoint (#18)
2 parents 2b1023f + 666570b commit 40a3e52

File tree

3 files changed

+33
-12
lines changed

3 files changed

+33
-12
lines changed

app.json

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"healthchecks": {
3+
"api": [
4+
{
5+
"type": "startup",
6+
"name": "api check",
7+
"description": "Checking if the app responds to the /health/ready endpoint",
8+
"path": "/healthz",
9+
"attempts": 3
10+
}
11+
]
12+
}
13+
}

teufa/app.py

+5-11
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
from flask import Flask, g, request
2-
from sqlalchemy import select
1+
from flask import Flask
32

4-
from . import db as dbm
53
from .config import Config
64
from .ext import db
75
from .v1_api import bp as v1_bp
@@ -13,14 +11,6 @@ def create_app():
1311

1412
db.init_app(app)
1513

16-
@app.before_request
17-
def before_request():
18-
if not hasattr(g, "tenant"):
19-
hostname = request.host.split(":")[0]
20-
g.tenant = db.session.scalars(
21-
select(dbm.Tenant).filter_by(hostname=hostname).limit(1)
22-
).first()
23-
2414
@app.teardown_request
2515
def teardown_request(exc):
2616
if exc:
@@ -29,4 +19,8 @@ def teardown_request(exc):
2919

3020
app.register_blueprint(v1_bp)
3121

22+
@app.route("/healthz")
23+
def healthz():
24+
return {"status": "ok"}
25+
3226
return app

teufa/v1_api/__init__.py

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,23 @@
1-
from flask import Blueprint
1+
from flask import Blueprint, Flask, g, request
22
from flask_restful import Api
3+
from sqlalchemy import select
34

5+
from ..ext import db
6+
from . import db as dbm
47
from .flights import FlightCollectionResource, FlightResource
58

69
bp = Blueprint("api", __name__, url_prefix="/api")
10+
11+
12+
@bp.before_request
13+
def before_request():
14+
if not hasattr(g, "tenant"):
15+
hostname = request.host.split(":")[0]
16+
g.tenant = db.session.scalars(
17+
select(dbm.Tenant).filter_by(hostname=hostname).limit(1)
18+
).first()
19+
20+
721
api = Api(bp)
822

923
api.add_resource(FlightCollectionResource, "/v1/flights")

0 commit comments

Comments
 (0)