lnbits-legend/lnbits/__init__.py

91 lines
2.3 KiB
Python
Raw Normal View History

import importlib
2019-12-13 22:51:41 +01:00
2020-03-08 23:00:41 +01:00
from flask import Flask
from flask_assets import Environment, Bundle
from flask_compress import Compress
from flask_talisman import Talisman
2020-03-08 23:00:41 +01:00
from os import getenv
2020-04-16 15:23:38 +02:00
from .core import core_app, migrations as core_migrations
from .helpers import ExtensionManager, megajson
app = Flask(__name__)
valid_extensions = [ext for ext in ExtensionManager().extensions if ext.is_valid]
# optimization & security
# -----------------------
Compress(app)
Talisman(
app,
2020-03-08 23:00:41 +01:00
force_https=getenv("LNBITS_WITH_ONION", 0) == 0,
content_security_policy={
"default-src": [
"'self'",
"'unsafe-eval'",
"'unsafe-inline'",
2020-03-10 23:12:22 +01:00
"blob:",
2020-04-17 20:39:23 +02:00
"api.opennode.co",
"fonts.googleapis.com",
"fonts.gstatic.com",
"github.com",
"avatars2.githubusercontent.com",
]
},
)
# blueprints / extensions
# -----------------------
app.register_blueprint(core_app)
for ext in 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.")
# filters
# -------
app.jinja_env.globals["DEBUG"] = app.config["DEBUG"]
app.jinja_env.globals["EXTENSIONS"] = valid_extensions
2020-04-16 17:10:53 +02:00
app.jinja_env.globals["SITE_TITLE"] = getenv("LNBITS_SITE_TITLE", "LNbits")
app.jinja_env.filters["megajson"] = megajson
# assets
# ------
assets = Environment(app)
assets.url = app.static_url_path
assets.register("base_css", Bundle("scss/base.scss", filters="pyscss", output="css/base.css"))
2020-04-16 15:23:38 +02:00
# commands
# --------
@app.cli.command("migrate")
def migrate_databases():
"""Creates the necessary databases if they don't exist already; or migrates them."""
core_migrations.migrate()
2020-04-16 15:23:38 +02:00
for ext in valid_extensions:
try:
ext_migrations = importlib.import_module(f"lnbits.extensions.{ext.code}.migrations")
ext_migrations.migrate()
except Exception:
raise ImportError(f"Please make sure that the extension `{ext.code}` has a migrations file.")
2020-04-16 15:23:38 +02:00
# init
# ----
2020-03-10 23:12:22 +01:00
if __name__ == "__main__":
app.run()