aboutsummaryrefslogtreecommitdiff
path: root/packet/__init__.py
blob: 82e211945302e7abb6360cae04203d36ddc39307 (plain) (blame)
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
"""
The application setup and initialization code lives here
"""

import json
import logging
import os

import csh_ldap
import onesignal_sdk.client as onesignal
from flask import Flask
from flask_gzip import Gzip
from flask_migrate import Migrate
from flask_pyoidc.flask_pyoidc import OIDCAuthentication
from flask_pyoidc.provider_configuration import ProviderConfiguration, ClientMetadata
from flask_sqlalchemy import SQLAlchemy

import sentry_sdk
from sentry_sdk.integrations.flask import FlaskIntegration
from sentry_sdk.integrations.sqlalchemy import SqlalchemyIntegration

from .git import get_version

app: Flask = Flask(__name__)
gzip = Gzip(app)

# Load default configuration and any environment variable overrides
_root_dir = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
app.config.from_pyfile(os.path.join(_root_dir, 'config.env.py'))

# Load file based configuration overrides if present
_pyfile_config = os.path.join(_root_dir, 'config.py')
if os.path.exists(_pyfile_config):
    app.config.from_pyfile(_pyfile_config)

# Fetch the version number
app.config['VERSION'] = get_version()

# Logger configuration
logging.getLogger().setLevel(app.config['LOG_LEVEL'])
app.logger.info('Launching packet ' + app.config['VERSION'])
app.logger.info('Using the {} realm'.format(app.config['REALM']))

# Initialize the extensions
db = SQLAlchemy(app)
migrate = Migrate(app, db)
app.logger.info('SQLAlchemy pointed at ' + repr(db.engine.url))

APP_CONFIG = ProviderConfiguration(issuer=app.config['OIDC_ISSUER'],
                          client_metadata=ClientMetadata(app.config['OIDC_CLIENT_ID'],
                                                            app.config['OIDC_CLIENT_SECRET']))

# Initialize Onesignal Notification apps
csh_onesignal_client = None
if app.config['ONESIGNAL_USER_AUTH_KEY'] and \
   app.config['ONESIGNAL_CSH_APP_AUTH_KEY'] and \
   app.config['ONESIGNAL_CSH_APP_ID']:
    csh_onesignal_client = onesignal.Client(
        user_auth_key=app.config['ONESIGNAL_USER_AUTH_KEY'],
        rest_api_key=app.config['ONESIGNAL_CSH_APP_AUTH_KEY'],
        app_id=app.config['ONESIGNAL_CSH_APP_ID']
    )
    app.logger.info('CSH Onesignal configured and notifications enabled')

intro_onesignal_client = None
if app.config['ONESIGNAL_USER_AUTH_KEY'] and \
   app.config['ONESIGNAL_INTRO_APP_AUTH_KEY'] and \
   app.config['ONESIGNAL_INTRO_APP_ID']:
    intro_onesignal_client = onesignal.Client(
        user_auth_key=app.config['ONESIGNAL_USER_AUTH_KEY'],
        rest_api_key=app.config['ONESIGNAL_INTRO_APP_AUTH_KEY'],
        app_id=app.config['ONESIGNAL_INTRO_APP_ID']
    )
    app.logger.info('Intro Onesignal configured and notifications enabled')

# OIDC Auth
auth = OIDCAuthentication({'app': APP_CONFIG}, app)
app.logger.info('OIDCAuth configured')

# Sentry
# pylint: disable=abstract-class-instantiated
sentry_sdk.init(
    dsn=app.config['SENTRY_DSN'],
    integrations=[FlaskIntegration(), SqlalchemyIntegration()]
)


# pylint: disable=wrong-import-position
from .ldap import ldap
from . import models
from . import context_processors
from . import commands
from .routes import api, shared

if app.config['REALM'] == 'csh':
    from .routes import upperclassmen
    from .routes import admin
else:
    from .routes import freshmen

app.logger.info('Routes registered')