__init__.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. # -*- encoding: utf-8 -*-
  2. """
  3. Copyright (c) 2019 - present AppSeed.us
  4. """
  5. from flask import Flask
  6. from flask_login import LoginManager
  7. from flask_sqlalchemy import SQLAlchemy
  8. from importlib import import_module
  9. db = SQLAlchemy()
  10. login_manager = LoginManager()
  11. def register_extensions(app):
  12. db.init_app(app)
  13. login_manager.init_app(app)
  14. def register_blueprints(app):
  15. for module_name in ('authentication', 'home'):
  16. module = import_module('apps.{}.routes'.format(module_name))
  17. app.register_blueprint(module.blueprint)
  18. def configure_database(app):
  19. @app.before_first_request
  20. def initialize_database():
  21. try:
  22. db.create_all()
  23. except Exception as e:
  24. print('> Error: DBMS Exception: ' + str(e) )
  25. # fallback to SQLite
  26. basedir = os.path.abspath(os.path.dirname(__file__))
  27. app.config['SQLALCHEMY_DATABASE_URI'] = SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.join(basedir, 'db.sqlite3')
  28. print('> Fallback to SQLite ')
  29. db.create_all()
  30. @app.teardown_request
  31. def shutdown_session(exception=None):
  32. db.session.remove()
  33. from apps.authentication.oauth import github_blueprint
  34. def create_app(config):
  35. app = Flask(__name__)
  36. app.config.from_object(config)
  37. register_extensions(app)
  38. app.register_blueprint(github_blueprint, url_prefix="/login")
  39. register_blueprints(app)
  40. configure_database(app)
  41. return app