Skip to content

Commit

Permalink
Merge pull request #122 from TaskMasterErnest/main
Browse files Browse the repository at this point in the history
Local Testing and Development Improvements, and Addressing Test Failures
  • Loading branch information
chukaibejih authored Oct 28, 2023
2 parents b2515dc + 89a6f95 commit 92fd0bd
Show file tree
Hide file tree
Showing 11 changed files with 184 additions and 13 deletions.
9 changes: 9 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
*.log
*.pot
*.pyc
.env*
# __pycache__
db.sqlite3
venv
virtualenv
.idea
30 changes: 24 additions & 6 deletions .env.templates
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,31 @@ SECRET_KEY =


# Development environment
DB_USER =
DB_PASSWORD =
DB_HOST =
DB_PORT =
DB_NAME =
POSTGRES_USER =
POSTGRES_PASSWORD =
POSTGRES_HOST =
POSTGRES_PORT =
POSTGRES_DB =

REDIS_HOST =
REDIS_PORT =

SIGNING_KEY =


STRIPE_SECRET =
STRIPE_SECRET =

########### SAMPLE ENV-VAR SETTING ###########
# DJANGO_SUPERUSER_USERNAME=admin
# DJANGO_SUPERUSER_PASSWORD=superpasswd
# DJANGO_SUPERUSER_EMAIL=hello@smartlearn.com
# SECRET_KEY=""

# POSTGRES_DB=postgresDB
# POSTGRES_PASSWORD=mysecret1@Passwd
# POSTGRES_USER=myuser
# POSTGRES_HOST=postgres_db
# POSTGRES_PORT=5432

# REDIS_HOST=localhost
# REDIS_PORT=6379
57 changes: 57 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
name: Django CI - Test Phase

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

jobs:
build:
runs-on: ubuntu-latest

env:
DJANGO_SUPERUSER_EMAIL: hello@smartlearn.com
SECRET_KEY: ""
POSTGRES_DB: dockerdc
POSTGRES_PASSWORD: mysecret1@Passwd
POSTGRES_USER: postgres
POSTGRES_HOST: localhost
POSTGRES_PORT: 5432

strategy:
max-parallel: 4
matrix:
python-version: [3.9]

services:
postgres_db:
image: postgres:13
env:
POSTGRES_DB: ${{ env.POSTGRES_DB }}
POSTGRES_PASSWORD: ${{ env.POSTGRES_PASSWORD }}
POSTGRES_USER: ${{ env.POSTGRES_USER }}
POSTGRES_HOST: ${{ env.POSTGRES_HOST }}
POSTGRES_PORT: 5432
# POSTGRES_HOST_AUTH_METHOD: trust
ports:
- 5432:5432
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python-version }}
- name: Install Dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Run Tests
run: |
python manage.py test
18 changes: 18 additions & 0 deletions Dockerfile.dev
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
FROM python:3.11-slim

WORKDIR /api

COPY . .

# create a virtual env for the python application
# this goes into the /opt dir
RUN python3 -m venv /opt/venv

# install requirements and run the entrypoint script
RUN /opt/venv/bin/pip install -r requirements.txt && \
chmod +x entrypoint.sh

EXPOSE 8080

# Start the application
CMD ["/api/entrypoint.sh"]
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,17 @@ To run the API locally, follow these steps:
3. Activate the virtual environment: `source venv/bin/activate`
4. Install dependencies: `pip install -r requirements.txt`
5. Go to .settings.DATABASES section, deactivate #PRODUCTION mode and activate #Development mode, add PostgreSQL configuration to connect to your database to be the default database.
6. Change `.env.templates` to .env and setup you environment variables.
6. Change `.env.templates` to .env and setup your environment variables.
7. Set up the database: `python manage.py migrate`
8. Create a superuser account: `python manage.py createsuperuser`
9. Start the development server: `python manage.py runserver`

To run the API locally, with Docker and Docker Compose, follow these steps:

1. Go to .settings.DATABASES section, deactivate #PRODUCTION mode and activate #Development mode, add PostgreSQL configuration to connect to your database to be the default database.
2. Change `.env.templates` to .env and setup your environment variables.
3. Start application and database integration: `sudo docker compose -f docker-compose.yaml up --build`
4. Stop and clear integration: `sudo docker compose -f docker-compose.yaml down`

Running Tests
---------------
Expand Down
1 change: 0 additions & 1 deletion build.sh
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,3 @@ python manage.py migrate
if [[ $CREATE_SUPERUSER ]]; then
python manage.py createsuperuser --no-input
fi

36 changes: 36 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
version: "3.9"
services:
postgres_db:
image: postgres:16
env_file:
- .env
expose:
- 5432
ports:
- "5432:5432"
volumes:
- postgres_data:/var/lib/postgresql/data/
api:
build:
context: .
dockerfile: Dockerfile.dev
depends_on:
- postgres_db
image: smart_learn:v1
env_file:
- .env
ports:
- "8080:8080"
command: sh -c "chmod +x /api/migrate.sh && sh /api/migrate.sh && /api/entrypoint.sh"
restart: always
redis:
image: redis:alpine
env_file:
- .env
expose:
- 6379
ports:
- "6379:6379"

volumes:
postgres_data:
5 changes: 5 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash
# creating a default application port to run on
APP_PORT=${PORT:-8080}
cd /api/
/opt/venv/bin/gunicorn --worker-tmp-dir /dev/shm smart_learning.wsgi:application --bind "0.0.0.0:$APP_PORT"
14 changes: 14 additions & 0 deletions migrate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env bash

set -o errexit # exit when there is an error error

# provide the superuser email, or a default
SUPERUSER_EMAIL=${DJANGO_SUPERUSER_EMAIL:-"hello@smartlearn.com"}

# enter working directory
cd /api/

# use the virtual env to run the python commands
/opt/venv/bin/python manage.py migrate --noinput

/opt/venv/bin/python manage.py createsuperuser --email $SUPERUSER_EMAIL --noinput || true
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ drf-yasg==1.21.5
gunicorn==20.1.0
phonenumbers==8.13.7
Pillow==9.4.0
psycopg2==2.9.5
# psycopg2==2.9.5
psycopg2-binary==2.9.7
python-dotenv==1.0.0
random-password-generator==2.2.0
Expand Down
17 changes: 13 additions & 4 deletions smart_learning/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = os.getenv('SECRET_KEY'),

ALLOWED_HOSTS = ['smart-learn.onrender.com', '127.0.0.1', 'localhost']
ALLOWED_HOSTS = ['smart-learn.onrender.com', '127.0.0.1', 'localhost', '0.0.0.0']
CORS_ORIGIN_ALLOW_ALL = True

# SECURITY WARNING: don't run with debug turned on in production!
Expand Down Expand Up @@ -123,11 +123,20 @@

DATABASES = {
# Development
# 'default': {
# "ENGINE": "django.db.backends.sqlite3",
# "NAME": BASE_DIR / "db.sqlite3",
# }

# DEVELOPMENT
'default': {
"ENGINE": "django.db.backends.sqlite3",
"NAME": BASE_DIR / "db.sqlite3",
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': os.environ.get("POSTGRES_DB"),
'USER': os.environ.get("POSTGRES_USER"),
'PASSWORD': os.environ.get("POSTGRES_PASSWORD"),
'HOST': os.environ.get("POSTGRES_HOST", 'localhost'),
'PORT': os.environ.get("POSTGRES_PORT", 5432),
}


#PRODUCTION
# 'default': dj_database_url.config(
Expand Down

0 comments on commit 92fd0bd

Please sign in to comment.