2020-02-19 20:52:34 +01:00
|
|
|
import importlib
|
2019-12-13 22:51:41 +01:00
|
|
|
|
2020-03-08 23:00:41 +01:00
|
|
|
from flask import Flask
|
2020-04-26 13:28:19 +02:00
|
|
|
from flask_assets import Environment, Bundle # type: ignore
|
|
|
|
from flask_compress import Compress # type: ignore
|
2020-05-18 12:54:14 +02:00
|
|
|
from flask_cors import CORS # type: ignore
|
2020-04-26 13:28:19 +02:00
|
|
|
from flask_talisman import Talisman # type: ignore
|
2020-03-08 23:00:41 +01:00
|
|
|
from os import getenv
|
2020-04-21 14:12:29 +02:00
|
|
|
from werkzeug.middleware.proxy_fix import ProxyFix
|
2019-12-13 17:56:19 +01:00
|
|
|
|
2020-04-16 15:23:38 +02:00
|
|
|
from .core import core_app, migrations as core_migrations
|
2020-04-21 15:43:22 +02:00
|
|
|
from .helpers import ExtensionManager
|
2020-04-23 18:30:16 +02:00
|
|
|
from .settings import FORCE_HTTPS
|
2019-12-13 17:59:40 +01:00
|
|
|
|
|
|
|
|
2020-04-21 16:50:40 +02:00
|
|
|
disabled_extensions = getenv("LNBITS_DISABLED_EXTENSIONS", "").split(",")
|
|
|
|
|
2019-12-13 17:56:19 +01:00
|
|
|
app = Flask(__name__)
|
2020-08-31 04:19:43 +02:00
|
|
|
app.wsgi_app = ProxyFix(app.wsgi_app, x_proto=1, x_host=1) # type: ignore
|
2020-04-21 16:50:40 +02:00
|
|
|
valid_extensions = [ext for ext in ExtensionManager(disabled=disabled_extensions).extensions if ext.is_valid]
|
2020-02-19 20:52:34 +01:00
|
|
|
|
|
|
|
|
|
|
|
# optimization & security
|
|
|
|
# -----------------------
|
|
|
|
|
2020-03-04 23:11:15 +01:00
|
|
|
Compress(app)
|
2020-05-18 12:54:14 +02:00
|
|
|
CORS(app)
|
2020-02-10 13:29:20 +01:00
|
|
|
Talisman(
|
|
|
|
app,
|
2020-04-23 18:30:16 +02:00
|
|
|
force_https=FORCE_HTTPS,
|
2020-02-10 13:29:20 +01:00
|
|
|
content_security_policy={
|
2020-09-03 23:02:15 +02:00
|
|
|
"default-src": [
|
|
|
|
"'self'",
|
|
|
|
"'unsafe-eval'",
|
|
|
|
"'unsafe-inline'",
|
|
|
|
"blob:",
|
|
|
|
"api.opennode.co",
|
|
|
|
]
|
2020-02-10 13:29:20 +01:00
|
|
|
},
|
|
|
|
)
|
2020-01-31 21:07:05 +01:00
|
|
|
|
2020-02-19 20:52:34 +01:00
|
|
|
|
|
|
|
# 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.")
|
|
|
|
|
|
|
|
|
2020-01-31 21:07:05 +01:00
|
|
|
# filters
|
2020-02-19 20:52:34 +01:00
|
|
|
# -------
|
|
|
|
|
2020-03-04 23:11:15 +01:00
|
|
|
app.jinja_env.globals["DEBUG"] = app.config["DEBUG"]
|
2020-02-19 20:52:34 +01:00
|
|
|
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")
|
2019-12-13 17:56:19 +01:00
|
|
|
|
2020-01-31 21:07:05 +01:00
|
|
|
|
2020-03-04 23:11:15 +01:00
|
|
|
# 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
|
|
|
|
# --------
|
|
|
|
|
2020-08-31 04:19:43 +02:00
|
|
|
|
2020-04-16 15:23:38 +02:00
|
|
|
@app.cli.command("migrate")
|
|
|
|
def migrate_databases():
|
|
|
|
"""Creates the necessary databases if they don't exist already; or migrates them."""
|
|
|
|
core_migrations.migrate()
|
2019-12-13 17:56:19 +01:00
|
|
|
|
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-03-04 23:11:15 +01:00
|
|
|
|
2019-12-14 00:34:19 +01:00
|
|
|
|
2020-04-16 15:23:38 +02:00
|
|
|
# init
|
|
|
|
# ----
|
2019-12-13 17:56:19 +01:00
|
|
|
|
2020-03-10 23:12:22 +01:00
|
|
|
if __name__ == "__main__":
|
2020-03-04 23:11:15 +01:00
|
|
|
app.run()
|