This commit is contained in:
2025-12-09 14:46:02 +08:00
parent c7a22a288a
commit abe314fdc8
76 changed files with 7601 additions and 1667 deletions

View File

@@ -8,27 +8,33 @@ https://docs.djangoproject.com/en/5.2/howto/deployment/asgi/
"""
import os
from django.core.asgi import get_asgi_application
from django.conf import settings
from channels.routing import ProtocolTypeRouter, URLRouter
from channels.auth import AuthMiddlewareStack
from channels.security.websocket import AllowedHostsOriginValidator
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'hertz_server_django.settings')
# Import Django first to ensure proper initialization
from django.core.asgi import get_asgi_application
# Initialize Django ASGI application early to ensure the AppRegistry
# is populated before importing code that may import ORM models.
django_asgi_app = get_asgi_application()
# Import other modules AFTER Django setup
from django.conf import settings
from channels.routing import ProtocolTypeRouter, URLRouter
from channels.auth import AuthMiddlewareStack
from channels.security.websocket import AllowedHostsOriginValidator
# Import websocket routing AFTER Django setup to avoid AppRegistryNotReady
from hertz_demo import routing as demo_routing
from hertz_studio_django_yolo import routing as yolo_routing
# Merge websocket routes
websocket_urlpatterns = (
demo_routing.websocket_urlpatterns +
yolo_routing.websocket_urlpatterns
)
if 'hertz_studio_django_yolo' in settings.INSTALLED_APPS:
from hertz_studio_django_yolo import routing as yolo_routing
websocket_urlpatterns = (
demo_routing.websocket_urlpatterns +
yolo_routing.websocket_urlpatterns
)
else:
websocket_urlpatterns = demo_routing.websocket_urlpatterns
# 在开发环境下放宽Origin校验便于第三方客户端如 Apifox、wscat调试
websocket_app = AuthMiddlewareStack(
@@ -47,3 +53,4 @@ else:
"http": django_asgi_app,
"websocket": AllowedHostsOriginValidator(websocket_app),
})