fixed asgi.py, added option to serve static files via starlette
This commit is contained in:
parent
99ec96f9f1
commit
d6909ebbd1
2 changed files with 28 additions and 5 deletions
|
@ -1,4 +1,5 @@
|
|||
import os
|
||||
from contextlib import suppress
|
||||
|
||||
from channels.auth import AuthMiddlewareStack
|
||||
from channels.routing import ProtocolTypeRouter, URLRouter
|
||||
|
@ -6,11 +7,30 @@ from django.core.asgi import get_asgi_application
|
|||
|
||||
from c3nav.urls import websocket_urlpatterns
|
||||
|
||||
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings')
|
||||
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "c3nav.settings")
|
||||
django_asgi = get_asgi_application()
|
||||
|
||||
application = ProtocolTypeRouter({
|
||||
"http": get_asgi_application(),
|
||||
"websocket": AuthMiddlewareStack(
|
||||
URLRouter(websocket_urlpatterns)
|
||||
),
|
||||
"http": django_asgi,
|
||||
"websocket": AuthMiddlewareStack(
|
||||
URLRouter(websocket_urlpatterns)
|
||||
),
|
||||
})
|
||||
|
||||
# optional support for static files via starlette
|
||||
with suppress(ImportError):
|
||||
# settings need to be loaded after django init via get_asgi_application
|
||||
from django.conf import settings
|
||||
from starlette.applications import Starlette
|
||||
from starlette.routing import Mount
|
||||
from starlette.staticfiles import StaticFiles
|
||||
|
||||
static_app = ProtocolTypeRouter({
|
||||
"http": Starlette(routes=[
|
||||
Mount(settings.STATIC_URL, app=StaticFiles(directory=settings.STATIC_ROOT), name='static'),
|
||||
Mount('/', app=django_asgi),
|
||||
]),
|
||||
"websocket": AuthMiddlewareStack(
|
||||
URLRouter(websocket_urlpatterns)
|
||||
),
|
||||
})
|
||||
|
|
3
src/requirements/server-asgi.txt
Normal file
3
src/requirements/server-asgi.txt
Normal file
|
@ -0,0 +1,3 @@
|
|||
starlette==0.30.0
|
||||
uvicorn==0.22.0
|
||||
gunicorn==20.1.0
|
Loading…
Add table
Add a link
Reference in a new issue