-
Notifications
You must be signed in to change notification settings - Fork 643
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #189 from lvalics/main
CORS fixed and started to add APP_URL to replace in NGINX automatically the domain.
- Loading branch information
Showing
6 changed files
with
57 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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;" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters