aboutsummaryrefslogtreecommitdiff
path: root/gn2/wqflask/startup.py
blob: 069755feea08fc404ac1fe7636caaad2d75f7628 (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
"""Checks to do before the application is started."""
from typing import Tuple

from flask import Blueprint, current_app, render_template

class StartupError(Exception):
    """Base class for Application Check Errors."""

class MissingConfigurationError(StartupError):
    """Raised in case of a missing required setting."""

    def __init__(self, missing=Tuple[str, ...]):
        """Initialise the MissingConfigurationError object."""
        super().__init__("At least one required configuration is missing.")
        self.missing = missing

startup_errors = Blueprint("app_check_errors", __name__)
__MANDATORY_CONFIGURATIONS__ = (
    "REDIS_URL", # URI to Redis server
    "SQL_URI", # URI to MariaDB server
    "GN_SERVER_URL", # REST API Server
    "AUTH_SERVER_URL" # Auth(entic/oris)ation Server
)

def check_mandatory_configs(app):
    """Check that all mandatory configuration settings are defined."""
    missing = tuple(
        setting for setting in __MANDATORY_CONFIGURATIONS__
        if (setting not in app.config
            or app.config.get(setting) is None
            or app.config.get(setting).strip() == ""))
    if len(missing) > 0:
        print(missing)
        raise MissingConfigurationError(missing)

@startup_errors.route("/")
def error_index():
    """Display errors experienced at application startup"""
    return render_template(
        "startup_errors.html",
        error_type = type(current_app.startup_error).__name__,
        error_value = current_app.startup_error)