Skip to content

Commit 8d6b84e

Browse files
authored
Merge pull request #1 from alexdiamandi/creationalPatternBranch
Creational pattern branch
2 parents 1c7e4ae + c3f0371 commit 8d6b84e

File tree

3 files changed

+25
-18
lines changed

3 files changed

+25
-18
lines changed

app/caches.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55

66
import aiocache
77

8-
from .config import get_settings
8+
from .config import _Settings
99

1010
LOGGER = logging.getLogger(name="app.caches")
1111

12-
SETTINGS = get_settings()
12+
SETTINGS = _Settings.get_settings()
1313

1414
if SETTINGS.rediscloud_url:
1515
REDIS_URL = SETTINGS.rediscloud_url

app/config.py

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88

99

1010
class _Settings(BaseSettings):
11+
12+
settingsInstance = None
13+
1114
port: int = 5000
1215
rediscloud_url: AnyUrl = None
1316
local_redis_url: AnyUrl = None
@@ -16,18 +19,23 @@ class _Settings(BaseSettings):
1619
# Sentry
1720
sentry_dsn: str = None
1821

22+
def __init__(self):
23+
if _Settings.settingsInstance:
24+
raise Exception("This is a singleton")
25+
else:
26+
_Settings.settingsInstance = self
1927

2028
@functools.lru_cache()
21-
def get_settings(**kwargs) -> BaseSettings:
22-
"""
23-
Read settings from the environment or `.env` file.
24-
https://pydantic-docs.helpmanual.io/usage/settings/#dotenv-env-support
25-
26-
Usage:
27-
import app.config
28-
29-
settings = app.config.get_settings(_env_file="")
30-
port_number = settings.port
31-
"""
32-
CFG_LOGGER.info("Loading Config settings from Environment ...")
33-
return _Settings(**kwargs)
29+
def get_settings(**kwargs) -> BaseSettings:
30+
"""
31+
Read settings from the environment or `.env` file.
32+
https://pydantic-docs.helpmanual.io/usage/settings/#dotenv-env-support
33+
34+
Usage:
35+
import app.config
36+
37+
settings = app.config._Settings.get_settings(_env_file="")
38+
port_number = settings.port
39+
"""
40+
CFG_LOGGER.info("Loading Config settings from Environment ...")
41+
return _Settings(**kwargs)

app/main.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from scout_apm.async_.starlette import ScoutMiddleware
1414
from sentry_sdk.integrations.asgi import SentryAsgiMiddleware
1515

16-
from .config import get_settings
16+
from .config import _Settings
1717
from .data import data_source
1818
from .routers import V1, V2
1919
from .utils.httputils import setup_client_session, teardown_client_session
@@ -23,7 +23,7 @@
2323
# ############
2424
LOGGER = logging.getLogger("api")
2525

26-
SETTINGS = get_settings()
26+
SETTINGS = _Settings.get_settings()
2727

2828
if SETTINGS.sentry_dsn: # pragma: no cover
2929
sentry_sdk.init(dsn=SETTINGS.sentry_dsn)
@@ -113,7 +113,6 @@ async def handle_validation_error(
113113
APP.include_router(V1, prefix="", tags=["v1"])
114114
APP.include_router(V2, prefix="/v2", tags=["v2"])
115115

116-
117116
# Running of app.
118117
if __name__ == "__main__":
119118
uvicorn.run(

0 commit comments

Comments
 (0)