4
4
from litestar import Litestar , WebSocket , get , websocket
5
5
from litestar .datastructures import MutableScopeHeaders
6
6
from litestar .enums import ScopeType
7
- from litestar .middleware import AbstractMiddleware
8
- from litestar .types import Message , Receive , Scope , Send
7
+ from litestar .middleware import ASGIMiddleware
8
+ from litestar .types import ASGIApp , Message , Receive , Scope , Send
9
9
10
10
11
- class MyMiddleware (AbstractMiddleware ):
12
- scopes = { ScopeType .HTTP }
13
- exclude = [ "first_path" , "second_path" ]
11
+ class MyMiddleware (ASGIMiddleware ):
12
+ scopes = ( ScopeType .HTTP ,)
13
+ exclude_path_pattern = ( "first_path" , "second_path" )
14
14
exclude_opt_key = "exclude_from_my_middleware"
15
15
16
- async def __call__ (self , scope : Scope , receive : Receive , send : Send ) -> None :
16
+ async def handle (self , scope : Scope , receive : Receive , send : Send , next_app : ASGIApp ) -> None :
17
17
start_time = time .monotonic ()
18
18
19
19
async def send_wrapper (message : "Message" ) -> None :
@@ -23,7 +23,7 @@ async def send_wrapper(message: "Message") -> None:
23
23
headers ["X-Process-Time" ] = str (process_time )
24
24
await send (message )
25
25
26
- await self . app (scope , receive , send_wrapper )
26
+ await next_app (scope , receive , send_wrapper )
27
27
28
28
29
29
@websocket ("/my-websocket" )
@@ -68,5 +68,5 @@ def not_excluded_handler() -> Dict[str, str]:
68
68
third_handler ,
69
69
not_excluded_handler ,
70
70
],
71
- middleware = [MyMiddleware ],
71
+ middleware = [MyMiddleware () ],
72
72
)
0 commit comments