forked from aegis/pyserveX
cython path_matcher added to reduce time on hot operations
This commit is contained in:
+10
-14
@@ -8,7 +8,8 @@ This module provides functionality to mount external ASGI/WSGI applications
|
||||
import importlib
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from typing import Dict, Any, Optional, Callable, cast
|
||||
from typing import Any, Callable, Dict, Optional, cast
|
||||
|
||||
from starlette.types import ASGIApp, Receive, Scope, Send
|
||||
|
||||
from .logging_utils import get_logger
|
||||
@@ -74,20 +75,17 @@ class ASGIAppLoader:
|
||||
def _wrap_wsgi(self, wsgi_app: Callable) -> ASGIApp:
|
||||
try:
|
||||
from a2wsgi import WSGIMiddleware
|
||||
|
||||
return cast(ASGIApp, WSGIMiddleware(wsgi_app))
|
||||
except ImportError:
|
||||
logger.warning("a2wsgi not installed, trying asgiref")
|
||||
try:
|
||||
from asgiref.wsgi import WsgiToAsgi
|
||||
|
||||
return cast(ASGIApp, WsgiToAsgi(wsgi_app))
|
||||
except ImportError:
|
||||
logger.error(
|
||||
"Neither a2wsgi nor asgiref installed. "
|
||||
"Install with: pip install a2wsgi or pip install asgiref"
|
||||
)
|
||||
raise ImportError(
|
||||
"WSGI adapter not available. Install a2wsgi or asgiref."
|
||||
)
|
||||
logger.error("Neither a2wsgi nor asgiref installed. " "Install with: pip install a2wsgi or pip install asgiref")
|
||||
raise ImportError("WSGI adapter not available. Install a2wsgi or asgiref.")
|
||||
|
||||
def get_app(self, app_path: str) -> Optional[ASGIApp]:
|
||||
return self._apps.get(app_path)
|
||||
@@ -132,7 +130,7 @@ class MountedApp:
|
||||
if self.path == "":
|
||||
return original_path
|
||||
|
||||
new_path = original_path[len(self.path):]
|
||||
new_path = original_path[len(self.path) :]
|
||||
return new_path if new_path else "/"
|
||||
|
||||
|
||||
@@ -215,10 +213,7 @@ class ASGIMountManager:
|
||||
modified_scope["path"] = mount.get_modified_path(path)
|
||||
modified_scope["root_path"] = scope.get("root_path", "") + mount.path
|
||||
|
||||
logger.debug(
|
||||
f"Routing request to mounted app '{mount.name}': "
|
||||
f"{path} -> {modified_scope['path']}"
|
||||
)
|
||||
logger.debug(f"Routing request to mounted app '{mount.name}': " f"{path} -> {modified_scope['path']}")
|
||||
|
||||
try:
|
||||
await mount.app(modified_scope, receive, send)
|
||||
@@ -288,7 +283,8 @@ def create_django_app(
|
||||
os.environ.setdefault("DJANGO_SETTINGS_MODULE", settings_module)
|
||||
|
||||
try:
|
||||
from django.core.asgi import get_asgi_application # type: ignore[import-untyped]
|
||||
from django.core.asgi import get_asgi_application
|
||||
|
||||
return cast(ASGIApp, get_asgi_application())
|
||||
except ImportError as e:
|
||||
logger.error(f"Failed to load Django application: {e}")
|
||||
|
||||
Reference in New Issue
Block a user