diff options
Diffstat (limited to '')
| -rw-r--r-- | webmentions_ssg/__init__.py | 117 |
1 files changed, 12 insertions, 105 deletions
diff --git a/webmentions_ssg/__init__.py b/webmentions_ssg/__init__.py index 8fed1a7..63e9f13 100644 --- a/webmentions_ssg/__init__.py +++ b/webmentions_ssg/__init__.py @@ -8,47 +8,20 @@ import logging import logging.handlers import os from pathlib import Path -from urllib.parse import urlsplit import coloredlogs from flask import Flask -from flask_bootstrap import Bootstrap5 -from flask_limiter import Limiter -from flask_limiter.util import get_remote_address -from flask_mail import Mail -from flask_migrate import Migrate -from flask_sqlalchemy import SQLAlchemy -from flask_wtf.csrf import CSRFProtect -from sqlalchemy.orm import DeclarativeBase from werkzeug.middleware.proxy_fix import ProxyFix -from webmentions_ssg.config import ( - DefaultConfig, - DevelopmentConfig, - ProductionConfig, - normalize_allowed_hostnames, - normalize_ignored_hostnames, -) - -from .security import validate_secret_key -from .tasks import Huey -from .url_security import is_http_url - APP_NAME = "webmentions-ssg" VERSION = "0.0.2" - -class Base(DeclarativeBase): - pass - - -CSRF = CSRFProtect() -BOOTSTRAP = Bootstrap5() -DATABASE = SQLAlchemy(model_class=Base) -MIGRATE = Migrate() -HUEY = Huey() -MAIL = Mail() -LIMITER = Limiter(key_func=get_remote_address) +from .auth import AUTH +from .cli import DB_CLI, MAIL_CLI, USERS_CLI +from .config import DefaultConfig, DevelopmentConfig, ProductionConfig, validate_config +from .extensions import BOOTSTRAP, CSRF, DATABASE, HUEY, LIMITER, MAIL, MIGRATE +from .net import is_http_url +from .views import register_routes def create_app(config_class: type[DefaultConfig] | None = None) -> Flask: @@ -91,71 +64,15 @@ def create_app(config_class: type[DefaultConfig] | None = None) -> Flask: app.config.from_prefixed_env(prefix="FLASK") app.config.from_prefixed_env(prefix="WEBMENTIONS_SSG") - # A fixed key is convenient only for local debugging. All other - # deployments must supply their own persistent, random signing key. - using_local_dev_key = ( - issubclass(config_class, DevelopmentConfig) - and app.debug - and app.config.get("SECRET_KEY") == DevelopmentConfig.SECRET_KEY - ) - if not app.testing and not using_local_dev_key: - validate_secret_key(app.config.get("SECRET_KEY")) + app.config.update(validate_config(app.config, config_class=config_class)) - app.config["ALLOWED_HOSTNAMES"] = normalize_allowed_hostnames( - app.config["ALLOWED_HOSTNAMES"] - ) - app.config["IGNORED_HOSTNAMES"] = normalize_ignored_hostnames( - app.config["IGNORED_HOSTNAMES"] - ) - proxy_hops = app.config["PROXY_FIX_TRUSTED_HOPS"] - if type(proxy_hops) is not int or proxy_hops < 0: - raise ValueError("PROXY_FIX_TRUSTED_HOPS must be a non-negative integer.") - if proxy_hops: - # Trust only the configured number of proxies; never trust forwarded - # host, port or URL prefix headers for rate limiting. + if proxy_hops := app.config["PROXY_FIX_TRUSTED_HOPS"]: + # Do not trust forwarded host, port, or URL prefixes. app.wsgi_app = ProxyFix(app.wsgi_app, x_for=proxy_hops, x_proto=proxy_hops) - for key in ( - "MAX_CONTENT_LENGTH", - "MAX_WEBMENTION_URL_BYTES", - "RECEIVED_RETENTION_DAYS", - ): - value = app.config[key] - if type(value) is not int or value <= 0: - raise ValueError(f"{key} must be a positive integer.") - if not app.testing and not app.debug and not app.config["ALLOWED_HOSTNAMES"]: - raise RuntimeError("Configure ALLOWED_HOSTNAMES before running in production.") - - if ( - not app.testing - and not app.debug - and app.config["RATELIMIT_STORAGE_URI"] == "memory://" - ): - raise RuntimeError( - "Configure RATELIMIT_STORAGE_URI with shared storage before running in production." - ) - - if not app.testing and not app.debug: - # HUEY_URL overrides the legacy HUEY.url option; absent either, Huey - # falls back to an in-process queue that cannot serve a separate worker. - huey_url = app.config.get( - "HUEY_URL", app.config.get("HUEY", {}).get("url", "memory://") - ) - if isinstance(huey_url, str) and urlsplit(huey_url).scheme.lower() in { - "memory", - "blackhole", - }: - raise RuntimeError( - "Configure HUEY_URL with a shared, persistent queue before " - "running in production (memory:// and blackhole:// are unsafe)." - ) - - # Local development and tests should start from a fresh checkout: their - # default log directory and development SQLite instance are generated. - if app.testing or app.debug: - Path(app.config["LOG_FILE"]).parent.mkdir(parents=True, exist_ok=True) - if app.debug: + if app.debug or app.testing: Path(app.instance_path).mkdir(parents=True, exist_ok=True) + Path(app.config["LOG_FILE"]).parent.mkdir(parents=True, exist_ok=True) default_logging_formatter = logging.Formatter(app.config["LOG_FORMAT"]) @@ -197,8 +114,6 @@ def create_app(config_class: type[DefaultConfig] | None = None) -> Flask: LIMITER.init_app(app) MAIL.init_app(app) - from .auth import AUTH - AUTH.init_app(app) BOOTSTRAP.init_app(app) HUEY.init_app(app) @@ -214,20 +129,12 @@ def create_app(config_class: type[DefaultConfig] | None = None) -> Flask: app.add_template_filter(is_http_url) - from .views import root_page - - app.register_blueprint(root_page) - - from .cli.users import USERS_CLI + app.register_blueprint(register_routes()) app.cli.add_command(USERS_CLI) - from .cli.mail import MAIL_CLI - app.cli.add_command(MAIL_CLI) - from .cli.database import DB_CLI - app.cli.add_command(DB_CLI) return app |
