import os
import properties

from flask import Flask, send_from_directory

from api import auth_api, account_api
import database as db

app = Flask(__name__)

# This hook ensures that a connection is opened to handle any queries
# generated by the request.
# These hooks are called on every single request.  Probably not an
# appropriate method for handling connection pooling...
@app.before_request
def _db_connect():
    db.myDB.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.myDB.is_closed():
        db.myDB.close()

@app.route("/")
def main():
    return send_from_directory("static/", "index.html")

PORT = int(os.getenv('PORT', properties.d["port"]))

app.register_blueprint(auth_api.auth_api)
app.register_blueprint(account_api.account_api)

if __name__ == '__main__':
    
    app.run(debug=properties.d["debug"], host=properties.d["host"], port=PORT)