File size: 2,625 Bytes
ddebc3e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Config backend factory — follows ACCOUNT_STORAGE automatically."""

import os
from pathlib import Path

from app.platform.paths import data_path
from .base import ConfigBackend


def get_config_backend_name() -> str:
    """Return the active config backend name (mirrors ACCOUNT_STORAGE)."""
    return os.getenv("ACCOUNT_STORAGE", "local").strip().lower()


def create_config_backend() -> ConfigBackend:
    """Instantiate the config backend that matches the account storage backend.

    ``ACCOUNT_STORAGE=local``       → TOML file (``${DATA_DIR}/config.toml``)
    ``ACCOUNT_STORAGE=redis``       → Redis  (ACCOUNT_REDIS_URL)
    ``ACCOUNT_STORAGE=mysql``       → MySQL  (ACCOUNT_MYSQL_URL)
    ``ACCOUNT_STORAGE=postgresql``  → PostgreSQL (ACCOUNT_POSTGRESQL_URL)

    No extra env vars needed — reuses the same connection settings as accounts.
    """
    backend = get_config_backend_name()

    if backend == "local":
        return _make_toml()
    if backend == "redis":
        return _make_redis()
    if backend in ("mysql", "postgresql"):
        return _make_sql(backend)

    raise ValueError(f"Unknown account storage backend: {backend!r}")


def _make_toml() -> ConfigBackend:
    from .toml import TomlConfigBackend

    path_str = os.getenv("CONFIG_LOCAL_PATH", str(data_path("config.toml"))).strip()
    path = Path(path_str)
    if not path.is_absolute():
        path = Path(__file__).resolve().parents[5] / path
    return TomlConfigBackend(path)


def _make_redis() -> ConfigBackend:
    from redis.asyncio import Redis
    from .redis import RedisConfigBackend

    url = os.getenv("ACCOUNT_REDIS_URL", "").strip()
    if not url:
        raise ValueError("Redis config backend requires ACCOUNT_REDIS_URL")
    r = Redis.from_url(url, decode_responses=False)
    return RedisConfigBackend(r)


def _make_sql(dialect: str) -> ConfigBackend:
    from .sql import SqlConfigBackend
    from app.control.account.backends.sql import (
        create_mysql_engine,
        create_pgsql_engine,
    )

    if dialect == "mysql":
        url = os.getenv("ACCOUNT_MYSQL_URL", "").strip()
        if not url:
            raise ValueError("MySQL config backend requires ACCOUNT_MYSQL_URL")
        engine = create_mysql_engine(url)
    else:
        url = os.getenv("ACCOUNT_POSTGRESQL_URL", "").strip()
        if not url:
            raise ValueError("PostgreSQL config backend requires ACCOUNT_POSTGRESQL_URL")
        engine = create_pgsql_engine(url)

    return SqlConfigBackend(engine, dialect=dialect, dispose_engine=False)


__all__ = ["create_config_backend", "get_config_backend_name"]