statusforce/statusforce/app.py

49 lines
1.6 KiB
Python

import os
from flask import Flask
from flask_admin import Admin
from flask_login import LoginManager
from statusforce.db import db, session
from statusforce.models import Service, Incident, User
from statusforce.views import AuthAdminIndexView, AuthModelView, main
def get_app():
# Set up Flask application and initial config
app = Flask(__name__)
app.secret_key = b'super-secret-please-replace-me'
app.config['FLASK_ADMIN_SWATCH'] = 'cosmo'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///statusforce.sqlite'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
# Pull configuration from environment variables
for key, value in os.environ.items():
if key.startswith('FLASK_ADMIN_') or key.startswith('SQLALCHEMY_'):
app.config[key] = value
elif key == 'FLASK_SECRET_KEY':
# Special case, apparently
app.secret_key = value
elif key.startswith('FLASK_'):
app.config[key.replace('FLASK_', '')] = value
# Set up database
db.init_app(app)
with app.app_context():
db.create_all()
# Set up the authentication
login_manager = LoginManager(app)
@login_manager.user_loader
def load_user(user_id):
return User.query.get(user_id)
# Set up admin interface
admin = Admin(app, name='StatusForce', index_view=AuthAdminIndexView(), template_mode='bootstrap4')
admin.add_view(AuthModelView(Service, session))
admin.add_view(AuthModelView(Incident, session))
# Finally, attache the blueprint
app.register_blueprint(main)
return app