|
|
@ -1,3 +1,5 @@ |
|
|
|
|
|
|
|
import asyncio |
|
|
|
|
|
|
|
|
|
|
|
from django.http import Http404 |
|
|
|
from django.http import Http404 |
|
|
|
from django_scopes import scope |
|
|
|
from django_scopes import scope |
|
|
|
|
|
|
|
|
|
|
@ -5,22 +7,39 @@ from scopedsites.models import Domain |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class DomainScopeMiddleware: |
|
|
|
class DomainScopeMiddleware: |
|
|
|
|
|
|
|
async_capable = True |
|
|
|
|
|
|
|
|
|
|
|
def __init__(self, get_response): |
|
|
|
def __init__(self, get_response): |
|
|
|
self.get_response = get_response |
|
|
|
self.get_response = get_response |
|
|
|
|
|
|
|
|
|
|
|
def __call__(self, request): |
|
|
|
self.is_async = asyncio.iscoroutinefunction(get_response) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.__call__ = self._ahandler if self.is_async else self._handler |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def _ahandler(self, request): |
|
|
|
|
|
|
|
d = Domain.get_from_request(request) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
with scope(domain=d): |
|
|
|
|
|
|
|
return await self.get_response(request) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _handler(self, request): |
|
|
|
d = Domain.get_from_request(request) # technically does hit the db layer, but should get caught in memcache |
|
|
|
d = Domain.get_from_request(request) # technically does hit the db layer, but should get caught in memcache |
|
|
|
# TODO: performance testing |
|
|
|
# TODO: performance testing |
|
|
|
|
|
|
|
|
|
|
|
with scope(domain=d): |
|
|
|
with scope(domain=d): |
|
|
|
response = self.get_response(request) |
|
|
|
return self.get_response(request) |
|
|
|
|
|
|
|
|
|
|
|
return response |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class DomainAutoCreateMiddleware: |
|
|
|
class DomainAutoCreateMiddleware: |
|
|
|
|
|
|
|
async_capable = True |
|
|
|
|
|
|
|
|
|
|
|
def __init__(self, get_response): |
|
|
|
def __init__(self, get_response): |
|
|
|
self.get_response = get_response |
|
|
|
self.get_response = get_response |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.is_async = asyncio.iscoroutinefunction(get_response) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.__call__ = self._ahandler if self.is_async else self._handler |
|
|
|
|
|
|
|
|
|
|
|
self.cache = set() |
|
|
|
self.cache = set() |
|
|
|
|
|
|
|
|
|
|
|
def ensure_exists(self, r): |
|
|
|
def ensure_exists(self, r): |
|
|
@ -33,6 +52,10 @@ class DomainAutoCreateMiddleware: |
|
|
|
except Http404: |
|
|
|
except Http404: |
|
|
|
Domain.objects.create(fqdn=r.META.get('HTTP_HOST')) |
|
|
|
Domain.objects.create(fqdn=r.META.get('HTTP_HOST')) |
|
|
|
|
|
|
|
|
|
|
|
def __call__(self, request): |
|
|
|
def _handler(self, req): |
|
|
|
self.ensure_exists(request) |
|
|
|
self.ensure_exists(req) |
|
|
|
return self.get_response(request) |
|
|
|
return self.get_response(req) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def _ahandler(self, req): |
|
|
|
|
|
|
|
self.ensure_exists(req) |
|
|
|
|
|
|
|
return await self.get_response(req) |
|
|
|