Settings
This commit is contained in:
110
app/lib/settings.py
Normal file
110
app/lib/settings.py
Normal file
@ -0,0 +1,110 @@
|
||||
"""All configuration via environment.
|
||||
|
||||
Take note of the environment variable prefixes required for each
|
||||
settings class, except `AppSettings`.
|
||||
"""
|
||||
from typing import Literal, Optional, Union
|
||||
|
||||
__all__ = [
|
||||
"APISettings",
|
||||
"AppSettings",
|
||||
"DatabaseSettings",
|
||||
"EmailSettings",
|
||||
"OpenAPISettings",
|
||||
"ServerSettings",
|
||||
]
|
||||
|
||||
from pydantic import Extra
|
||||
from pydantic_settings import BaseSettings
|
||||
|
||||
|
||||
class BaseEnvSettings(BaseSettings):
|
||||
class Config:
|
||||
env_file = ".env"
|
||||
env_file_encoding = "utf-8"
|
||||
extra = Extra.ignore
|
||||
|
||||
|
||||
class AppSettings(BaseEnvSettings):
|
||||
class Config:
|
||||
case_sensitive = True
|
||||
|
||||
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):
|
||||
class Config:
|
||||
env_prefix = "API_"
|
||||
case_sensitive = True
|
||||
|
||||
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):
|
||||
class Config:
|
||||
env_prefix = "OPENAPI_"
|
||||
case_sensitive = True
|
||||
|
||||
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):
|
||||
class Config:
|
||||
env_prefix = "DB_"
|
||||
case_sensitive = True
|
||||
|
||||
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"
|
||||
|
||||
|
||||
class ServerSettings(BaseEnvSettings):
|
||||
class Config:
|
||||
env_prefix = "UVICORN_"
|
||||
case_sensitive = True
|
||||
|
||||
HOST: str = "localhost"
|
||||
LOG_LEVEL: str = "info"
|
||||
PORT: int = 8000
|
||||
RELOAD: bool = True
|
||||
KEEPALIVE: int = 65
|
||||
|
||||
|
||||
class EmailSettings(BaseEnvSettings):
|
||||
class Config:
|
||||
env_prefix = "EMAIL_"
|
||||
case_sensitive = True
|
||||
|
||||
|
||||
# `.parse_obj()` thing is a workaround for pyright and pydantic interplay, see:
|
||||
# https://github.com/pydantic/pydantic/issues/3753#issuecomment-1087417884
|
||||
api = APISettings.parse_obj({})
|
||||
app = AppSettings.parse_obj({})
|
||||
db = DatabaseSettings.parse_obj({})
|
||||
openapi = OpenAPISettings.parse_obj({})
|
||||
server = ServerSettings.parse_obj({})
|
||||
Reference in New Issue
Block a user