aboutsummaryrefslogtreecommitdiff
path: root/wsgi.py
blob: 9fb63cbd79d3ac2c45b434c90e8be05b2e3a3c5f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
"""Run the application"""
import sys
from logging import getLogger, StreamHandler

from flask import Flask

from qc_app import create_app
from qc_app.check_connections import check_db, check_redis

def check_and_build_app() -> Flask:
    """Setup the application for running."""
    # Setup the app
    appl = create_app()

    # Check connections
    check_db(appl.config["SQL_URI"])
    check_redis(appl.config["REDIS_URL"])
    return appl

def setup_logging(appl: Flask):
    """Setup application logging"""
    loglevel = appl.config["LOG_LEVEL"].upper()

    # Maybe call `logging.dictConfig(…)` here instead of all this stuff below
    handler_stderr = StreamHandler(stream=sys.stderr)
    appl.logger.addHandler(handler_stderr)

    rootlogger = getLogger()
    rootlogger.addHandler(handler_stderr)
    rootlogger.setLevel(loglevel)

    appl.logger.setLevel(loglevel)

app = check_and_build_app()

if __name__ != "__main__":# Running via gunicorn
    gunicorn_logger = getLogger("gunicorn.error")
    app.logger.handlers = gunicorn_logger.handlers
    app.logger.setLevel(gunicorn_logger.level)#pylint: disable=[no-member]

if __name__ == "__main__":# Running via flask
    # Run the app
    setup_logging(app)
    app.run()