diff --git a/.env.example b/.env.example index 390dad84..f2e536de 100644 --- a/.env.example +++ b/.env.example @@ -1,12 +1,15 @@ -SECRET_KEY=djangosecretkey -CLIENT_ID=407408718192.apps.googleusercontent.com -APP_ID=facebookoauthid -APP_SECRET=facebookoauthsecret -DOWNLOAD=passwordtodownloadleaderboard -DB_NAME=df -DB_USER=blahaja -DB_PASSWORD=notmypass -DB_HOST=localhost -DB_PORT=5432 -DEBUG=True -GOOGLE_MAPS_API_KEY= \ No newline at end of file +SECRET_KEY= +CLIENT_ID= +APP_ID= +APP_SECRET= +DOWNLOAD= +DB_NAME= +DB_USER= +DB_PASSWORD= +DB_HOST= +DB_PORT= +DEBUG= +GOOGLE_MAPS_API_KEY= +POSTGRES_USER= +POSTGRES_PASSWORD= +POSTGRES_DB= \ No newline at end of file diff --git a/Digital_Fortress_Backend/settings.py b/Digital_Fortress_Backend/settings.py index 8ae05e38..4e3c1742 100644 --- a/Digital_Fortress_Backend/settings.py +++ b/Digital_Fortress_Backend/settings.py @@ -109,7 +109,7 @@ 'NAME': config('DB_NAME'), 'USER': config('DB_USER'), 'PASSWORD': config('DB_PASSWORD'), - 'HOST': 'localhost', + 'HOST': config('DB_HOST'), 'PORT': '', } } diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..4852762e --- /dev/null +++ b/Dockerfile @@ -0,0 +1,12 @@ +FROM python:3.7-slim-buster + +RUN pip install --upgrade pip + +COPY ./requirements.txt . +RUN pip install -r requirements.txt + +COPY . . + +# WORKDIR /app + +ENTRYPOINT [ "sh", "entrypoint.sh" ] \ No newline at end of file diff --git a/README.md b/README.md index 77186331..d43c0f12 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,49 @@ This is the backend repository built on Django framework and utilises Django Res 2. Then, apply the migrations using `python manange.py migrate` 3. Run the server using `python manage.py runserver --settings=Digital_Fortress_Backend.dev_settings` + +## How to deploy + +### Build the docker containers + +1. Clone the project + +```bash + git clone https://github.com/lugnitdgp/Digital_Fortress_Backend.git +``` + +2. Install docker + + + +3. Set-up environment variables + +```bash + cp .env.example .env +``` + +4. Spin-up the docker conatainers + +```bash + sudo docker-compose up -d --build +``` +5. Create admin + +```bash + sudo docker exec -it [django_container_name] python manage.py createsuperuser +``` + +### Stopping the conatainers + +```bash + sudo docker-compose stop +``` + +### Removing the volumes and the container +```bash + sudo docker-compose down -v +``` + ### Status Codes: 200 : Success diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 00000000..eb6ed4af --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,35 @@ +version: '3.8' + +services: + django_gunicorn: + volumes: + - static:/static + env_file: + - .env + build: + context: . + ports: + - "8000:8000" + depends_on: + - db + + db: + image: postgres:13.0-alpine + volumes: + - db_data:/var/lib/postgresql/data/ + env_file: + - .env + + nginx: + build: ./nginx + volumes: + - static:/static + ports: + - "80:80" + depends_on: + - db + - django_gunicorn + +volumes: + db_data: + static: \ No newline at end of file diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100644 index 00000000..07c5c9e4 --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,7 @@ +#!/bin/sh + +python manage.py makemigrations --no-input +python manage.py migrate --no-input +python manage.py collectstatic --no-input + +gunicorn Digital_Fortress_Backend.wsgi:application --bind 0.0.0.0:8000 \ No newline at end of file diff --git a/nginx/Dockerfile b/nginx/Dockerfile new file mode 100644 index 00000000..ec370358 --- /dev/null +++ b/nginx/Dockerfile @@ -0,0 +1,3 @@ +FROM nginx:1.19.0-alpine + +COPY ./default.conf /etc/nginx/conf.d/default.conf \ No newline at end of file diff --git a/nginx/default.conf b/nginx/default.conf new file mode 100644 index 00000000..517bb9f6 --- /dev/null +++ b/nginx/default.conf @@ -0,0 +1,20 @@ +upstream django_gunicorn { + server django_gunicorn:8000; +} + +server { + listen 80; + server_name dfapi.nitdgplug.org; + + location /static/ { + alias /static/; + } + location / { + proxy_set_header Host $http_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; + proxy_pass http://django_gunicorn; + } + +} diff --git a/requirements.txt b/requirements.txt index f2ae9919..09b995df 100644 --- a/requirements.txt +++ b/requirements.txt @@ -27,3 +27,4 @@ six==1.12.0 sqlparse==0.3.0 toml==0.10.0 urllib3==1.25.3 +gunicorn==20.0.4 \ No newline at end of file