0.3.4
chat v3
This commit is contained in:
22
TWB/asgi.py
22
TWB/asgi.py
@@ -13,14 +13,32 @@ from django.core.asgi import get_asgi_application
|
||||
from channels.routing import ProtocolTypeRouter, URLRouter
|
||||
from channels.auth import AuthMiddlewareStack
|
||||
from ChatServiceApp.websocket_urls import websocket_urlpatterns
|
||||
from channels.security.websocket import AllowedHostsOriginValidator
|
||||
from channels.sessions import SessionMiddlewareStack
|
||||
from .ws_middleware import QueryAuthMiddleware
|
||||
|
||||
|
||||
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'TWB.settings')
|
||||
|
||||
application = ProtocolTypeRouter({
|
||||
'http': get_asgi_application(),
|
||||
'websocket': AuthMiddlewareStack(
|
||||
"websocket": AuthMiddlewareStack(
|
||||
URLRouter(
|
||||
websocket_urlpatterns
|
||||
)
|
||||
)
|
||||
),
|
||||
# 'websocket': AuthMiddlewareStack(
|
||||
# URLRouter(
|
||||
# websocket_urlpatterns
|
||||
# )
|
||||
# )
|
||||
# "websocket": AllowedHostsOriginValidator(
|
||||
# SessionMiddlewareStack(
|
||||
# AuthMiddlewareStack(
|
||||
# URLRouter(
|
||||
# websocket_urlpatterns
|
||||
# )
|
||||
# )
|
||||
# )
|
||||
# ),
|
||||
})
|
||||
|
||||
8
TWB/run_daphne.py
Normal file
8
TWB/run_daphne.py
Normal file
@@ -0,0 +1,8 @@
|
||||
import sys
|
||||
|
||||
if __name__ == '__main__':
|
||||
# insert here whatever commands you use to run daphne
|
||||
sys.argv = ['daphne', 'TWB.asgi:application']
|
||||
from daphne.cli import CommandLineInterface
|
||||
|
||||
CommandLineInterface.entrypoint()
|
||||
@@ -33,7 +33,9 @@ ALLOWED_HOSTS = ["*"]
|
||||
# Application definition
|
||||
|
||||
INSTALLED_APPS = [
|
||||
'channels',
|
||||
# 'channels',
|
||||
"daphne",
|
||||
'ChatServiceApp',
|
||||
'modeltranslation',
|
||||
|
||||
'django.contrib.admin',
|
||||
@@ -51,7 +53,7 @@ INSTALLED_APPS = [
|
||||
'AuthApp',
|
||||
'RoutesApp',
|
||||
'ReferenceDataApp',
|
||||
'ChatServiceApp',
|
||||
|
||||
]
|
||||
|
||||
MIDDLEWARE = [
|
||||
@@ -86,12 +88,16 @@ TEMPLATES = [
|
||||
},
|
||||
]
|
||||
|
||||
# WSGI_APPLICATION = 'TWB.wsgi.application'
|
||||
WSGI_APPLICATION = 'TWB.wsgi.application'
|
||||
ASGI_APPLICATION = 'TWB.asgi.application'
|
||||
|
||||
CHANNEL_LAYERS = {
|
||||
'default': {
|
||||
'BACKEND': 'channels.layers.InMemoryChannelLayer'
|
||||
# 'BACKEND': 'channels.layers.InMemoryChannelLayer'
|
||||
"BACKEND": "channels_redis.core.RedisChannelLayer",
|
||||
"CONFIG": {
|
||||
"hosts": [("127.0.0.1", 6379)],
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
26
TWB/ws_middleware.py
Normal file
26
TWB/ws_middleware.py
Normal file
@@ -0,0 +1,26 @@
|
||||
from channels.db import database_sync_to_async
|
||||
|
||||
@database_sync_to_async
|
||||
def get_user(user_id):
|
||||
from django.contrib.auth.models import User, AnonymousUser
|
||||
try:
|
||||
return User.objects.get(id=user_id)
|
||||
except User.DoesNotExist:
|
||||
return AnonymousUser()
|
||||
|
||||
class QueryAuthMiddleware:
|
||||
"""
|
||||
Custom middleware (insecure) that takes user IDs from the query string.
|
||||
"""
|
||||
|
||||
def __init__(self, app):
|
||||
# Store the ASGI application we were passed
|
||||
self.app = app
|
||||
|
||||
async def __call__(self, scope, receive, send):
|
||||
# Look up user from query string (you should also do things like
|
||||
# checking if it is a valid user ID, or if scope["user"] is already
|
||||
# populated).
|
||||
scope['user'] = await get_user(int(scope["query_string"]))
|
||||
|
||||
return await self.app(scope, receive, send)
|
||||
Reference in New Issue
Block a user