Coverage for src/lexigram/web/integrations/starlette.py: 0%

14 statements  

« prev     ^ index     » next       coverage.py v7.15.4, created at 2026-08-25 04:37 +0800

1"""Creates and configures the Starlette ASGI application instance.""" 

2 

3from __future__ import annotations 

4 

5from typing import Any 

6 

7from starlette.applications import Starlette 

8from starlette.middleware import Middleware 

9 

10from lexigram.contracts.core.di import ContainerResolverProtocol 

11from lexigram.logging import get_logger 

12 

13logger = get_logger(__name__) 

14 

15 

16def build_starlette_app( 

17 middleware: list[Middleware], 

18 exception_handlers: dict[Any, Any], 

19 lifespan: Any, 

20 container: ContainerResolverProtocol, 

21) -> Starlette: 

22 """Construct and configure the Starlette ASGI application. 

23 

24 Args: 

25 middleware: Pre-built middleware stack. 

26 exception_handlers: Exception handler mapping. 

27 lifespan: Lifespan context manager. 

28 container: Container resolver for state attachment. 

29 

30 Returns: 

31 Configured Starlette application. 

32 """ 

33 app = Starlette( 

34 middleware=middleware, 

35 lifespan=lifespan, 

36 exception_handlers=exception_handlers, 

37 ) 

38 

39 # Attach container to state and instance for access in middleware/handlers/tests 

40 app.state.container = container 

41 app.container = container # type: ignore[attr-defined] 

42 

43 logger.info("starlette_app_created", middleware_count=len(middleware)) 

44 return app 

45 

46 

47__all__ = ["build_starlette_app"]