From 498aa1137bfc57c0d6ac723d4a337237fe34aa41 Mon Sep 17 00:00:00 2001
From: Eliot Berriot <contact@eliotberriot.com>
Date: Sun, 25 Feb 2018 13:05:01 +0100
Subject: [PATCH] Initial channels integration

---
 api/config/settings/common.py | 14 +++++++++++++-
 api/requirements/base.txt     |  2 ++
 docker/nginx/conf.dev         |  8 ++++++++
 front/config/index.js         |  1 +
 front/package.json            |  1 +
 5 files changed, 25 insertions(+), 1 deletion(-)

diff --git a/api/config/settings/common.py b/api/config/settings/common.py
index f5ddec00..e5389756 100644
--- a/api/config/settings/common.py
+++ b/api/config/settings/common.py
@@ -30,6 +30,7 @@ ALLOWED_HOSTS = env.list('DJANGO_ALLOWED_HOSTS')
 # APP CONFIGURATION
 # ------------------------------------------------------------------------------
 DJANGO_APPS = (
+    'channels',
     # Default Django apps:
     'django.contrib.auth',
     'django.contrib.contenttypes',
@@ -253,9 +254,9 @@ MEDIA_URL = env("MEDIA_URL", default='/media/')
 # URL Configuration
 # ------------------------------------------------------------------------------
 ROOT_URLCONF = 'config.urls'
-
 # See: https://docs.djangoproject.com/en/dev/ref/settings/#wsgi-application
 WSGI_APPLICATION = 'config.wsgi.application'
+ASGI_APPLICATION = "config.routing.application"
 
 # AUTHENTICATION CONFIGURATION
 # ------------------------------------------------------------------------------
@@ -284,6 +285,17 @@ CACHES = {
 }
 
 CACHES["default"]["BACKEND"] = "django_redis.cache.RedisCache"
+from urllib.parse import urlparse
+cache_url = urlparse(CACHES['default']['LOCATION'])
+CHANNEL_LAYERS = {
+    "default": {
+        "BACKEND": "channels_redis.core.RedisChannelLayer",
+        "CONFIG": {
+            "hosts": [(cache_url.hostname, cache_url.port)],
+        },
+    },
+}
+
 CACHES["default"]["OPTIONS"] = {
     "CLIENT_CLASS": "django_redis.client.DefaultClient",
     "IGNORE_EXCEPTIONS": True,  # mimics memcache behavior.
diff --git a/api/requirements/base.txt b/api/requirements/base.txt
index 133fcc0c..d402d359 100644
--- a/api/requirements/base.txt
+++ b/api/requirements/base.txt
@@ -59,3 +59,5 @@ pyacoustid>=1.1.5,<1.2
 raven>=6.5,<7
 python-magic==0.4.15
 ffmpeg-python==0.1.10
+channels>=2,<2.1
+channels_redis>=2.1,<2.2
diff --git a/docker/nginx/conf.dev b/docker/nginx/conf.dev
index 29c04fc6..9847c2dc 100644
--- a/docker/nginx/conf.dev
+++ b/docker/nginx/conf.dev
@@ -28,6 +28,11 @@ http {
     #gzip  on;
     proxy_cache_path /tmp/funkwhale-transcode levels=1:2 keys_zone=transcode:10m max_size=1g inactive=24h use_temp_path=off;
 
+    map $http_upgrade $connection_upgrade {
+        default upgrade;
+        ''      close;
+    }
+
     server {
         listen 6001;
         charset     utf-8;
@@ -40,6 +45,9 @@ http {
         proxy_set_header X-Forwarded-Proto $scheme;
         proxy_set_header X-Forwarded-Host   localhost:8080;
         proxy_set_header X-Forwarded-Port   8080;
+        proxy_http_version 1.1;
+        proxy_set_header Upgrade $http_upgrade;
+        proxy_set_header Connection $connection_upgrade;
         proxy_redirect off;
 
         location /_protected/media {
diff --git a/front/config/index.js b/front/config/index.js
index 7ce6e26e..14cbe3e4 100644
--- a/front/config/index.js
+++ b/front/config/index.js
@@ -32,6 +32,7 @@ module.exports = {
       '/api': {
         target: 'http://nginx:6001',
         changeOrigin: true,
+        ws: true
       },
       '/media': {
         target: 'http://nginx:6001',
diff --git a/front/package.json b/front/package.json
index d6bdb8c5..201694e4 100644
--- a/front/package.json
+++ b/front/package.json
@@ -17,6 +17,7 @@
   "dependencies": {
     "axios": "^0.17.1",
     "dateformat": "^2.0.0",
+    "django-channels": "^1.1.6",
     "js-logger": "^1.3.0",
     "jwt-decode": "^2.2.0",
     "lodash": "^4.17.4",
-- 
GitLab