Add new app factory function and config
This commit is contained in:
parent
7e3db84b3c
commit
3ab6e2c48d
|
@ -0,0 +1,26 @@
|
||||||
|
from typing import Optional
|
||||||
|
import os
|
||||||
|
|
||||||
|
|
||||||
|
class AmanuensisConfig:
|
||||||
|
"""Base config type. Defines config keys for subclasses to override."""
|
||||||
|
|
||||||
|
# If CONFIG_FILE is defined, the config file it points to may override
|
||||||
|
# config values defined on the config object itself.
|
||||||
|
CONFIG_FILE: Optional[str] = None
|
||||||
|
STATIC_ROOT: Optional[str] = "static"
|
||||||
|
SECRET_KEY: Optional[str] = None
|
||||||
|
DATABASE_URI: Optional[str] = None
|
||||||
|
TESTING: bool = False
|
||||||
|
|
||||||
|
|
||||||
|
class EnvironmentConfig(AmanuensisConfig):
|
||||||
|
"""Loads config values from environment variables."""
|
||||||
|
|
||||||
|
CONFIG_FILE = os.environ.get("AMANUENSIS_CONFIG_FILE", AmanuensisConfig.CONFIG_FILE)
|
||||||
|
STATIC_ROOT = os.environ.get("AMANUENSIS_STATIC_ROOT", AmanuensisConfig.STATIC_ROOT)
|
||||||
|
SECRET_KEY = os.environ.get("AMANUENSIS_SECRET_KEY", AmanuensisConfig.SECRET_KEY)
|
||||||
|
DATABASE_URI = os.environ.get(
|
||||||
|
"AMANUENSIS_DATABASE_URI", AmanuensisConfig.DATABASE_URI
|
||||||
|
)
|
||||||
|
TESTING = os.environ.get("AMANUENSIS_TESTING", "").lower() in ("true", "1")
|
|
@ -1,46 +1,56 @@
|
||||||
import os
|
import json
|
||||||
|
|
||||||
from flask import Flask
|
from flask import Flask, g
|
||||||
|
|
||||||
from amanuensis.config import RootConfigDirectoryContext, ENV_CONFIG_DIR
|
from amanuensis.config import AmanuensisConfig
|
||||||
from amanuensis.models import ModelFactory
|
from amanuensis.db import DbContext
|
||||||
from .auth import get_login_manager, bp_auth
|
|
||||||
from .helpers import register_custom_filters
|
|
||||||
from .home import bp_home
|
|
||||||
from .lexicon import bp_lexicon
|
|
||||||
from .session import bp_session
|
|
||||||
|
|
||||||
|
|
||||||
def get_app(root: RootConfigDirectoryContext) -> Flask:
|
def get_app(
|
||||||
# Flask app init
|
config: AmanuensisConfig,
|
||||||
with root.read_config() as cfg:
|
db: DbContext = None,
|
||||||
app = Flask(
|
) -> Flask:
|
||||||
__name__,
|
"""Application factory"""
|
||||||
template_folder='.',
|
# Create the Flask object
|
||||||
static_folder=cfg.static_root
|
app = Flask(__name__, template_folder=".", static_folder=config.STATIC_ROOT)
|
||||||
)
|
|
||||||
app.secret_key = bytes.fromhex(cfg.secret_key)
|
|
||||||
app.config['root'] = root
|
|
||||||
app.config['model_factory'] = ModelFactory(root)
|
|
||||||
app.jinja_options['trim_blocks'] = True
|
|
||||||
app.jinja_options['lstrip_blocks'] = True
|
|
||||||
register_custom_filters(app)
|
|
||||||
|
|
||||||
# Flask-Login init
|
# Load keys from the config object
|
||||||
login_manager = get_login_manager(root)
|
app.config.from_object(config)
|
||||||
login_manager.init_app(app)
|
|
||||||
|
|
||||||
# Blueprint inits
|
# If a config file is now specified, also load keys from there
|
||||||
app.register_blueprint(bp_auth)
|
if app.config.get("CONFIG_FILE", None):
|
||||||
app.register_blueprint(bp_home)
|
app.config.from_file(app.config["CONFIG_FILE"], json.load)
|
||||||
app.register_blueprint(bp_lexicon)
|
|
||||||
app.register_blueprint(bp_session)
|
|
||||||
|
|
||||||
return app
|
# Assert that all required config values are now set
|
||||||
|
for config_key in ("SECRET_KEY", "DATABASE_URI"):
|
||||||
|
if not app.config.get(config_key):
|
||||||
|
raise Exception(f"{config_key} must be defined")
|
||||||
|
|
||||||
|
# Create the database context, if one wasn't already given
|
||||||
|
if db is None:
|
||||||
|
db = DbContext(app.config["DATABASE_URI"])
|
||||||
|
|
||||||
def default():
|
# Make the database connection available to requests via g
|
||||||
cwd = os.getcwd()
|
def db_setup():
|
||||||
config_dir = os.environ.get(ENV_CONFIG_DIR, "amanuensis")
|
g.db = db
|
||||||
root = RootConfigDirectoryContext(os.path.join(cwd, config_dir))
|
app.before_request(db_setup)
|
||||||
return get_app(root)
|
|
||||||
|
# Tear down the session on request teardown
|
||||||
|
def db_teardown(response_or_exc):
|
||||||
|
db.session.remove()
|
||||||
|
app.teardown_appcontext(db_teardown)
|
||||||
|
|
||||||
|
# Configure jinja options
|
||||||
|
app.jinja_options.update(trim_blocks=True, lstrip_blocks=True)
|
||||||
|
|
||||||
|
# Set up Flask-Login
|
||||||
|
# TODO
|
||||||
|
|
||||||
|
# Register blueprints
|
||||||
|
# TODO
|
||||||
|
|
||||||
|
def test():
|
||||||
|
return "Hello, world!"
|
||||||
|
app.route("/")(test)
|
||||||
|
|
||||||
|
return app
|
||||||
|
|
Loading…
Reference in New Issue