diff --git a/.env.sample b/.env.sample index e50074b..439c473 100644 --- a/.env.sample +++ b/.env.sample @@ -12,7 +12,7 @@ DBHOST=postgres DBPORT=5432 SECRET_KEY=django-insecure-ys)is-uls_$yaa(f%iyy^^7pe4a@ql)3thr9loszz#!8l4m4fk PRODUCTION=false -WEBSITE_HOSTNAME="" +ALLOWED_HOSTS="" # Celery env CACHELOCATION=redis://redis:6379/0 diff --git a/api/admin.py b/api/admin.py deleted file mode 100644 index 8c38f3f..0000000 --- a/api/admin.py +++ /dev/null @@ -1,3 +0,0 @@ -from django.contrib import admin - -# Register your models here. diff --git a/api/models.py b/api/models.py index 4eabb1d..78a118a 100644 --- a/api/models.py +++ b/api/models.py @@ -1,4 +1,3 @@ -import datetime import math from django.contrib.auth.models import User diff --git a/api/tests.py b/api/tests.py deleted file mode 100644 index 7ce503c..0000000 --- a/api/tests.py +++ /dev/null @@ -1,3 +0,0 @@ -from django.test import TestCase - -# Create your tests here. diff --git a/api/views.py b/api/views.py index 5d3bd7e..9b1339a 100644 --- a/api/views.py +++ b/api/views.py @@ -6,7 +6,6 @@ from django.db.models import F, Max from django.shortcuts import get_object_or_404 from rest_framework import generics, permissions, status -from rest_framework.authtoken.models import Token from rest_framework.response import Response from rest_framework.reverse import reverse from rest_framework.views import APIView diff --git a/core/__init__.py b/config/__init__.py similarity index 100% rename from core/__init__.py rename to config/__init__.py diff --git a/core/asgi.py b/config/asgi.py similarity index 82% rename from core/asgi.py rename to config/asgi.py index eac5cc5..3faa23e 100644 --- a/core/asgi.py +++ b/config/asgi.py @@ -11,6 +11,6 @@ from django.core.asgi import get_asgi_application -os.environ.setdefault("DJANGO_SETTINGS_MODULE", "core.settings") +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings") application = get_asgi_application() diff --git a/api/tasks.py b/config/django/__init__.py similarity index 100% rename from api/tasks.py rename to config/django/__init__.py diff --git a/core/settings.py b/config/django/base.py similarity index 76% rename from core/settings.py rename to config/django/base.py index 8d352d0..bce79ca 100644 --- a/core/settings.py +++ b/config/django/base.py @@ -10,8 +10,8 @@ https://docs.djangoproject.com/en/5.0/ref/settings/ """ -import os from pathlib import Path +from config.env import env # Build paths inside the project like this: BASE_DIR / 'subdir'. BASE_DIR = Path(__file__).resolve().parent.parent @@ -21,12 +21,12 @@ # See https://docs.djangoproject.com/en/5.0/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! -SECRET_KEY = os.getenv("SECRET_KEY") +SECRET_KEY = env.str("SECRET_KEY") # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True -ALLOWED_HOSTS = ["localhost", "127.0.0.1", ".ngrok-free.app", "*"] +ALLOWED_HOSTS = ["*"] # Application definition @@ -34,7 +34,6 @@ INSTALLED_APPS = [ "api", "corsheaders", - "django_celery_results", "rest_framework", "knox", "django.contrib.admin", @@ -71,7 +70,7 @@ "django.middleware.clickjacking.XFrameOptionsMiddleware", ] -ROOT_URLCONF = "core.urls" +ROOT_URLCONF = "config.urls" TEMPLATES = [ { @@ -89,19 +88,19 @@ }, ] -WSGI_APPLICATION = "core.wsgi.application" +WSGI_APPLICATION = "config.wsgi.application" -# Database -# https://docs.djangoproject.com/en/5.0/ref/settings/#databases +# Configure Postgres database based on connection string of the libpq Keyword/Value form +# https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNSTRING + DATABASES = { "default": { - "ENGINE": os.environ.get("DBENGINE"), - "NAME": os.environ.get("DBNAME"), - "HOST": os.environ.get("DBHOST"), - "USER": os.environ.get("DBUSER"), - "PASSWORD": os.environ.get("DBPASS"), - "PORT": os.environ.get("DBPORT"), + "ENGINE": env.str("DBENGINE"), + "NAME": env.str("DBNAME"), + "HOST": env.str("DBHOST"), + "USER": env.str("DBUSER"), + "PASSWORD": env.str("DBPASS"), } } @@ -109,9 +108,10 @@ CACHES = { "default": { "BACKEND": "django_redis.cache.RedisCache", - "LOCATION": os.environ.get("CACHELOCATION"), + "LOCATION": env.str("CACHELOCATION"), "OPTIONS": { "CLIENT_CLASS": "django_redis.client.DefaultClient", + "COMPRESSOR": "django_redis.compressors.zlib.ZlibCompressor", }, } } @@ -158,3 +158,23 @@ # https://docs.djangoproject.com/en/5.0/ref/settings/#default-auto-field DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField" + +LOGGING = { + "version": 1, + "disable_existing_loggers": False, + "handlers": { + "console": {"class": "logging.StreamHandler", "formatter": "app"}, + }, + "root": { + "handlers": ["console"], + "level": "INFO", + }, + "formatters": { + "app": { + "format": ( + "%(asctime)s [%(levelname)-8s] " "(%(module)s.%(funcName)s) %(message)s" + ), + "datefmt": "%Y-%m-%d %H:%M:%S", + }, + }, +} diff --git a/config/django/production.py b/config/django/production.py new file mode 100644 index 0000000..795d2f0 --- /dev/null +++ b/config/django/production.py @@ -0,0 +1,11 @@ +from .base import * # noqa +from config.env import env + +DEBUG = False + +# Configure the domain name using the environment variable +ALLOWED_HOSTS = env.list("ALLOWED_HOSTS") + [ + "127.0.0.1" +] # localhost required for docker health checks + +CSRF_TRUSTED_ORIGINS = [f"https://{host}/" for host in ALLOWED_HOSTS] diff --git a/config/env.py b/config/env.py new file mode 100644 index 0000000..463cfb8 --- /dev/null +++ b/config/env.py @@ -0,0 +1,3 @@ +import environ + +env = environ.Env() diff --git a/core/urls.py b/config/urls.py similarity index 100% rename from core/urls.py rename to config/urls.py diff --git a/core/wsgi.py b/config/wsgi.py similarity index 82% rename from core/wsgi.py rename to config/wsgi.py index f1f2540..24d8991 100644 --- a/core/wsgi.py +++ b/config/wsgi.py @@ -11,6 +11,6 @@ from django.core.wsgi import get_wsgi_application -os.environ.setdefault("DJANGO_SETTINGS_MODULE", "core.settings") +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings") application = get_wsgi_application() diff --git a/core/production.py b/core/production.py deleted file mode 100644 index 6f7ffe2..0000000 --- a/core/production.py +++ /dev/null @@ -1,61 +0,0 @@ -import os - -from .settings import * - -# Configure the domain name using the environment variable -ALLOWED_HOSTS = ( - [os.environ["WEBSITE_HOSTNAME"], "127.0.0.1"] - if "WEBSITE_HOSTNAME" in os.environ - else ["127.0.0.1"] -) -CSRF_TRUSTED_ORIGINS = ( - ["https://" + os.environ["WEBSITE_HOSTNAME"]] - if "WEBSITE_HOSTNAME" in os.environ - else [] -) -DEBUG = False -# SECURE_SSL_REDIRECT = True -# SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https") - -# Configure Postgres database based on connection string of the libpq Keyword/Value form -# https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNSTRING -DATABASES = { - "default": { - "ENGINE": os.environ.get("DBENGINE"), - "NAME": os.environ.get("DBNAME"), - "HOST": os.environ.get("DBHOST"), - "USER": os.environ.get("DBUSER"), - "PASSWORD": os.environ.get("DBPASS"), - } -} - -CACHES = { - "default": { - "BACKEND": "django_redis.cache.RedisCache", - "LOCATION": os.environ.get("CACHELOCATION"), - "OPTIONS": { - "CLIENT_CLASS": "django_redis.client.DefaultClient", - "COMPRESSOR": "django_redis.compressors.zlib.ZlibCompressor", - }, - } -} - -LOGGING = { - "version": 1, - "disable_existing_loggers": False, - "handlers": { - "console": {"class": "logging.StreamHandler", "formatter": "app"}, - }, - "root": { - "handlers": ["console"], - "level": "INFO", - }, - "formatters": { - "app": { - "format": ( - "%(asctime)s [%(levelname)-8s] " "(%(module)s.%(funcName)s) %(message)s" - ), - "datefmt": "%Y-%m-%d %H:%M:%S", - }, - }, -} diff --git a/manage.py b/manage.py index e68d295..67d3c9e 100755 --- a/manage.py +++ b/manage.py @@ -11,10 +11,10 @@ def main(): # If Website is hosted, use the production settings if os.environ.get("PRODUCTION") == "true": logging.info("Production settings loaded") - os.environ.setdefault("DJANGO_SETTINGS_MODULE", "core.production") + os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.django.production") else: logging.info("Development settings loaded") - os.environ.setdefault("DJANGO_SETTINGS_MODULE", "core.settings") + os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.django.base") try: from django.core.management import execute_from_command_line diff --git a/requirements.txt b/requirements.txt index e3f385e..754ac5a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,101 +1,18 @@ -amqp==5.2.0 asgiref==3.7.2 -billiard==4.2.0 celery==5.3.6 -click==8.1.7 -click-didyoumean==0.3.0 -click-plugins==1.1.1 -click-repl==0.3.0 conditional==1.5 Django==5.0.1 -django-celery-results==2.5.1 -django-clone==5.3.3 -django-cors-headers==4.3.1 -django-redis==5.4.0 -djangorestframework==3.14.0 -kombu==5.3.5 -prompt-toolkit==3.0.43 -psycopg==3.1.17 -psycopg2-binary==2.9.9 -pyngrok==7.1.0 -python-dateutil==2.8.2 -python-dotenv==1.0.1 -pytz==2023.3.post1 -PyYAML==6.0.1 -redis==5.0.1 -six==1.16.0 -sqlparse==0.4.4 -typing_extensions==4.9.0 -tzdata==2023.4 -vine==5.1.0 -wcwidth==0.2.13 -amqp==5.2.0 -asgiref==3.7.2 -async-timeout==4.0.3 -billiard==4.2.0 -celery==5.3.6 -click==8.1.7 -click-didyoumean==0.3.0 -click-plugins==1.1.1 -click-repl==0.3.0 -conditional==1.5 -Django==5.0.1 -django-celery-results==2.5.1 -django-clone==5.3.3 -django-cors-headers==4.3.1 -django-redis==5.4.0 -djangorestframework==3.14.0 -gunicorn==21.2.0 -kombu==5.3.5 -packaging==24.0 -prompt-toolkit==3.0.43 -psycopg==3.1.17 -psycopg2-binary==2.9.9 -pyngrok==7.1.0 -python-dateutil==2.8.2 -python-dotenv==1.0.1 -pytz==2023.3.post1 -PyYAML==6.0.1 -redis==5.0.1 -six==1.16.0 -sqlparse==0.4.4 -typing_extensions==4.9.0 -tzdata==2023.4 -vine==5.1.0 -wcwidth==0.2.13 -amqp==5.2.0 -asgiref==3.7.2 -async-timeout==4.0.3 -billiard==4.2.0 -celery==5.3.6 -click==8.1.7 -click-didyoumean==0.3.0 -click-plugins==1.1.1 -click-repl==0.3.0 -conditional==1.5 -Django==5.0.1 -django-celery-results==2.5.1 django-clone==5.3.3 django-cors-headers==4.3.1 +django-environ==0.12.0 +django-health-check==3.18.3 django-redis==5.4.0 +django-rest-knox==5.0.2 djangorestframework==3.14.0 -gunicorn==21.2.0 -kombu==5.3.5 -packaging==24.0 -prompt-toolkit==3.0.43 -psycopg==3.1.17 psycopg2-binary==2.9.9 -pyngrok==7.1.0 -python-dateutil==2.8.2 -python-dotenv==1.0.1 pytz==2023.3.post1 -PyYAML==6.0.1 redis==5.0.1 +setuptools==72.1.0 six==1.16.0 sqlparse==0.4.4 typing_extensions==4.9.0 -tzdata==2023.4 -vine==5.1.0 -wcwidth==0.2.13 -django-rest-knox==5.0.2 -django-health-check==3.18.3 diff --git a/tasks/celery.py b/tasks/celery.py index ce1b4e1..f32d5e7 100644 --- a/tasks/celery.py +++ b/tasks/celery.py @@ -5,10 +5,10 @@ from celery import Celery if os.environ.get("PRODUCTION") == "true": - os.environ.setdefault("DJANGO_SETTINGS_MODULE", "core.production") + os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.django.production") broker_url = os.getenv("BROKERLOCATION") else: - os.environ.setdefault("DJANGO_SETTINGS_MODULE", "core.settings") + os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.django.base") broker_url = os.getenv("BROKERLOCATION") @@ -17,7 +17,6 @@ app = Celery("tasks") app.conf.timezone = "Asia/Kolkata" app.conf.broker_url = broker_url -app.conf.result_backend = "django-db" @app.task