-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathinfrastructure.py
More file actions
177 lines (151 loc) · 6.06 KB
/
infrastructure.py
File metadata and controls
177 lines (151 loc) · 6.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import asyncio
import logging
from collections.abc import AsyncIterator, Iterator
from concurrent.futures import ThreadPoolExecutor
from typing import cast
from dishka import Provider, Scope, from_context, provide
from sqlalchemy.ext.asyncio import AsyncEngine, AsyncSession, async_sessionmaker, create_async_engine
from starlette.requests import Request
from app.infrastructure.adapters.bcrypt_password_hasher import HasherSemaphore, HasherThreadPoolExecutor
from app.infrastructure.auth_ctx.cookie_manager import CookieManager, CookieName
from app.infrastructure.auth_ctx.handlers.change_password import ChangePassword
from app.infrastructure.auth_ctx.handlers.log_in import LogIn
from app.infrastructure.auth_ctx.handlers.log_out import LogOut
from app.infrastructure.auth_ctx.handlers.sign_up import SignUp
from app.infrastructure.auth_ctx.jwt_processor import JwtProcessor
from app.infrastructure.auth_ctx.service import AuthService
from app.infrastructure.auth_ctx.sqla_transaction_manager import AuthSqlaTransactionManager
from app.infrastructure.auth_ctx.sqla_tx_storage import AuthSessionSqlaTxStorage
from app.infrastructure.auth_ctx.sqla_user_tx_storage import AuthSqlaUserTxStorage
from app.infrastructure.auth_ctx.types_ import AuthAsyncSession
from app.infrastructure.auth_ctx.utc_timer import AuthSessionUtcTimer
from app.main.config.settings import (
CookieSettings,
JwtSettings,
PasswordHasherSettings,
PostgresSettings,
SessionSettings,
SqlaSettings,
)
logger = logging.getLogger(__name__)
class HasherThreadPoolProvider(Provider):
scope = Scope.APP
@provide
def provide_hasher_threadpool_executor(
self,
settings: PasswordHasherSettings,
) -> Iterator[HasherThreadPoolExecutor]:
executor = HasherThreadPoolExecutor(
ThreadPoolExecutor(
max_workers=settings.MAX_THREADS,
thread_name_prefix="bcrypt",
)
)
yield executor
logger.debug("Disposing hasher threadpool executor...")
executor.shutdown(wait=True, cancel_futures=True)
logger.debug("Hasher threadpool executor is disposed.")
@provide
def provide_hasher_semaphore(self, settings: PasswordHasherSettings) -> HasherSemaphore:
return HasherSemaphore(asyncio.Semaphore(settings.MAX_THREADS))
class PersistenceSqlaProvider(Provider):
@provide(scope=Scope.APP)
async def provide_async_engine(
self,
postgres: PostgresSettings,
sqla: SqlaSettings,
) -> AsyncIterator[AsyncEngine]:
async_engine = create_async_engine(
url=postgres.dsn,
echo=sqla.ECHO,
echo_pool=sqla.ECHO_POOL,
pool_size=sqla.POOL_SIZE,
max_overflow=sqla.MAX_OVERFLOW,
connect_args={"connect_timeout": 5},
pool_pre_ping=True,
)
logger.debug("Async engine created with DSN: %s", postgres.dsn)
yield async_engine
logger.debug("Disposing async engine...")
await async_engine.dispose()
logger.debug("Engine is disposed.")
@provide(scope=Scope.APP)
def provide_async_session_factory(
self,
engine: AsyncEngine,
) -> async_sessionmaker[AsyncSession]:
async_session_factory = async_sessionmaker(
bind=engine,
class_=AsyncSession,
autoflush=False,
expire_on_commit=False,
)
logger.debug("Async session maker initialized.")
return async_session_factory
@provide(scope=Scope.REQUEST)
async def provide_primary_async_session(
self,
async_session_factory: async_sessionmaker[AsyncSession],
) -> AsyncIterator[AsyncSession]:
"""Provides UoW (AsyncSession) for the primary context"""
logger.debug("Starting primary async session...")
async with async_session_factory() as session:
logger.debug("Primary async session started.")
yield session
logger.debug("Closing primary async session...")
logger.debug("Primary async session closed.")
@provide(scope=Scope.REQUEST)
async def provide_auth_async_session(
self,
async_session_factory: async_sessionmaker[AsyncSession],
) -> AsyncIterator[AuthAsyncSession]:
"""Provides UoW (AsyncSession) for the auth context."""
logger.debug("Starting auth async session...")
async with async_session_factory() as session:
logger.debug("Auth async session started.")
yield cast(AuthAsyncSession, session)
logger.debug("Closing auth async session...")
logger.debug("Auth async session closed.")
class AuthProvider(Provider):
scope = Scope.REQUEST
# Auth context
auth_service = provide(AuthService)
@provide(scope=Scope.APP)
def provide_utc_auth_session_timer(
self,
settings: SessionSettings,
) -> AuthSessionUtcTimer:
return AuthSessionUtcTimer(
ttl=settings.ttl,
refresh_threshold_ratio=settings.REFRESH_THRESHOLD_RATIO,
)
auth_session_tx_storage = provide(AuthSessionSqlaTxStorage)
auth_tx_manager = provide(AuthSqlaTransactionManager)
@provide(scope=Scope.APP)
def provide_jwt_processor(
self,
settings: JwtSettings,
) -> JwtProcessor:
return JwtProcessor(
secret=settings.SECRET,
algorithm=settings.ALGORITHM,
)
@provide(scope=Scope.APP)
def provide_cookie_name(self, settings: CookieSettings) -> CookieName:
return CookieName(settings.NAME)
cookie_manager = provide(CookieManager)
auth_sqla_user_tx_storage = provide(AuthSqlaUserTxStorage)
# Account handlers
sign_up = provide(SignUp)
log_in = provide(LogIn)
change_password = provide(ChangePassword)
log_out = provide(LogOut)
class RequestProvider(Provider):
request = from_context(provides=Request, scope=Scope.REQUEST)
def infrastructure_providers() -> tuple[Provider, ...]:
return (
HasherThreadPoolProvider(),
PersistenceSqlaProvider(),
AuthProvider(),
RequestProvider(),
)