import os from logging.config import dictConfig from flask import Flask from flask_migrate import Migrate, upgrade from flask_sqlalchemy import SQLAlchemy from atheneum import utility db: SQLAlchemy = SQLAlchemy() dictConfig({ 'version': 1, 'formatters': {'default': { 'format': '[%(asctime)s] %(levelname)s in %(module)s: %(message)s', }}, 'handlers': {'wsgi': { 'class': 'logging.StreamHandler', 'stream': 'ext://flask.logging.wsgi_errors_stream', 'formatter': 'default' }}, 'root': { 'level': 'INFO', 'handlers': ['wsgi'] } }) def create_app(test_config: dict = None) -> Flask: app = Flask(__name__, instance_relative_config=True) app.logger.debug('Creating Atheneum Server') data_directory = os.getenv('ATHENEUM_DATA_DIRECTORY', '/tmp') app.logger.debug('Atheneum Data Directory: %s', data_directory) default_database_uri = 'sqlite:///{}/atheneum.db'.format(data_directory) app.config.from_mapping( SECRET_KEY='dev', SQLALCHEMY_DATABASE_URI=default_database_uri, SQLALCHEMY_TRACK_MODIFICATIONS=False ) if test_config is None: app.logger.debug('Loading configurations') app.config.from_object('atheneum.default_settings') app.config.from_pyfile('config.py', silent=True) if os.getenv('ATHENEUM_SETTINGS', None): app.config.from_envvar('ATHENEUM_SETTINGS') else: app.logger.debug('Loading test configuration') app.config.from_object(test_config) try: os.makedirs(app.instance_path) except OSError: pass app.json_encoder = utility.CustomJSONEncoder app.logger.debug('Initializing Application') db.init_app(app) app.logger.debug('Registering Database Models') Migrate(app, db) return app def register_blueprints(app: Flask) -> None: from atheneum.api import auth_blueprint app.register_blueprint(auth_blueprint) app = create_app() register_blueprints(app) def init_db() -> None: """Clear existing data and create new tables.""" upgrade('migrations') if __name__ == "__main__": app.run()