"""
ASGI config for vuln_demo project.

It exposes the ASGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/5.0/howto/deployment/asgi/
"""

# The imports here aren't on top level because they need to be executed after setting the DJANGO_SETTINGS_MODULE
# ruff: noqa: E402

import os

import django

if os.environ.get("VULN_DEMO_SKIP_ASGI_SETUP", "0") != "1":
    # This needs to happen before importing anything from Django
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "vuln_demo.settings")

    # This is necessary to configure Django (sets up logging, populates the app registry, etc.)
    # Without this, we might run into: django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
    django.setup()

from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
from channels.security.websocket import AllowedHostsOriginValidator
from django.core.asgi import get_asgi_application

from vuln_demo.apps.vulnerabilities.routing import websocket_urlpatterns

application = ProtocolTypeRouter(
    {
        "http": get_asgi_application(),
        "websocket": AllowedHostsOriginValidator(AuthMiddlewareStack(URLRouter(websocket_urlpatterns))),
    }
)
