Skip to content

Commit

Permalink
Merge pull request #189 from lvalics/main
Browse files Browse the repository at this point in the history
CORS fixed and started to add APP_URL to replace in NGINX automatically the domain.
  • Loading branch information
davidsmithdevops committed Oct 27, 2023
2 parents eb686e2 + 0366903 commit cd183d2
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 1 deletion.
7 changes: 7 additions & 0 deletions dj_backend_server/Dockerfile.nginx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Use the official Nginx image
FROM nginx

COPY ./nginx/nginx.conf /etc/nginx/nginx.conf.template
COPY ./entrypoint-nginx.sh /entrypoint-nginx.sh
RUN chmod +x /entrypoint-nginx.sh
ENTRYPOINT ["/entrypoint-nginx.sh"]
26 changes: 26 additions & 0 deletions dj_backend_server/api/middleware/cors_middleware.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from django.utils.deprecation import MiddlewareMixin
from web.models.chatbot import Chatbot

class CorsMiddleware(MiddlewareMixin):
def process_response(self, request, response):
# Get the origin of the request
origin = request.META.get('HTTP_ORIGIN')

# Check if the origin is in the database
origin_in_db = Chatbot.objects.filter(website=origin).exists()
# print(f"Origin of the request: {origin}")
# print(f"Is the origin in the database: {origin_in_db}")

if origin_in_db:
# Add the 'Access-Control-Allow-Origin' header to the response
response['Access-Control-Allow-Origin'] = origin
response['Access-Control-Allow-Methods'] = 'GET, POST, OPTIONS'
response['Access-Control-Allow-Headers'] = 'X-Requested-With, Content-Type, X-Bot-Token'

# print(f"Website URLs checked: {[chatbot.website for chatbot in Chatbot.objects.all()]}")
# print(f"Response status code: {response.status_code}")
# print(f"Response content: {response.content}")
# print(f"Response headers: {response.headers}")


return response
1 change: 1 addition & 0 deletions dj_backend_server/dj_backend_server/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
MIDDLEWARE = [
'django.middleware.locale.LocaleMiddleware',
'django.middleware.security.SecurityMiddleware',
'api.middleware.cors_middleware.CorsMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
Expand Down
6 changes: 6 additions & 0 deletions dj_backend_server/docker-compose.linux.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ services:
nginx:
image: nginx
container_name: oc_nginx
build:
context: .
dockerfile: Dockerfile.nginx
restart: unless-stopped
ports:
- "80:80"
Expand All @@ -46,6 +49,9 @@ services:
- ./static:/app/web/static/
networks:
- openchat_network
env_file:
- .env.docker
#entrypoint: ["/entrypoint-nginx.sh"]
depends_on:
- qdrant
- mysql
Expand Down
16 changes: 16 additions & 0 deletions dj_backend_server/entrypoint-nginx.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash

# Remove 'http://' or 'https://' prefix from APP_URL
CLEANED_APP_URL=${APP_URL#http://}
CLEANED_APP_URL=${APP_URL#https://}

echo "Replacing APP_URL with $CLEANED_APP_URL"

# Define the file path as a variable, for example:
NGINX_CONF="/etc/nginx/nginx.conf"

sed "s|yourdomain.com|$CLEANED_APP_URL|g" NGINX_CONF > /tmp/nginx.conf
mv /tmp/nginx.conf NGINX_CONF

# Start your app normally
# exec nginx -g "daemon off;"
2 changes: 1 addition & 1 deletion dj_backend_server/nginx/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ http {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme; # Forward the original scheme (HTTP or HTTPS)
proxy_set_header Origin ""; # Optionally forward the Origin header
proxy_set_header Origin $http_origin; # Optionally forward the Origin header
proxy_ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
add_header Cache-Control "public, max-age=2592000";
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always; # HSTS header
Expand Down

0 comments on commit cd183d2

Please sign in to comment.