68 lines
1.8 KiB
Python
68 lines
1.8 KiB
Python
from flask import Flask, render_template, g
|
|
from werkzeug.exceptions import HTTPException
|
|
|
|
from flask_mobility import Mobility
|
|
from flask_session import Session
|
|
|
|
from . import filters
|
|
from .lib import OIDCAuthentication
|
|
import os
|
|
|
|
mobility = Mobility()
|
|
|
|
auth = OIDCAuthentication()
|
|
# SESSION_TYPE = 'filesystem'
|
|
sess = Session()
|
|
|
|
|
|
def create_app(environment='development'):
|
|
|
|
from config import config
|
|
|
|
# Instantiate app.
|
|
app_prefix = os.getenv('APPLICATION_ROOT', '')
|
|
app = Flask(__name__,
|
|
static_url_path=f"{app_prefix}/static")
|
|
|
|
# Set app config.
|
|
env = os.environ.get('FLASK_ENV', environment)
|
|
app.config.from_object(config[env])
|
|
app.config.from_prefixed_env(prefix="HSMAN")
|
|
config[env].configure(app)
|
|
app.config['APP_TZ'] = os.environ.get('TZ', 'UTC')
|
|
app.config['ADMIN_GROUPS'] = list(
|
|
map(str.strip, app.config.get('ADMIN_GROUPS', "").split(',')))
|
|
app.logger.debug(f"admin groups: {app.config['ADMIN_GROUPS']}")
|
|
|
|
app.logger.info("middleware init: mobility")
|
|
mobility.init_app(app)
|
|
|
|
app.logger.info("middleware init: auth")
|
|
auth.init_app(app)
|
|
|
|
# Register blueprints.
|
|
from .views import main_blueprint, rest_blueprint
|
|
app.logger.info(f"register blueprint: 'main' [prefix '{
|
|
main_blueprint.url_prefix}']")
|
|
app.register_blueprint(main_blueprint)
|
|
|
|
app.logger.info(f"register blueprint: 'rest' [prefix '{
|
|
rest_blueprint.url_prefix}']")
|
|
app.register_blueprint(rest_blueprint)
|
|
|
|
app.logger.info("jinja2 custom filters loaded")
|
|
filters.init_app(app)
|
|
|
|
sess.init_app(app)
|
|
|
|
# Error handlers.
|
|
@app.errorhandler(HTTPException)
|
|
def handle_http_error(exc):
|
|
return render_template('error.html', error=exc), exc.code
|
|
|
|
@app.context_processor
|
|
def inject_auth():
|
|
return dict(auth=auth)
|
|
|
|
return app
|