aboutsummaryrefslogtreecommitdiff
path: root/webmentions_ssg/config.py
blob: e8288dba95439f3dedef292c1fa0201faca39756 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# SPDX-FileCopyrightText: 2026 Dennis Fink <me+coding@dennisfink.me>
#
# SPDX-License-Identifier: BSD-3-Clause

"""Define application settings for production, development, and testing."""

import logging
import secrets


def normalize_allowed_hostnames(value: object) -> set[str]:
    """Validate and normalize the configured collection of allowed hostnames."""
    if not isinstance(value, (list, tuple, set, frozenset)):
        raise TypeError(
            "ALLOWED_HOSTNAMES must be a list, tuple, or set of hostnames, not a string."
        )

    hostnames: set[str] = set()
    for hostname in value:
        if not isinstance(hostname, str) or not hostname.strip():
            raise ValueError("ALLOWED_HOSTNAMES must contain non-empty strings only.")
        hostnames.add(hostname.strip().lower())
    return hostnames


class DefaultConfig:
    TESTING = False
    DEBUG = False
    SECRET_KEY = secrets.token_urlsafe()
    BOOTSTRAP_SERVE_LOCAL = True
    # BOOTSTRAP_BOOTSWATCH_THEME = "Materia"
    # Secure by default; deployment configuration can override these values.
    SESSION_COOKIE_SECURE = True
    SESSION_COOKIE_HTTPONLY = True
    SESSION_COOKIE_SAMESITE = "Lax"
    LOG_FILE = "/var/log/webmentions-ssg.log"
    LOG_FORMAT = "[%(asctime)s]:%(levelname)s:%(name)s %(message)s"
    LOG_LEVEL = logging.INFO
    LOGFILE_MAX_BYTES = 20_000_000
    LOGFILE_BACKUP_COUNT = 10
    SQLALCHEMY_DATABASE_URI = "sqlite:///webmentions-ssg.db"

    # Empty by default: no target hostnames are accepted until configured.
    ALLOWED_HOSTNAMES = set()
    MAX_REDIRECTS = 20
    MAX_SOURCE_BYTES = 1_000_000
    REQUEST_TIMEOUT = 5.0
    # Requeue abandoned verification attempts after 15 minutes.
    VERIFICATION_TIMEOUT_SECONDS = 900
    VERIFICATION_REAPER_SCHEDULE = "*/5 * * * *"

    SOURCE_DIRECTORY = None
    SOURCE_BASE_URL = None
    SCANNER_SCHEDULE = "0 * * * *"
    # Total send attempts per source revision (not additional retries).
    SEND_MAX_ATTEMPTS = 6
    SEND_RETRY_BASE_SECONDS = 3600
    SEND_RETRY_MAX_SECONDS = 86400

    NOTIFICATION_EMAIL = None
    NOTIFICATION_SCHEDULE = "0 8 * * *"

    MAIL_SERVER = "localhost"
    MAIL_PORT = 25
    MAIL_USE_TLS = False
    MAIL_USE_SSL = False
    MAIL_USERNAME = None
    MAIL_PASSWORD = None
    MAIL_DEFAULT_SENDER = None

    RATELIMIT_STORAGE_URI = "memory://"
    RATELIMIT_HEADERS_ENABLED = True

    ENDPOINT_RATE_LIMIT = "10/minute;100/hour"
    ENDPOINT_GLOBAL_RATE_LIMIT = "1000/hour"
    ENDPOINT_REVERIFY_COOLDOWN_SECONDS = 600

    LOGIN_RATE_LIMIT = "5/minute;30/hour"


class DevelopmentConfig(DefaultConfig):
    DEBUG = True
    SESSION_COOKIE_SECURE = False
    LOG_FILE = "testing/webmentions-ssg.log"
    LOG_LEVEL = logging.DEBUG
    CONFIG_FILE = "testing/webmentions-ssg.cfg"


class ProductionConfig(DefaultConfig):
    CONFIG_FILE = "/etc/webmentions-ssg.cfg"


class TestingConfig(DefaultConfig):
    TESTING = True
    SESSION_COOKIE_SECURE = False

    SECRET_KEY = "testing-secret-key"
    WTF_CSRF_ENABLED = False

    LOG_FILE = "testing/webmentions-ssg-tests.log"
    LOG_LEVEL = logging.DEBUG

    SQLALCHEMY_DATABASE_URI = "sqlite:///:memory:"
    SQLALCHEMY_TRACK_MODIFICATIONS = False

    HUEY_URL = "memory://"

    ALLOWED_HOSTNAMES = {"dennisfink.me"}