From dd9abcd38b5ec5b65085611115d6b806a2a7a6df Mon Sep 17 00:00:00 2001 From: Bryon Lewis Date: Fri, 16 Feb 2024 15:07:35 -0500 Subject: [PATCH] adding prod-deployment infrastructure --- bats_ai/settings.py | 1 + client/.env.production | 7 +++--- dev/client.Dockerfile | 29 ++++++++++++++++++++++ docker-compose.prod.yml | 53 +++++++++++++++++++++++++++++++++++++++++ nginx/nginx.conf | 39 ++++++++++++++++++++++++++++++ setup.py | 1 + 6 files changed, 126 insertions(+), 4 deletions(-) create mode 100644 dev/client.Dockerfile create mode 100644 docker-compose.prod.yml create mode 100644 nginx/nginx.conf diff --git a/bats_ai/settings.py b/bats_ai/settings.py index 9764a92..58cd007 100644 --- a/bats_ai/settings.py +++ b/bats_ai/settings.py @@ -14,6 +14,7 @@ CORS_ALLOWED_ORIGINS = [ 'http://localhost:3000', + 'http://localhost', ] diff --git a/client/.env.production b/client/.env.production index edf6415..bee52e4 100644 --- a/client/.env.production +++ b/client/.env.production @@ -1,4 +1,3 @@ -VUE_APP_API_ROOT=https://CHANGEME/api/v1 -VUE_APP_OAUTH_API_ROOT=https://CHANGEME/oauth/ -VUE_APP_OAUTH_CLIENT_ID=CHANGEME -VUE_APP_SENTRY_DSN=CHANGEME +VUE_APP_API_ROOT=http://localhost:8000/api/v1 +VUE_APP_OAUTH_API_ROOT=http://localhost:8000/oauth/ +VUE_APP_OAUTH_CLIENT_ID=HSJWFZ2cIpWQOvNyCXyStV9hiOd7DfWeBOCzo4pP diff --git a/dev/client.Dockerfile b/dev/client.Dockerfile new file mode 100644 index 0000000..e741064 --- /dev/null +++ b/dev/client.Dockerfile @@ -0,0 +1,29 @@ +# Use official Node.js image as the base image +FROM node:16 as build-stage + +# Set working directory +WORKDIR /app + +# Copy package.json and package-lock.json +COPY client/package*.json ./ + +# Install dependencies +RUN npm install + +# Copy the rest of the application +COPY client . + +# Build the Vue.js application +RUN npm run build + +# Production stage +FROM nginx:alpine + +# Copy built app from build stage to nginx +COPY --from=build-stage /app/dist /usr/share/nginx/html + +# Expose port 80 to the outer world +EXPOSE 80 + +# Command to run nginx +CMD ["nginx", "-g", "daemon off;"] diff --git a/docker-compose.prod.yml b/docker-compose.prod.yml new file mode 100644 index 0000000..4c4cf94 --- /dev/null +++ b/docker-compose.prod.yml @@ -0,0 +1,53 @@ +version: '3.8' + +services: + django: + build: + context: . + dockerfile: ./dev/django.Dockerfile + command: 'gunicorn bats_ai.wsgi:application --bind 0.0.0.0:8000' + # ./manage.py runserver 0.0.0.0:8000 --noreload + # entrypoint: ["/bin/bash"] + # command: "" + # Log printing via Rich is enhanced by a TTY + tty: true + env_file: ./dev/.env.docker-compose + volumes: + - .:/opt/django-project + ports: + - 8000:8000 + depends_on: + - postgres + - rabbitmq + - minio + celery: + build: + context: . + dockerfile: ./dev/django.Dockerfile + command: [ + "celery", + "--app", "bats_ai.celery", + "worker", + "--loglevel", "INFO", + "--without-heartbeat" + ] + # Docker Compose does not set the TTY width, which causes Celery errors + tty: false + env_file: ./dev/.env.docker-compose + volumes: + - .:/opt/django-project + depends_on: + - postgres + - rabbitmq + - minio + client: + build: + context: . + dockerfile: ./dev/client.Dockerfile + ports: + - 80:80 + depends_on: + - django +networks: + backend: + driver: bridge \ No newline at end of file diff --git a/nginx/nginx.conf b/nginx/nginx.conf new file mode 100644 index 0000000..0a908aa --- /dev/null +++ b/nginx/nginx.conf @@ -0,0 +1,39 @@ +worker_processes 1; + +events { + worker_connections 1024; +} + +http { + include /etc/nginx/mime.types; + default_type application/octet-stream; + + upstream django { + server django:8000; + } + upstream vue_app { + server vue_app:80; + } + + server { + listen 80; + server_name localhost; + + location /api { + proxy_pass http://django; + proxy_set_header Host $host; + 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; + } + + location / { + proxy_pass http://vue_app; + proxy_set_header Host $host; + 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; + try_files $uri $uri/ /index.html; + } + } +} diff --git a/setup.py b/setup.py index a8deb60..6589de7 100644 --- a/setup.py +++ b/setup.py @@ -37,6 +37,7 @@ include_package_data=True, install_requires=[ 'celery', + 'gunicorn', 'django-ninja', 'django>=4.1, <4.2', 'django-allauth',