diff --git a/src/api.py b/src/api.py index 1f63f8c..88c18f7 100644 --- a/src/api.py +++ b/src/api.py @@ -20,6 +20,19 @@ else: app = Flask(__name__) +# This hook ensures that a connection is opened to handle any queries +# generated by the request. +@app.before_request +def _db_connect(): + db.connect() + +# This hook ensures that the connection is closed when we've finished +# processing the request. +@app.teardown_request +def _db_close(exc): + if not db.is_closed(): + db.close() + @app.route('/', methods=['GET']) def root(): query = PPETable.select() #.where(PPETable.is_active == True)