Skip to content

Commit

Permalink
added daphne and websocket
Browse files Browse the repository at this point in the history
  • Loading branch information
walosha committed Jan 29, 2023
1 parent ce27961 commit 5bdd6c6
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 3 deletions.
2 changes: 1 addition & 1 deletion church/core/asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"""

import os

# from channels.security.websocket import AllowedHostsOriginValidator
from django.core.asgi import get_asgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'core.settings')
Expand Down
24 changes: 24 additions & 0 deletions church/core/middleware.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from django.urls import reverse
from django.shortcuts import get_object_or_404, redirect
from .models import Course


def subdomain_course_middleware(get_response):
"""
Subdomains for courses
"""
def middleware(request):
host_parts = request.get_host().split('.')
if len(host_parts) > 2 and host_parts[0] != 'www':
# get course for the given subdomain
course = get_object_or_404(Course, slug=host_parts[0])
course_url = reverse('course_detail',
args=[course.slug])
# redirect current request to the course_detail view
url = '{}://{}{}'.format(request.scheme,
'.'.join(host_parts[1:]),
course_url)
return redirect(url)
response = get_response(request)
return response
return middleware
1 change: 1 addition & 0 deletions church/daphne.sock.lock
18 changes: 16 additions & 2 deletions config/nginx/default.conf.template
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@ upstream uwsgi_app {

}

server {
# upstream for Daphne
upstream daphne {
server unix:/code/church/daphne.sock;
}

server {
listen 80;
server_name www.educaproject.com educaproject.com;
server_name www.church.com church.com;
return 301 https://$host$request_uri;
}
server {
Expand All @@ -22,6 +27,15 @@ upstream uwsgi_app {
include /etc/nginx/uwsgi_params;
uwsgi_pass uwsgi_app;
}

location /ws/ {
proxy_http_version
1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_redirect off;
proxy_pass http://daphne;
}

location /static/ {
alias /code/church/static/;
Expand Down
21 changes: 21 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,26 @@ services:
depends_on:
- pg1

daphne:
build: .
working_dir: /code/church/
command:
[
"../wait-for-it.sh",
"pg1:5432",
"--",
"daphne",
"-u",
"/code/church/daphne.sock",
"core.asgi:application",
]
restart: always
volumes:
- .:/code
environment:
- DJANGO_SETTINGS_MODULE=core.settings.prod
depends_on:
- pg1
- cache
volumes:
pg1_data:
3 changes: 3 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ openssl req -x509 -newkey rsa:2048 -sha256 -days 3650 -nodes \
-subj '/CN=_.church.com' \
-addext 'subjectAltName=DNS:_.church.com'

//reload NGNIX
docker compose exec nginx nginx -s reload

`

## Features (Content Mgt System)
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ psycopg2==2.9.3
uwsgi==2.0.20
daphne==3.0.2
uwsgi==2.0.20
python-dateutil==1.4

0 comments on commit 5bdd6c6

Please sign in to comment.