config.py•1.02 kB
# app/core/config.py
from pydantic_settings import BaseSettings, SettingsConfigDict
from functools import lru_cache # For caching the settings instance
class Settings(BaseSettings):
OPENWEATHERMAP_API_KEY: str
# Specifies that settings should be loaded from an .env file
# For pydantic-settings v2.x.x and later:
model_config = SettingsConfigDict(env_file=".env", env_file_encoding='utf-8', extra='ignore')
# For older versions of pydantic (before v2) or older pydantic-settings,
# you might use a nested class `Config`:
# class Config:
# env_file = ".env"
# env_file_encoding = 'utf-8'
# extra = 'ignore' # Allows other env vars not defined in this model
# The lru_cache decorator caches the result of this function.
# This means the Settings object is created only once, the first time it's called.
@lru_cache
def get_settings() -> Settings:
return Settings()
# You can also instantiate settings directly if you prefer, for simpler cases
# settings = Settings()