Static files are configured using a handful of config values. Static files can be handled remotely or locally for prod or dev environments, respectively. Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
49 lines
1.4 KiB
Python
49 lines
1.4 KiB
Python
from pathlib import Path
|
|
import os
|
|
from typing import Sequence
|
|
|
|
from dotenv import load_dotenv
|
|
|
|
|
|
load_dotenv()
|
|
|
|
|
|
class ConfigError(Exception):
|
|
def __init__(self, name: str, reason: str):
|
|
super().__init__(f"configuration option {name}: {reason}")
|
|
|
|
|
|
def required(name: str) -> str:
|
|
value = os.getenv(name)
|
|
if value is None:
|
|
raise ConfigError(name, "required but not supplied in the environment")
|
|
return value
|
|
|
|
|
|
def default(name: str, default_value: str) -> str:
|
|
return os.getenv(name, default_value)
|
|
|
|
|
|
def one_of_default(name: str, values: Sequence[str], default_value: str) -> str:
|
|
value = default(name, default_value)
|
|
if value not in values:
|
|
required = ",".join(f'"{v}"' for v in values)
|
|
raise ConfigError(name, f"must be one of {required}; got {value} instead")
|
|
return value
|
|
|
|
|
|
THUMBS_DIR = Path(default("THUMBS_DIR", "thumbs"))
|
|
CACHE_DIR = Path(default("CACHE_DIR", "cache"))
|
|
|
|
DB_SQLITE3_PATH = Path(default("DB_SQLITE3_PATH", "bans.db"))
|
|
|
|
HTTP_DOMAIN = required("HTTP_DOMAIN")
|
|
HTTP_ROOT = default("HTTP_ROOT", r"")
|
|
|
|
STATIC_HANDLER = one_of_default("STATIC_HANDLER", ("remote", "local"), "local")
|
|
STATIC_LOCAL_PATH = Path(default("STATIC_LOCAL_PATH", "static"))
|
|
STATIC_LOCAL_FOLLOW_SYMLINKS = one_of_default(
|
|
"STATIC_LOCAL_FOLLOW_SYMLINKS", ("yes", "true", "1", "no", "false", "0"), "true"
|
|
) in ("yes", "true", "1")
|
|
STATIC_ROOT = default("STATIC_ROOT", "/static")
|