111 lines
5.1 KiB
Python
111 lines
5.1 KiB
Python
## Copyright © 2026 Olaf Kolkman
|
|
## SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
from dataclasses import dataclass, field
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
|
|
from cryptography.fernet import Fernet
|
|
|
|
|
|
BASE_DIR = Path(__file__).resolve().parent.parent.parent
|
|
DB_PATH = BASE_DIR / 'data' / 'linklog.db'
|
|
VERSION_FILE = BASE_DIR.parent / 'frontend' / 'version.json'
|
|
|
|
|
|
def normalize_public_url(value: str) -> str:
|
|
value = value.strip().rstrip('/')
|
|
if '://' in value:
|
|
return value
|
|
scheme = 'http' if value.startswith(('localhost', '127.0.0.1')) else 'https'
|
|
return f'{scheme}://{value}'
|
|
|
|
|
|
def load_version() -> str:
|
|
# frontend/version.json is the single source of truth for the app version, shared by backend and frontend.
|
|
try:
|
|
return json.loads(VERSION_FILE.read_text())['version']
|
|
except (OSError, KeyError, ValueError):
|
|
return '0.0.0'
|
|
|
|
|
|
@dataclass
|
|
class Settings:
|
|
app_env: str = os.getenv('APP_ENV', 'development').lower()
|
|
app_name: str = os.getenv('LINKLOG_APP_NAME', 'LinkLog')
|
|
version: str = field(default_factory=load_version)
|
|
database_url: str = os.getenv('LINKLOG_DATABASE_URL', f'sqlite:///{DB_PATH}')
|
|
secret_key: str = os.getenv('LINKLOG_SECRET_KEY', 'dev-secret-key-change-me')
|
|
data_encryption_key: str = os.getenv('LINKLOG_DATA_ENCRYPTION_KEY', '')
|
|
token_expiry_minutes: int = int(os.getenv('LINKLOG_TOKEN_EXPIRY_MINUTES', '15'))
|
|
refresh_token_expiry_days: int = int(os.getenv('LINKLOG_REFRESH_TOKEN_EXPIRY_DAYS', '30'))
|
|
public_url: str = normalize_public_url(os.getenv('LINKLOG_PUBLIC_URL', 'linklog.example.com'))
|
|
smtp_host: str = os.getenv('LINKLOG_SMTP_HOST', '')
|
|
smtp_port: int = int(os.getenv('LINKLOG_SMTP_PORT', '587'))
|
|
smtp_username: str = os.getenv('LINKLOG_SMTP_USERNAME', '')
|
|
smtp_password: str = os.getenv('LINKLOG_SMTP_PASSWORD', '')
|
|
smtp_from: str = os.getenv('LINKLOG_SMTP_FROM', 'LinkLog <no-reply@localhost>')
|
|
smtp_use_tls: bool = os.getenv('LINKLOG_SMTP_USE_TLS', 'true').lower() in {'1', 'true', 'yes'}
|
|
email_verification_expiry_hours: int = int(os.getenv('LINKLOG_EMAIL_VERIFICATION_EXPIRY_HOURS', '24'))
|
|
password_reset_expiry_hours: int = int(os.getenv('LINKLOG_PASSWORD_RESET_EXPIRY_HOURS', '1'))
|
|
mastodon_client_name: str = os.getenv('LINKLOG_MASTODON_CLIENT_NAME', 'LinkLog')
|
|
mastodon_oauth_expiry_minutes: int = int(os.getenv('LINKLOG_MASTODON_OAUTH_EXPIRY_MINUTES', '10'))
|
|
max_post_characters: int = int(os.getenv('LINKLOG_MAX_POST_CHARACTERS', '500'))
|
|
log_level: str = os.getenv('LINKLOG_LOG_LEVEL', 'INFO').upper()
|
|
tracking_params: list[str] = None
|
|
feed_page_sizes: list[int] = None
|
|
|
|
def __post_init__(self):
|
|
if self.tracking_params is None:
|
|
default_tracking_params = [
|
|
'utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content',
|
|
'utm_id', 'utm_name', 'gclid', 'fbclid', 'dclid', 'msclkid', 'mc_cid',
|
|
'mc_eid', 'igshid', 'ga_source', 'ga_medium', 'ga_campaign', 'ga_place',
|
|
'campaignid', 'hsa_cr', 'hsa_cam', 'hsa_grp', 'hsa_ad', 'hsa_src',
|
|
'hsa_acc', 'hsa_net', 'hsa_mt', 'hsa_kw', 'hsa_ver', 'hsa_tgt',
|
|
]
|
|
configured_params = os.getenv('LINKLOG_TRACKING_PARAMS')
|
|
self.tracking_params = (
|
|
[item.strip() for item in configured_params.split(',') if item.strip()]
|
|
if configured_params
|
|
else default_tracking_params
|
|
)
|
|
if self.feed_page_sizes is None:
|
|
default_page_sizes = [25, 100, 250]
|
|
configured_sizes = os.getenv('LINKLOG_FRONTEND_LOADPOSTS')
|
|
parsed_sizes = []
|
|
if configured_sizes:
|
|
for item in configured_sizes.split(','):
|
|
item = item.strip()
|
|
if not item:
|
|
continue
|
|
try:
|
|
value = int(item)
|
|
except ValueError:
|
|
continue
|
|
if value > 0 and value not in parsed_sizes:
|
|
parsed_sizes.append(value)
|
|
if len(parsed_sizes) == 5:
|
|
break
|
|
self.feed_page_sizes = parsed_sizes or default_page_sizes
|
|
|
|
|
|
settings = Settings()
|
|
|
|
|
|
def validate_configuration(values: Settings) -> None:
|
|
if values.app_env == 'production':
|
|
if not values.secret_key or values.secret_key == 'dev-secret-key-change-me':
|
|
raise RuntimeError('LINKLOG_SECRET_KEY must be configured in production')
|
|
if len(values.secret_key) < 32 or len(set(values.secret_key)) < 12:
|
|
raise RuntimeError('LINKLOG_SECRET_KEY must be at least 32 characters with sufficient entropy')
|
|
if not values.data_encryption_key:
|
|
raise RuntimeError('LINKLOG_DATA_ENCRYPTION_KEY must be configured in production')
|
|
|
|
if values.data_encryption_key:
|
|
try:
|
|
Fernet(values.data_encryption_key.encode('ascii'))
|
|
except (ValueError, UnicodeEncodeError) as error:
|
|
raise RuntimeError('LINKLOG_DATA_ENCRYPTION_KEY must be a valid Fernet key') from error
|