Buckets:

download
raw
1.88 kB
"""Middleware, exception handlers, and lifecycle hook decorators for Server."""
from __future__ import annotations
from typing import Any, Callable, Type, Union
class ServerLifecycleMixin:
"""Register custom middleware, exception handlers, and startup/shutdown hooks."""
def middleware(self, middleware_type: str = "http") -> Callable:
"""Add middleware to the application.
Args:
middleware_type: Type of middleware ("http" or "websocket")
Returns:
Decorator function for middleware
"""
def decorator(func: Callable) -> Callable:
self.middleware_manager._custom_middleware.append(
{"func": func, "middleware_type": middleware_type}
)
return func
return decorator
def exception_handler(
self, exc_class_or_status_code: Union[int, Type[Exception]]
) -> Callable:
"""Add exception handler.
Args:
exc_class_or_status_code: Exception class or HTTP status code
Returns:
Decorator function for exception handlers
"""
def decorator(func: Callable) -> Callable:
self._exception_handlers[exc_class_or_status_code] = func
return func
return decorator
async def on_startup(self, func: Callable[[], Any]) -> Callable[[], Any]:
"""Register startup task.
Args:
func: Startup function
Returns:
The original function
"""
return self.lifecycle_manager.add_startup_hook(func)
async def on_shutdown(self, func: Callable[[], Any]) -> Callable[[], Any]:
"""Register shutdown task.
Args:
func: Shutdown function
Returns:
The original function
"""
return self.lifecycle_manager.add_shutdown_hook(func)

Xet Storage Details

Size:
1.88 kB
·
Xet hash:
ba3879e328a3cf444ce91c4e4d4e6f22a029654584417c414a76a6a15e69b5c7

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.