114 lines
2.8 KiB
Python
114 lines
2.8 KiB
Python
"""All configuration via environment.
|
|
|
|
Take note of the environment variable prefixes required for each
|
|
settings class, except `AppSettings`.
|
|
"""
|
|
import sys
|
|
from typing import Literal, Optional, Union
|
|
|
|
__all__ = [
|
|
"APISettings",
|
|
"AppSettings",
|
|
"DatabaseSettings",
|
|
"EmailSettings",
|
|
"OpenAPISettings",
|
|
"ServerSettings",
|
|
]
|
|
|
|
from pydantic_settings import BaseSettings, SettingsConfigDict
|
|
|
|
from const import ROOT_DIR
|
|
|
|
|
|
class BaseEnvSettings(BaseSettings):
|
|
model_config = SettingsConfigDict(case_sensitive=True, extra="ignore", env_file_encoding="utf-8")
|
|
|
|
|
|
class AppSettings(BaseEnvSettings):
|
|
BUILD_NUMBER: str = "0"
|
|
DEBUG: bool = False
|
|
ENVIRONMENT: str = "local"
|
|
LOG_LEVEL: str = "INFO"
|
|
NAME: str = "addressbook"
|
|
|
|
@property
|
|
def slug(self) -> str:
|
|
return "-".join(s.lower() for s in self.NAME.split())
|
|
|
|
|
|
class APISettings(BaseEnvSettings):
|
|
model_config = SettingsConfigDict(env_prefix="API_")
|
|
|
|
CACHE_EXPIRATION: int = 60
|
|
DB_SESSION_DEPENDENCY_KEY: str = "db_session"
|
|
DEFAULT_PAGINATION_LIMIT: int = 100
|
|
DEFAULT_USER_NAME: str = "__default_user__"
|
|
HEALTH_PATH: str = "/health"
|
|
SECRET_KEY: str = "abc123"
|
|
USER_DEPENDENCY_KEY: str = "user"
|
|
|
|
|
|
class OpenAPISettings(BaseEnvSettings):
|
|
model_config = SettingsConfigDict(env_prefix="OPENAPI_")
|
|
|
|
TITLE: Optional[str] = "My Litestar App"
|
|
VERSION: str = "0.1.0"
|
|
CONTACT_NAME: str = "My Name"
|
|
CONTACT_EMAIL: str = "some_human@some_domain.com"
|
|
|
|
|
|
class DatabaseSettings(BaseEnvSettings):
|
|
model_config = SettingsConfigDict(env_prefix="DB_")
|
|
|
|
ECHO: bool = False
|
|
ECHO_POOL: Union[bool, Literal["debug"]] = False
|
|
POOL_DISABLE: bool = False
|
|
POOL_MAX_OVERFLOW: int = 10
|
|
POOL_SIZE: int = 5
|
|
POOL_TIMEOUT: int = 30
|
|
HOST: str = "localhost"
|
|
PORT: int = 5432
|
|
NAME: str = "db-name"
|
|
USER: str = "db-user"
|
|
PASSWORD: str = "db-password"
|
|
FLYWAY_PATH: str = "/usr/local/bin/flyway"
|
|
|
|
|
|
class TestingSettings(BaseEnvSettings):
|
|
model_config = SettingsConfigDict(env_prefix="TESTS_")
|
|
|
|
DB_TEMPLATE_NAME: str = "db-template-name"
|
|
DROP_DATABASE_BEFORE_TESTS: bool = True
|
|
DROP_DATABASE_AFTER_TESTS: bool = True
|
|
|
|
|
|
class ServerSettings(BaseEnvSettings):
|
|
model_config = SettingsConfigDict(env_prefix="UVICORN_")
|
|
|
|
HOST: str = "localhost"
|
|
LOG_LEVEL: str = "info"
|
|
PORT: int = 8000
|
|
RELOAD: bool = True
|
|
KEEPALIVE: int = 65
|
|
|
|
|
|
class EmailSettings(BaseEnvSettings):
|
|
model_config = SettingsConfigDict(env_prefix="EMAIL_")
|
|
|
|
|
|
if "pytest" in sys.modules:
|
|
env_file = ROOT_DIR / ".env.testing"
|
|
else:
|
|
env_file = ROOT_DIR / ".env"
|
|
|
|
params = {
|
|
"_env_file": env_file,
|
|
}
|
|
|
|
api = APISettings(**params)
|
|
app = AppSettings(**params)
|
|
db = DatabaseSettings(**params)
|
|
openapi = OpenAPISettings(**params)
|
|
server = ServerSettings(**params)
|
|
testing = TestingSettings(**params)
|