Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Investigate neccessity of config file for all settings. #113

Closed
davidjnevin opened this issue Nov 26, 2023 · 1 comment
Closed

Investigate neccessity of config file for all settings. #113

davidjnevin opened this issue Nov 26, 2023 · 1 comment
Assignees

Comments

@davidjnevin
Copy link
Owner

eg:

from functools import lru_cache
from typing import Optional

from pydantic_settings import BaseSettings, SettingsConfigDict

class BaseConfig(BaseSettings):
ENV_STATE: Optional[str] = None

model_config = SettingsConfigDict(
    env_file=".env", extra="ignore", env_file_encoding="utf-8"
)

class GlobalConfig(BaseConfig):
DATABASE_URL: Optional[str] = None
DB_FORCE_ROLL_BACK: bool = False
LOG_FILE: Optional[str] = None
LOGTAIL_API_KEY: Optional[str] = None
JWT_ALGORITHM: Optional[str] = None
JWT_SECRET_KEY: Optional[str] = None
MAILGUN_API_KEY: Optional[str] = None
MAILGUN_DOMAIN: Optional[str] = None
B2_API_KEY_ID: Optional[str] = None
B2_BUCKET_NAME: Optional[str] = None
B2_API_KEY: Optional[str] = None
OPENAI_API_KEY: Optional[str] = None
OPENAI_IMAGE_SIZE: Optional[str] = None
SENTRY_DSN: Optional[str] = None

class DevConfig(GlobalConfig):
model_config = SettingsConfigDict(env_prefix="DEV_")

class ProdConfig(GlobalConfig):
model_config = SettingsConfigDict(env_prefix="PROD_")

class TestConfig(GlobalConfig):
DATABASE_URL: str = "sqlite:///test.db"
DB_FORCE_ROLL_BACK: bool = True
JWT_SECRET_KEY: str = (
"4598398cca0a7ecb7c7466fb30e43d4525bb3f5c59974183c8f46724e63ccee7"
)
JWT_ALGORITHM: str = "HS256"

model_config = SettingsConfigDict(env_prefix="TEST_")

@lru_cache()
def get_config(env_state: str):
configs = {"dev": DevConfig, "prod": ProdConfig, "test": TestConfig}
return configsenv_state

config = get_config(BaseConfig().ENV_STATE)

@davidjnevin davidjnevin self-assigned this Nov 26, 2023
@davidjnevin
Copy link
Owner Author

Implemented. It seemed more maintainable to have a single config for all future .env variables. Pre-existing in infrastructure untouched. Issue #115

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant