33import logging .config
44import typing as t
55
6- from ellar .app .context import ApplicationContext
76from ellar .auth .handlers import AuthenticationHandlerType
87from ellar .auth .middleware import IdentityMiddleware , SessionMiddleware
9- from ellar .common import GlobalGuard , IIdentitySchemes
8+ from ellar .common import (
9+ GlobalGuard ,
10+ IHostContext ,
11+ IHostContextFactory ,
12+ IIdentitySchemes ,
13+ )
1014from ellar .common .compatible import cached_property
1115from ellar .common .constants import (
1216 ELLAR_LOG_FMT_STRING ,
2125from ellar .common .types import ASGIApp , TReceive , TScope , TSend
2226from ellar .core .conf import Config
2327from ellar .core .connection import Request
28+ from ellar .core .context import ApplicationContext
2429from ellar .core .middleware import (
2530 CORSMiddleware ,
2631 ExceptionMiddleware ,
2732 Middleware ,
28- RequestServiceProviderMiddleware ,
2933 RequestVersioningMiddleware ,
34+ ServerErrorMiddleware ,
3035 TrustedHostMiddleware ,
3136)
3237from ellar .core .routing import ApplicationRouter , AppStaticFileMount
@@ -151,6 +156,7 @@ def debug(self, value: bool) -> None:
151156 def build_middleware_stack (self ) -> ASGIApp :
152157 service_middleware = self .injector .get (IExceptionMiddlewareService )
153158 service_middleware .build_exception_handlers (* self ._exception_handlers )
159+
154160 error_handler = service_middleware .get_500_error_handler ()
155161 allowed_hosts = self .config .ALLOWED_HOSTS
156162
@@ -175,7 +181,7 @@ def build_middleware_stack(self) -> ASGIApp:
175181 max_age = self .config .CORS_MAX_AGE ,
176182 ),
177183 Middleware (
178- RequestServiceProviderMiddleware ,
184+ ServerErrorMiddleware ,
179185 debug = self .debug ,
180186 handler = error_handler ,
181187 ),
@@ -219,19 +225,25 @@ def application_context(self) -> ApplicationContext:
219225 return ApplicationContext .create (app = self )
220226
221227 async def __call__ (self , scope : TScope , receive : TReceive , send : TSend ) -> None :
222- async with self .application_context ():
228+ async with self .application_context () as context :
223229 scope ["app" ] = self
224230
225- if self .middleware_stack is None :
226- self .middleware_stack = self .build_middleware_stack ()
231+ async with context .injector .create_asgi_args () as service_provider :
232+ context_factory = service_provider .get (IHostContextFactory )
233+ service_provider .update_scoped_context (
234+ IHostContext , context_factory .create_context (scope )
235+ )
236+
237+ if self .middleware_stack is None :
238+ self .middleware_stack = self .build_middleware_stack ()
227239
228- if (
229- self .config .STATIC_MOUNT_PATH
230- and self .config .STATIC_MOUNT_PATH not in self .router .routes
231- ):
232- self .router .append (AppStaticFileMount (self ))
240+ if (
241+ self .config .STATIC_MOUNT_PATH
242+ and self .config .STATIC_MOUNT_PATH not in self .router .routes
243+ ):
244+ self .router .add_route (AppStaticFileMount (self ))
233245
234- await self .middleware_stack (scope , receive , send )
246+ await self .middleware_stack (scope , receive , send )
235247
236248 @property
237249 def routes (self ) -> t .List [BaseRoute ]:
@@ -294,6 +306,7 @@ def get_module_loaders(self) -> t.Generator[ModuleTemplating, None, None]:
294306 yield loader
295307
296308 def _create_jinja_environment (self ) -> Environment :
309+ # TODO: rename to `create_jinja_environment`
297310 def select_jinja_auto_escape (filename : str ) -> bool :
298311 if filename is None : # pragma: no cover
299312 return True
0 commit comments