Restore RSGI support directly on KayaApp instances

This commit is contained in:
2026-07-15 14:10:48 +00:00
parent 7c766caf47
commit 2662efe6a8
+20 -1
View File
@@ -2,7 +2,7 @@ from abc import ABC, abstractmethod
from asyncio import Queue, AbstractEventLoop from asyncio import Queue, AbstractEventLoop
from asyncio import get_running_loop from asyncio import get_running_loop
from logging import getLogger from logging import getLogger
from typing import Callable, Awaitable, Any, Mapping, Sequence, Optional, Unpack, Tuple, cast from typing import Callable, Awaitable, Any, Mapping, Sequence, Optional, Tuple, cast
from pwo import Maybe, AsyncQueueIterator from pwo import Maybe, AsyncQueueIterator
from ._http_context import HttpContext from ._http_context import HttpContext
from ._http_method import HttpMethod from ._http_method import HttpMethod
@@ -12,6 +12,7 @@ from ._websocket import WebSocket
from ._asgi import AsgiContext, AsgiWebSocket from ._asgi import AsgiContext, AsgiWebSocket
from ._tree import Tree from ._tree import Tree
from ._types.asgi import LifespanScope, HTTPScope as ASGIHTTPScope, WebSocketScope as ASGIWebSocketScope from ._types.asgi import LifespanScope, HTTPScope as ASGIHTTPScope, WebSocketScope as ASGIWebSocketScope
from typing_extensions import Unpack
log = getLogger(__name__) log = getLogger(__name__)
@@ -69,6 +70,22 @@ class AbstractKayaApp(ABC):
async def handle_websocket(self, ws: WebSocket) -> None: async def handle_websocket(self, ws: WebSocket) -> None:
raise NotImplementedError() raise NotImplementedError()
def __rsgi_init__(self, loop: AbstractEventLoop) -> None:
self.setup(loop)
def __rsgi_del__(self, loop: AbstractEventLoop) -> None:
self.shutdown(loop)
async def __rsgi__(self, scope: Any, protocol: Any) -> None:
try:
from kaya.rsgi import RsgiContext, RsgiWebSocket
except ImportError as exc:
raise RuntimeError("RSGI support requires kaya-rsgi to be installed") from exc
if scope.proto == 'ws':
await self.handle_websocket(RsgiWebSocket(scope, protocol))
else:
await self.handle_request(RsgiContext(scope, protocol))
class KayaApp(AbstractKayaApp): class KayaApp(AbstractKayaApp):
_tree: Tree _tree: Tree
@@ -145,3 +162,5 @@ class KayaApp(AbstractKayaApp):
def PATCH(self, path: str, recursive: bool = False) -> Callable[[HttpHandler], HttpHandler]: def PATCH(self, path: str, recursive: bool = False) -> Callable[[HttpHandler], HttpHandler]:
return self.route(path, (HttpMethod.PATCH,), recursive) return self.route(path, (HttpMethod.PATCH,), recursive)