Source code for ferrosoft.apps.ferrobase.middleware.appmeta

from asgiref.sync import iscoroutinefunction, markcoroutinefunction
from django.conf import settings

from ferrosoft.apps.ferrobase.apps import PLATFORM_APPS, AppMeta


[docs] class AppMetaMiddleware: """ Set APP_META dict in request object. This dict includes information about the application: NAME and ICON_PATH. The app is determined by HTTP Host header, allowing different branding per app. """ async_capable = True sync_capable = False # See https://www.rfc-editor.org/rfc/rfc3986.html#section-3.2.2 # # "URI producers should use names that conform to the DNS syntax, # even when use of DNS is not immediately apparent, and should # limit these names to no more than 255 characters in length." HOST_NAME_MAX_LENGTH = 255 SETTINGS_KEY = "FERROBASE_APP_HOSTS" def __init__(self, get_response): self.get_response = get_response if iscoroutinefunction(self.get_response): markcoroutinefunction(self) async def __call__(self, request): self._patch_request(request) return await self.get_response(request) def _patch_request(self, request): app_hosts = getattr(settings, self.SETTINGS_KEY, {}) http_host = request.headers.get("host", "") http_host = http_host[: min(len(http_host), self.HOST_NAME_MAX_LENGTH)] meta = PLATFORM_APPS.get(app_hosts.get(http_host, "ferrobase"), None) if isinstance(meta, AppMeta): request.APP_META = meta