Skip to content

Commit

Permalink
set-up docker for production
Browse files Browse the repository at this point in the history
  • Loading branch information
walosha committed Jan 28, 2023
1 parent 2fab996 commit 1eaeb66
Show file tree
Hide file tree
Showing 11 changed files with 132 additions and 54 deletions.
3 changes: 3 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.venv
.git
.gitignore
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,7 @@ venv.bak/
photos/
chapters.txt
media/
church-375900-fbd978c352ca.json
church-375900-fbd978c352ca.json
new.json
old.json
.env.prod
14 changes: 14 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Pull official base Python Docker image
FROM python:3.10.6
# Set environment variables
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
# Set work directory
WORKDIR /code
# Install dependencies
RUN pip install --upgrade pip

COPY requirements.txt /code/
RUN pip install -r requirements.txt
# Copy the Django project
COPY . /code/
Empty file added core/settings/__init__.py
Empty file.
46 changes: 1 addition & 45 deletions core/settings.py → core/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,44 +10,20 @@
https://docs.djangoproject.com/en/4.0/ref/settings/
"""
import os
from dotenv import load_dotenv
from pathlib import Path
from datetime import timedelta
import dj_database_url
from decouple import config
import cloudinary
import cloudinary.uploader
import cloudinary.api
load_dotenv()

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
BASE_DIR = Path(__file__).resolve().parent.parent.parent


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/4.0/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-i3518xh534trgj8z%dbs+jnxi)s=y-xe75q7im1iil$3!u0$v^'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = [
"127.0.0.1", "0.0.0.0", "localhost", "api", "chrome-extension://fhbjgbiflinjbdggehcddcbncdddomop"]
INTERNAL_IPS = ["127.0.0.1",
'chrome-extension://fhbjgbiflinjbdggehcddcbncdddomop']
if DEBUG:
import os # only if you haven't already imported this
import socket # only if you haven't already imported this

hostname, _, ips = socket.gethostbyname_ex(socket.gethostname())
INTERNAL_IPS = [ip[:-1] + '1' for ip in ips] + ['127.0.0.1', '10.0.2.2']

USE_X_FORWARDED_HOST = True
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
CORS_ALLOW_ALL_ORIGINS = True
CORS_ALLOW_ALL_ORIGINS = True

# Application definition
TAGGIT_CASE_INSENSITIVE = True
Expand Down Expand Up @@ -123,9 +99,6 @@
WSGI_APPLICATION = 'core.wsgi.application'


DATABASES = {
"default": dj_database_url.config(default=config('DATABASE_URL'))
}

cloudinary.config(
cloud_name=os.getenv("CLOUDINARY_CLOUD_NAME"),
Expand All @@ -136,17 +109,6 @@



# DATABASES = {
# 'default': {
# 'ENGINE': "django.db.backends.postgresql",
# 'HOST': "",
# 'NAME': "postgres",
# 'USER': "postgres ",
# 'PASSWORD': "",
# 'PORT': "5432",

# }
# }

# Password validation
# https://docs.djangoproject.com/en/4.0/ref/settings/#auth-password-validators
Expand Down Expand Up @@ -249,9 +211,3 @@
ACCOUNT_USERNAME_REQUIRED = False


SIMPLE_JWT = {
'ACCESS_TOKEN_LIFETIME': timedelta(minutes=60),
'REFRESH_TOKEN_LIFETIME': timedelta(days=1),
'SIGNING_KEY': SECRET_KEY,

}
36 changes: 36 additions & 0 deletions core/settings/local.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import dj_database_url
from .base import *
from dotenv import load_dotenv
from datetime import timedelta
from decouple import config


load_dotenv()


SECRET_KEY="django-insecure-i3518xh534trgj8z%dbs+jnxi)s=y-xe75q7im1iil$3!u0$v^"
DEBUG = True

os.getenv
# SECURITY WARNING: don't run with debug turned on in production!

if DEBUG:
import os # only if you haven't already imported this
import socket # only if you haven't already imported this

hostname, _, ips = socket.gethostbyname_ex(socket.gethostname())
INTERNAL_IPS = [ip[:-1] + '1' for ip in ips] + ['127.0.0.1', '10.0.2.2']

os.getenv('GOOGLE_SERVICE_ACCOUNT')

DATABASES = {
"default": dj_database_url.config(default=config('DATABASE_URL'))
}


SIMPLE_JWT = {
'ACCESS_TOKEN_LIFETIME': timedelta(minutes=60),
'REFRESH_TOKEN_LIFETIME': timedelta(days=1),
'SIGNING_KEY': os.getenv("SECRET_KEY"),

}
53 changes: 53 additions & 0 deletions core/settings/prod.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import dj_database_url
from .base import *
from dotenv import load_dotenv
from datetime import timedelta
from decouple import config

load_dotenv()

SECRET_KEY="django-insecure-i3518xh534trgj8z%dbs+jnxi)s=y-xe75q7im1iil$3!u0$v^"


DEBUG = False


ADMINS = [
('Olawae Afuye', 'hello@pacific-professional.com.ng'),
]


ALLOWED_HOSTS = ["*"]


INTERNAL_IPS = ["127.0.0.1",
'chrome-extension://fhbjgbiflinjbdggehcddcbncdddomop']




USE_X_FORWARDED_HOST = True
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
CORS_ALLOW_ALL_ORIGINS = True
CORS_ALLOW_ALL_ORIGINS = True



DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql",
"NAME": "postgres",
"USER": "postgres",
"PASSWORD": "password",
"HOST": "pg1", # set in docker-compose.yml
"PORT": 5432, # default postgres port
}
}


SIMPLE_JWT = {
'ACCESS_TOKEN_LIFETIME': timedelta(minutes=60),
'REFRESH_TOKEN_LIFETIME': timedelta(days=1),
'SIGNING_KEY': os.getenv("SECRET_KEY"),

}
10 changes: 10 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,15 @@ services:
- pg1_data:/var/lib/postgresql/data
environment:
- POSTGRES_PASSWORD=password
web:
build: .
command: python /code/manage.py runserver 0.0.0.0:8000
restart: always
volumes:
- .:/code
ports:
- "8000:8000"
environment:
- DJANGO_SETTINGS_MODULE=core.settings.prod
volumes:
pg1_data:
7 changes: 3 additions & 4 deletions event/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@




SCOPES = ["https://www.googleapis.com/auth/calendar"]
service_account_email = os.getenv('GOOGLE_SERVICE_ACCOUNT')
credentials = service_account.Credentials.from_service_account_file('church-375900-fbd978c352ca.json')
credentials = service_account.Credentials.from_service_account_file('old.json')
scoped_credentials = credentials.with_scopes(SCOPES)
calendarId = os.getenv('CALENDER_ID')

Expand Down Expand Up @@ -58,7 +57,7 @@ class CalenderCreateAPIView (generics.CreateAPIView):
def post(self, request, *args, **kwargs):
request = request.data
if self.request:
return self.get_context_data(request)
return self.form_valid(request)
else:
return self.form_valid(request)

Expand All @@ -74,7 +73,7 @@ def form_valid(self, request):
service = build_service(self.request)
event = {
"calendarId": calendarId,
"conferenceDataVersion": 1,
# "conferenceDataVersion": 1,
"end": {
'dateTime': datetime.now(pytz.timezone('US/Central')).isoformat(),
'timeZone': 'US/Central'
Expand Down
3 changes: 2 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
- pip install -r requirements.txt
- python manage.py makemigrations
- python manage.py migrate
- python manage.py runserver
- python manage.py runserver --settings=core.settings.local ( Development )
- python manage.py runserver --settings=core.settings.prod ( Production )

`
@@ Access swagger documentation on : api/doc/#/
Expand Down
9 changes: 6 additions & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ drf-spectacular==0.22.1
django-taggit==3.0.0
django-filter==22.1
pillow==9.4.0
cloudinary=1.31.0
django-podcasting=1.3.2
python-dotenv=0.21.1
cloudinary==1.31.0
django-podcasting==1.3.2
python-dotenv==0.21.1
google-api-python-client
psycopg2==2.9.3
uwsgi==2.0.20
daphne==3.0.2

0 comments on commit 1eaeb66

Please sign in to comment.