-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathconfig.py
More file actions
62 lines (54 loc) · 1.99 KB
/
config.py
File metadata and controls
62 lines (54 loc) · 1.99 KB
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
import os
from typing import Any
from pydantic import (
PostgresDsn,
computed_field,
field_validator,
model_validator,
)
from pydantic_core import MultiHostUrl
from pydantic_settings import BaseSettings
class Settings(BaseSettings):
POSTGRES_SERVER: str
POSTGRES_PORT: int = 5432
POSTGRES_USER: str
POSTGRES_PASSWORD: str | None = None
POSTGRES_PASSWORD_FILE: str | None = None
POSTGRES_DB: str
@model_validator(mode="before")
@classmethod
def check_postgres_password(cls, data: Any) -> Any:
"""Validate that either POSTGRES_PASSWORD or POSTGRES_PASSWORD_FILE is set."""
if isinstance(data, dict):
password_file: str | None = data.get("POSTGRES_PASSWORD_FILE") # type: ignore
password: str | None = data.get("POSTGRES_PASSWORD") # type: ignore
if password_file is None and password is None:
raise ValueError(
"At least one of POSTGRES_PASSWORD_FILE and POSTGRES_PASSWORD must be set."
)
return data # type: ignore
@field_validator("POSTGRES_PASSWORD_FILE", mode="before")
@classmethod
def read_password_from_file(cls, v: str | None) -> str | None:
if v is not None:
file_path = v
if os.path.exists(file_path):
with open(file_path) as file:
return file.read().strip()
raise ValueError(f"Password file {file_path} does not exist.")
return v
@computed_field
@property
def SQLALCHEMY_DATABASE_URI(self) -> PostgresDsn:
url = MultiHostUrl.build(
scheme="postgresql+psycopg",
username=self.POSTGRES_USER,
password=self.POSTGRES_PASSWORD
if self.POSTGRES_PASSWORD
else self.POSTGRES_PASSWORD_FILE,
host=self.POSTGRES_SERVER,
port=self.POSTGRES_PORT,
path=self.POSTGRES_DB,
)
return PostgresDsn(url)
settings = Settings() # type: ignore