|
1 | 1 | """Read and provide runtime configuration.""" |
2 | 2 |
|
3 | | -import logging |
4 | | -import os |
| 3 | +from functools import cache |
5 | 4 |
|
6 | | -from pydantic import BaseModel |
| 5 | +from pydantic_settings import BaseSettings, SettingsConfigDict |
7 | 6 |
|
8 | 7 | from {{ cookiecutter.project_slug }}.models import ServiceEnvironment |
9 | 8 |
|
10 | 9 |
|
11 | | -_logger = logging.getLogger(__name__) |
| 10 | +class Settings(BaseSettings): |
| 11 | + """Create app settings |
12 | 12 |
|
13 | | - |
14 | | -_ENV_VARNAME = "{{ cookiecutter.project_slug | upper }}_ENV" |
15 | | - |
16 | | - |
17 | | -class Config(BaseModel): |
18 | | - """Define app configuration data object.""" |
19 | | - |
20 | | - env: ServiceEnvironment |
21 | | - debug: bool |
22 | | - test: bool |
23 | | - |
24 | | - |
25 | | -def _dev_config() -> Config: |
26 | | - """Provide development environment configs |
27 | | -
|
28 | | - :return: dev env configs |
29 | | - """ |
30 | | - return Config(env=ServiceEnvironment.DEV, debug=True, test=False) |
31 | | - |
32 | | - |
33 | | -def _test_config() -> Config: |
34 | | - """Provide test env configs |
35 | | -
|
36 | | - :return: test configs |
37 | | - """ |
38 | | - return Config(env=ServiceEnvironment.TEST, debug=False, test=True) |
39 | | - |
40 | | - |
41 | | -def _staging_config() -> Config: |
42 | | - """Provide staging env configs |
43 | | -
|
44 | | - :return: staging configs |
45 | | - """ |
46 | | - return Config(env=ServiceEnvironment.STAGING, debug=False, test=False) |
47 | | - |
48 | | - |
49 | | -def _prod_config() -> Config: |
50 | | - """Provide production configs |
51 | | -
|
52 | | - :return: prod configs |
| 13 | + This is not a singleton, so every new call to this class will re-compute |
| 14 | + configuration settings, defaults, etc. |
53 | 15 | """ |
54 | | - return Config(env=ServiceEnvironment.PROD, debug=False, test=False) |
55 | | - |
56 | | - |
57 | | -def _default_config() -> Config: |
58 | | - """Provide default configs. This function sets what they are. |
59 | 16 |
|
60 | | - :return: default configs |
61 | | - """ |
62 | | - return _dev_config() |
| 17 | + model_config = SettingsConfigDict( |
| 18 | + env_prefix="{{ cookiecutter.project_slug }}_", |
| 19 | + env_file=".env", |
| 20 | + env_file_encoding="utf-8", |
| 21 | + extra="ignore" |
| 22 | + ) |
63 | 23 |
|
| 24 | + env: ServiceEnvironment = ServiceEnvironment.DEV |
| 25 | + debug: bool = False |
| 26 | + test: bool = False |
64 | 27 |
|
65 | | -_CONFIG_MAP = { |
66 | | - ServiceEnvironment.DEV: _dev_config, |
67 | | - ServiceEnvironment.TEST: _test_config, |
68 | | - ServiceEnvironment.STAGING: _staging_config, |
69 | | - ServiceEnvironment.PROD: _prod_config, |
70 | | -} |
71 | 28 |
|
| 29 | +@cache |
| 30 | +def get_config() -> Settings: |
| 31 | + """Get runtime configuration. |
72 | 32 |
|
73 | | -def _set_config() -> Config: |
74 | | - """Set configs based on environment variable `{{ cookiecutter.project_slug | upper }}_ENV`. |
| 33 | + This function is cached, so the config object only gets created/calculated once. |
75 | 34 |
|
76 | | - :return: complete config object with environment-specific parameters |
| 35 | + :return: Settings instance |
77 | 36 | """ |
78 | | - raw_env_value = os.environ.get(_ENV_VARNAME) |
79 | | - if not raw_env_value: |
80 | | - return _default_config() |
81 | | - try: |
82 | | - env_value = ServiceEnvironment(raw_env_value.lower()) |
83 | | - except ValueError: |
84 | | - _logger.error( |
85 | | - "Unrecognized value for %s: '%s'. Using default configs", |
86 | | - _ENV_VARNAME, |
87 | | - raw_env_value |
88 | | - ) |
89 | | - return _default_config() |
90 | | - return _CONFIG_MAP[env_value]() |
91 | | - |
92 | | - |
93 | | -config = _set_config() |
| 37 | + return Settings() |
0 commit comments