Skip to content

Commit

Permalink
feat(deployement): production environment
Browse files Browse the repository at this point in the history
  • Loading branch information
Topvennie committed Mar 11, 2024
1 parent c644f8c commit eace686
Show file tree
Hide file tree
Showing 17 changed files with 258 additions and 104 deletions.
20 changes: 20 additions & 0 deletions .dev.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
PUID=1000
PGID=1000
TZ=Europe/Brussels

DATADIR=./data

BACKEND_DIR=./backend

FRONTEND_DIR=./frontend

REDIS_IP=192.168.90.10
REDIS_PORT=6379

DJANGO_SECRET_KEY=
DJANGO_DEBUG=True
DJANGO_DB_ENGINE=django.db.backends.sqlite3
DJANGO_DB_NAME=${BACKEND_DIR}/db.sqlite3
DJANGO_REDIS_HOST=${REDIS_IP}
DJANGO_REDIS_PORT=${REDIS_PORT}
DJANGO_REDIS_PASSWORD=${REDIS_PASSWORD}
13 changes: 0 additions & 13 deletions .env

This file was deleted.

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.tool-versions
.env
data/*

!data/nginx/nginx.conf
Expand Down
30 changes: 30 additions & 0 deletions .prod.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
PUID=1000
PGID=1000
TZ=Europe/Brussels

DATADIR=./data

BACKEND_DIR=./backend

FRONTEND_DIR=./frontend

POSTGRES_IP=192.168.90.9
POSTGRES_PORT=5432
POSTGRES_DB=selab
POSTGRES_USER=selab_user
POSTGRES_PASSWORD=

REDIS_IP=192.168.90.10
REDIS_PORT=6379

DJANGO_SECRET_KEY=
DJANGO_DEBUG=false
DJANGO_DB_ENGINE=django.db.backends.postgresql
DJANGO_DB_NAME=${POSTGRES_DB}
DJANGO_DB_USER=${POSTGRES_USER}
DJANGO_DB_PASSWORD=${POSTGRES_PASSWORD}
DJANGO_DB_HOST=${POSTGRES_IP}
DJANGO_DB_PORT=${POSTGRES_PORT}
DJANGO_REDIS_HOST=${REDIS_IP}
DJANGO_REDIS_PORT=${REDIS_PORT}
DJANGO_REDIS_PASSWORD=${REDIS_PASSWORD}
File renamed without changes.
15 changes: 15 additions & 0 deletions backend/Dockerfile.prod
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
FROM python:3.11.4

RUN apt update && apt install -y gettext libgettextpo-dev && pip install --upgrade pip

WORKDIR /code

ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1

COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

COPY . .

RUN ./setup.sh
4 changes: 4 additions & 0 deletions backend/gunicorn_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
workers = 4
bind = "0.0.0.0:8080"
chdir = "/code/"
module = "ypovoli.wsgi:application"
3 changes: 2 additions & 1 deletion backend/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ cas-client==1.0.0
psycopg2-binary==2.9.9
djangorestframework-simplejwt==5.3.1
celery[redis]==5.3.6
django-redis==5.4.0
django-redis==5.4.0
gunicorn==21.2.0
33 changes: 16 additions & 17 deletions backend/ypovoli/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
https://docs.djangoproject.com/en/5.0/ref/settings/
"""

import os
from datetime import timedelta
from os import environ
from pathlib import Path
import os

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
Expand All @@ -26,10 +26,10 @@
# See https://docs.djangoproject.com/en/5.0/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = "django-insecure-_upw+)mo--8_0slsl&8ot0*h8p50z_rlid6nwobd*%%gm$_!1x"
SECRET_KEY = environ.get("DJANGO_SECRET_KEY", "lnZZ2xHc6HjU5D85GDE3Nnu4CJsBnm")

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
DEBUG = environ.get("DJANGO_DEBUG", False)
ALLOWED_HOSTS = []


Expand Down Expand Up @@ -89,17 +89,20 @@
# Application endpoints

CAS_ENDPOINT = "https://login.ugent.be"
CAS_RESPONSE = "https://localhost:8080/auth/cas/echo"
API_ENDPOINT = "https://localhost:8080"
CAS_RESPONSE = "https://localhost:8080/api/auth/cas/echo"
API_ENDPOINT = "https://localhost:8080/api"

# Database
# https://docs.djangoproject.com/en/5.0/ref/settings/#databases
DATABASES = {
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": BASE_DIR / "db.sqlite3",
"ENGINE": environ.get("DJANGO_DB_ENGINE", "django.db.backends.sqlite3"),
"NAME": environ.get("DJANGO_DB_NAME", BASE_DIR / "db.sqlite3"),
"USER": environ.get("DJANGO_DB_USER", ""),
"PASSWORD": environ.get("DJANGO_DB_PASSWORD", ""),
"HOST": environ.get("DJANGO_DB_HOST", ""),
"PORT": environ.get("DJANGO_DB_PORT", ""),
},
"production": {"ENGINE": "django.db.backends.postgresql"},
}

# Default primary key field type
Expand Down Expand Up @@ -145,25 +148,21 @@
}

REDIS_CUSTOM = {
"host": environ.get("REDIS_IP", "localhost"),
"port": environ.get("REDIS_PORT", 6379),
"password": environ.get("REDIS_PASSWORD", ""),
"host": environ.get("DJANGO_REDIS_HOST", "localhost"),
"port": environ.get("DJANGO_REDIS_PORT", 6379),
"db_django": 0,
"db_celery": 1,
}

CACHES = {
"default": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": f"redis://:{REDIS_CUSTOM['password']}@{REDIS_CUSTOM['host']}:{REDIS_CUSTOM['port']}/"
f"{REDIS_CUSTOM['db_django']}",
"LOCATION": f"redis://@{REDIS_CUSTOM['host']}:{REDIS_CUSTOM['port']}/{REDIS_CUSTOM['db_django']}",
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient",
},
}
}

CELERY_BROKER_URL = (
f"redis://:{REDIS_CUSTOM['password']}@{REDIS_CUSTOM['host']}:{REDIS_CUSTOM['port']}/"
f"{REDIS_CUSTOM['db_celery']}"
)
CELERY_BROKER_URL = f"redis://@{REDIS_CUSTOM['host']}:{REDIS_CUSTOM['port']}/{REDIS_CUSTOM['db_celery']}"
CELERY_RESULT_BACKEND = f"redis://@{REDIS_CUSTOM['host']}:{REDIS_CUSTOM['port']}/{REDIS_CUSTOM['db_celery']}"
43 changes: 28 additions & 15 deletions backend/ypovoli/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""

from django.urls import include, path
from django.urls import include, path, re_path
from drf_yasg import openapi
from drf_yasg.views import get_schema_view
from rest_framework import permissions
Expand All @@ -33,18 +33,31 @@


urlpatterns = [
# Base API endpoints.
path("", include("api.urls")),
# Authentication endpoints.
path("auth/", include("authentication.urls")),
path("notifications/", include("notifications.urls"), name="notifications"),
# Swagger documentation.
path(
"swagger/",
schema_view.with_ui("swagger", cache_timeout=0),
name="schema-swagger-ui",
),
path(
"swagger<format>/", schema_view.without_ui(cache_timeout=0), name="schema-json"
),
re_path(
"api/",
include(
[
# Base API endpoints.
path("", include("api.urls")),
# Authentication endpoints.
path("auth/", include("authentication.urls")),
path(
"notifications/",
include("notifications.urls"),
name="notifications",
),
# Swagger documentation.
path(
"swagger/",
schema_view.with_ui("swagger", cache_timeout=0),
name="schema-swagger-ui",
),
path(
"swagger<format>/",
schema_view.without_ui(cache_timeout=0),
name="schema-json",
),
]
),
)
]
52 changes: 0 additions & 52 deletions data/nginx/nginx.conf

This file was deleted.

5 changes: 5 additions & 0 deletions development.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
if ! [ -f .env ]; then
echo "Error: .env file does not exist."
exit 1
fi

echo "Checking for existing SSL certificates..."

if [ ! -f "data/nginx/ssl/private.key" ] || [ ! -f "data/nginx/ssl/certificate.crt" ]; then
Expand Down
12 changes: 6 additions & 6 deletions development.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ services:
- 443:443
- 8080:8080
volumes:
- $DATADIR/nginx/nginx.conf:/etc/nginx/nginx.conf:ro
- $DATADIR/nginx/nginx.dev.conf:/etc/nginx/nginx.conf:ro
- $DATADIR/nginx/ssl:/etc/nginx/ssl:ro
depends_on:
- backend
Expand All @@ -49,10 +49,10 @@ services:
container_name: backend
build:
context: $BACKEND_DIR
dockerfile: Dockerfile
dockerfile: Dockerfile.dev
command: bash -c "./setup.sh && python manage.py runsslserver 192.168.90.2:8080"
expose:
- 8000
- 8080
volumes:
- $BACKEND_DIR:/code

Expand All @@ -61,8 +61,8 @@ services:
container_name: celery
build:
context: $BACKEND_DIR
dockerfile: Dockerfile
command: celery -A ypovoli worker -l INFO
dockerfile: Dockerfile.dev
command: celery -A ypovoli worker -l DEBUG
volumes:
- $BACKEND_DIR:/code
depends_on:
Expand All @@ -74,7 +74,7 @@ services:
container_name: frontend
build:
context: $FRONTEND_DIR
dockerfile: Dockerfile
dockerfile: Dockerfile.dev
command: bash -c "npm install && npm run host"
expose:
- 5173
Expand Down
File renamed without changes.
12 changes: 12 additions & 0 deletions frontend/Dockerfile.prod
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
FROM node:16 as build-stage
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY ./ .
RUN npm run build

FROM nginx as production-stage
EXPOSE 3000
RUN mkdir /app
COPY nginx.conf /etc/nginx/conf.d/default.conf
COPY --from=build-stage /app/dist /app
9 changes: 9 additions & 0 deletions frontend/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
server {
listen 3000;

location / {
root /app;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
}
Loading

0 comments on commit eace686

Please sign in to comment.