refactor: clean up __init__ file following some Flask conventions
Flask extensions are loaded in a way that makes them easily reusable by blueprints. In this commit we are also adding `environs` to manage .env and settings: breaking changes! - FLASK_APP=lnbits.app - LNBITS_ALLOWED_USERS needs to be empty now to allow all users (NOT "all")
This commit is contained in:
committed by
fiatjaf
parent
ffa3c3f6a6
commit
1bc5e144d3
@@ -0,0 +1,75 @@
|
||||
import importlib
|
||||
|
||||
from flask import Flask
|
||||
from flask_assets import Bundle # type: ignore
|
||||
from flask_cors import CORS # type: ignore
|
||||
from flask_talisman import Talisman # type: ignore
|
||||
from werkzeug.middleware.proxy_fix import ProxyFix
|
||||
|
||||
from .commands import legacy_migrate
|
||||
from .core import core_app
|
||||
from .ext import assets, compress
|
||||
from .helpers import get_valid_extensions
|
||||
|
||||
|
||||
def create_app(config_object="lnbits.settings") -> Flask:
|
||||
"""Create application factory, as explained here: http://flask.pocoo.org/docs/patterns/appfactories/.
|
||||
:param config_object: The configuration object to use.
|
||||
"""
|
||||
app = Flask(__name__, static_folder="static")
|
||||
app.wsgi_app = ProxyFix(app.wsgi_app, x_proto=1, x_host=1) # type: ignore
|
||||
app.config.from_object(config_object)
|
||||
|
||||
register_flask_extensions(app)
|
||||
register_blueprints(app)
|
||||
register_filters(app)
|
||||
register_commands(app)
|
||||
|
||||
return app
|
||||
|
||||
|
||||
def register_blueprints(app) -> None:
|
||||
"""Register Flask blueprints / LNbits extensions."""
|
||||
app.register_blueprint(core_app)
|
||||
|
||||
for ext in get_valid_extensions():
|
||||
try:
|
||||
ext_module = importlib.import_module(f"lnbits.extensions.{ext.code}")
|
||||
app.register_blueprint(getattr(ext_module, f"{ext.code}_ext"), url_prefix=f"/{ext.code}")
|
||||
except Exception:
|
||||
raise ImportError(f"Please make sure that the extension `{ext.code}` follows conventions.")
|
||||
|
||||
|
||||
def register_commands(app):
|
||||
"""Register Click commands."""
|
||||
app.cli.add_command(legacy_migrate)
|
||||
|
||||
|
||||
def register_flask_extensions(app):
|
||||
"""Register Flask extensions."""
|
||||
"""If possible we use the .init_app() option so that Blueprints can also use extensions."""
|
||||
CORS(app)
|
||||
Talisman(
|
||||
app,
|
||||
force_https=app.config["FORCE_HTTPS"],
|
||||
content_security_policy={
|
||||
"default-src": [
|
||||
"'self'",
|
||||
"'unsafe-eval'",
|
||||
"'unsafe-inline'",
|
||||
"blob:",
|
||||
"api.opennode.co",
|
||||
]
|
||||
},
|
||||
)
|
||||
|
||||
assets.init_app(app)
|
||||
assets.register("base_css", Bundle("scss/base.scss", filters="pyscss", output="css/base.css"))
|
||||
compress.init_app(app)
|
||||
|
||||
|
||||
def register_filters(app):
|
||||
"""Jinja filters."""
|
||||
app.jinja_env.globals["DEBUG"] = app.config["DEBUG"]
|
||||
app.jinja_env.globals["EXTENSIONS"] = get_valid_extensions()
|
||||
app.jinja_env.globals["SITE_TITLE"] = app.config["LNBITS_SITE_TITLE"]
|
||||
Reference in New Issue
Block a user