aboutsummaryrefslogtreecommitdiff
path: root/jb/settings.py
blob: 0c6be216820da39828394c47314c1eeec06d8a70 (plain)
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
109
110
111
112
113
114
import json
from functools import lru_cache
from os.path import abspath
from os.path import dirname as pdirname
from pathlib import Path

from generalresearch.config import GRLBaseSettings, is_debug
from generalresearch.models.custom_types import (
    InfluxDsn,
    UUIDStr,
)
from pydantic import Field, HttpUrl, PostgresDsn, SecretStr, model_validator
from pydantic_settings import SettingsConfigDict

BASE_DIR = pdirname(pdirname(abspath(__file__)))

BASE_HTML_PATH = Path(BASE_DIR) / "templates" / "base.html"

MANIFEST_PATHS = [
    Path("/var/www/amt-jb-cdn") / "manifest-master.json",
    Path(BASE_DIR) / "jb-ui" / "dist/manifest.json",
    Path(BASE_DIR) / "jb-ui" / "dist/.vite/manifest.json",
]


def load_manifest():
    for path in MANIFEST_PATHS:
        if path.exists():
            with open(path) as f:
                return json.load(f)
    raise FileNotFoundError(
        f"No manifest found in any of: {[str(p) for p in MANIFEST_PATHS]}"
    )


def render_base_html() -> str:
    manifest = load_manifest()
    return (
        BASE_HTML_PATH.read_text()
        .replace("{{ css_file }}", manifest["style.css"]["file"])
        .replace("{{ js_file }}", manifest["src/main.tsx"]["file"])
    )


class Settings(GRLBaseSettings):
    model_config = SettingsConfigDict(
        env_file=(".env.test", ".env.testing", ".env.staging", ".env.prod"),
        env_file_encoding="utf-8",
        case_sensitive=False,
        extra="allow",
        cli_parse_args=False,
    )

    amt_jb_db: PostgresDsn | None = Field(default=None)

    # --- Pytest ---

    app_name: str = "AMT JB API"
    base_url: HttpUrl = Field(default=HttpUrl("https://jamesbillings67.com/"))

    fsb_host: HttpUrl = Field(default=HttpUrl("https://fsb.generalresearch.com/"))
    # Needed for admin function on fsb w/o authentication
    fsb_host_private_route: str | None = Field(default=None)

    product_id: UUIDStr | None = Field(default=None)

    influx_db: InfluxDsn | None = Field(default=None)

    session_token_ttl_seconds: int = Field(default=30 * 24 * 60 * 60, gt=0)  # 30 days
    session_jwt_secret: SecretStr | None = Field(default=None, min_length=32)

    magic_token_salt: SecretStr | None = Field(default=None, min_length=32)
    invite_amt_account_token: SecretStr | None = Field(default=None, min_length=32)

    gr_api_host: HttpUrl = Field(default=HttpUrl("https://generalresearch.com/api/v2/"))
    gr_api_token: SecretStr | None = Field(default=None, min_length=1)

    mautic_api_key: SecretStr | None = Field(default=None, min_length=32)

    @model_validator(mode="after")
    def validate_host_and_key(self) -> "Settings":

        self.debug = is_debug()

        if self.debug:
            return self

        if not self.amt_jb_db:
            raise ValueError("amt_jb_db is required")

        if not self.product_id:
            raise ValueError("product_id is required")

        if not self.session_jwt_secret:
            raise ValueError("session_jwt_secret is required")

        if not self.magic_token_salt:
            raise ValueError("magic_token_salt is required")

        if self.session_jwt_secret == self.magic_token_salt:
            raise ValueError("JWT Secret must be different than Magic Token Salt")

        if not self.gr_api_token:
            raise ValueError("gr_api_token is required")

        if not self.mautic_api_key:
            raise ValueError("mautic_api_key is required")

        return self


@lru_cache
def get_settings():
    return Settings()