Skip to content

Commit 948595a

Browse files
committed
Create an AUTH configuration and put it in a context variable
Signed-off-by: Federico Busetti <[email protected]>
1 parent e8b8de7 commit 948595a

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

src/common/config.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,16 @@ class DramatiqConfig(BaseModel):
1212
REDIS_URL: Optional[str] = None
1313

1414

15+
class AuthConfig(BaseModel):
16+
JWT_ALGORITHM: str = "HS256"
17+
JWKS_URL: Optional[str] = None
18+
19+
1520
class AppConfig(BaseSettings):
1621
model_config = SettingsConfigDict(env_nested_delimiter="__")
1722

1823
APP_NAME: str = "bootstrap"
24+
AUTH: AuthConfig = AuthConfig()
1925
DRAMATIQ: DramatiqConfig = DramatiqConfig()
2026
DEBUG: bool = False
2127
ENVIRONMENT: TYPE_ENVIRONMENT = "local"

src/http_app/__init__.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,24 @@
77
from structlog import get_logger
88

99
from common import AppConfig, application_init
10+
from http_app import context
1011
from http_app.routes import init_routes
1112

1213

1314
def create_app(
1415
test_config: Union[AppConfig, None] = None,
1516
) -> FastAPI:
1617
app_config = test_config or AppConfig()
18+
19+
"""
20+
The config is submitted here at runtime, this means
21+
that we cannot declare a function to be used with
22+
FastAPI dependency injection system because Depends
23+
is evaluated before this function is called.
24+
A context variable will achieve the same purpose.
25+
"""
26+
context.app_config.set(app_config)
27+
1728
application_init(app_config)
1829
app = FastAPI(
1930
debug=app_config.DEBUG,

src/http_app/context.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from contextvars import ContextVar
2+
3+
from common import AppConfig
4+
5+
app_config: ContextVar[AppConfig] = ContextVar("app_config")

0 commit comments

Comments
 (0)