diff --git a/.coveragerc b/.coveragerc new file mode 100644 index 0000000..b12264c --- /dev/null +++ b/.coveragerc @@ -0,0 +1,6 @@ +[coverage:run] +# This section tells coverage to ignore certain files or directories +omit = + */__init__.py + todo_project/wsgi.py + manage.py \ No newline at end of file diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..ecfce9e --- /dev/null +++ b/.env.example @@ -0,0 +1,5 @@ +ENV='DEVELOPMENT' +SECRET_KEY='unique-secret' +ALLOWED_HOSTS='localhost,127.0.0.1' +MONGODB_URI='mongodb://localhost:27017' +DB_NAME='todo-app' \ No newline at end of file diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..7206c27 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,41 @@ +name: Tests +on: + pull_request: + branches: + - "**" + +jobs: + build: + runs-on: ubuntu-latest + container: python:3.12-slim-bookworm + + services: + db: + image: mongo:latest + ports: + - 27017:27017 + + env: + MONGODB_URI: mongodb://db:27017 + DB_NAME: todo-app + + steps: + - name: Checkout code + uses: actions/checkout@v3 + + - name: Set up Python + uses: actions/setup-python@v4 + with: + python-version: '3.12' + + - name: Install dependencies + run: | + python3.12 -m pip install -r requirements.txt + + - name: Lint check + run: | + ruff check + + - name: Run tests + run: | + python3.12 manage.py test \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..85412ea --- /dev/null +++ b/.gitignore @@ -0,0 +1,107 @@ +__pycache__ +*.py[cod] +*$py.class + +*.so + +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +*.manifest +*.spec + +pip-log.txt +pip-delete-this-directory.txt + +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +cover/ + +*.mo +*.pot + +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +instance/ +.webassets-cache + +.scrapy + +docs/_build/ + +.pybuilder/ +target/ + +.ipynb_checkpoints + +profile_default/ +ipython_config.py + +.pdm.toml +.pdm-python +.pdm-build/ + +__pypackages__/ + +celerybeat-schedule +celerybeat.pid + +*.sage.py + +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +.spyderproject +.spyproject + +.ropeproject + +/site + +.mypy_cache/ +.dmypy.json +dmypy.json + +.pyre/ + +.pytype/ + +cython_debug/ + +.ruff_cache +mongo_data +logs \ No newline at end of file diff --git a/.python-version b/.python-version new file mode 100644 index 0000000..56bb660 --- /dev/null +++ b/.python-version @@ -0,0 +1 @@ +3.12.7 diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..1df7d31 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,17 @@ +# Use an official Python runtime as a parent image +FROM python:3.12-slim-bookworm + +# Set environment variables +ENV PYTHONUNBUFFERED 1 + +# Set the working directory in the container +WORKDIR /app + +# Copy the requirements file into the container +COPY requirements.txt /app/ + +# Install any needed packages specified in requirements.txt +RUN pip install -r requirements.txt + +# Copy the rest of the application code into the container +COPY . /app/ \ No newline at end of file diff --git a/README.md b/README.md index 8ddbf54..fc66e94 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,96 @@ -# This is for backend service -A template to create all public facing sites +# TODO Backend + +## Local development setup +1. Install pyenv + - For Mac/Linux - https://github.com/pyenv/pyenv?tab=readme-ov-file#installation + - For Windows - https://github.com/pyenv-win/pyenv-win/blob/master/docs/installation.md#chocolatey +2. Install the configured python version (3.12.7) using pyenv by running the command + - For Mac/Linux + ``` + pyenv install + ``` + - For Windows + ``` + pyenv install 3.12.7 + ``` +3. Create virtual environment by running the command + - For Mac/Linux + ``` + pyenv virtualenv 3.12.7 venv + ``` + - For Windows + ``` + python -m pip install virtualenv + python -m virtualenv venv + ``` +4. Activate the virtual environment by running the command + - For Mac/Linux + ``` + pyenv activate venv + ``` + - For Windows + ``` + .\venv\Scripts\activate + ``` +5. Install the project dependencies by running the command + ``` + python -m pip install -r requirements.txt + ``` +6. Create a `.env` file in the root directory, and copy the content from the `.env.example` file to it +7. Install [docker](https://docs.docker.com/get-docker/) and [docker compose](https://docs.docker.com/compose/install/) +8. Start MongoDB using docker + ``` + docker-compose up -d db + ``` +9. Start the development server by running the command + ``` + python manage.py runserver + ``` +10. Go to http://127.0.0.1:8000/health/ API to make sure the server it up. You should see this response + ``` + { + "status": "UP", + "components": { + "db": { + "status": "UP" + } + } + } + ``` + +## To simply try out the app +1. Install [docker](https://docs.docker.com/get-docker/) and [docker compose](https://docs.docker.com/compose/install/) +2. Start Django application and MongoDB using docker + ``` + docker-compose up -d + ``` +3. Go to http://127.0.0.1:8000/health/ API to make sure the server it up. You should see this response + ``` + { + "status": "UP" + } + ``` +4. On making changes to code and saving, live reload will work in this case as well + +## Command reference +1. To run the tests, run the following command + ``` + python manage.py test + ``` +2. To check test coverage, run the following command + ``` + coverage run --source='.' manage.py test + coverage report + ``` +3. To run the formatter + ``` + ruff format + ``` +4. To run lint check + ``` + ruff check + ``` +5. To fix lint issues + ``` + ruff check --fix + ``` \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..eec4faf --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,24 @@ +version: "3" + +services: + django-app: + build: . + container_name: todo-django-app + command: python manage.py runserver 0.0.0.0:8000 + environment: + MONGODB_URI: mongodb://db:27017 + DB_NAME: todo-app + volumes: + - .:/app + ports: + - "8000:8000" + depends_on: + - db + + db: + image: mongo:latest + container_name: todo-mongo + ports: + - "27017:27017" + volumes: + - ./mongo_data:/data/db \ No newline at end of file diff --git a/manage.py b/manage.py new file mode 100755 index 0000000..f06f50d --- /dev/null +++ b/manage.py @@ -0,0 +1,25 @@ +#!/usr/bin/env python +"""Django's command-line utility for administrative tasks.""" + +import sys + +from todo_project.settings.configure import configure_settings_module + + +def main(): + """Run administrative tasks.""" + configure_settings_module() + + try: + from django.core.management import execute_from_command_line + except ImportError as exc: + raise ImportError( + "Couldn't import Django. Are you sure it's installed and " + "available on your PYTHONPATH environment variable? Did you " + "forget to activate a virtual environment?" + ) from exc + execute_from_command_line(sys.argv) + + +if __name__ == "__main__": + main() diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..1568474 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,74 @@ +[tool.ruff] +# Exclude a variety of commonly ignored directories. +exclude = [ + ".bzr", + ".direnv", + ".eggs", + ".git", + ".git-rewrite", + ".hg", + ".ipynb_checkpoints", + ".mypy_cache", + ".nox", + ".pants.d", + ".pyenv", + ".pytest_cache", + ".pytype", + ".ruff_cache", + ".svn", + ".tox", + ".venv", + ".vscode", + "__pypackages__", + "_build", + "buck-out", + "build", + "dist", + "node_modules", + "site-packages", + "venv", +] + +# Same as Black. +line-length = 120 +indent-width = 4 + +target-version = "py313" + +[tool.ruff.lint] +# Enable Pyflakes (`F`) and a subset of the pycodestyle (`E`) codes by default. +# Unlike Flake8, Ruff doesn't enable pycodestyle warnings (`W`) or +# McCabe complexity (`C901`) by default. +select = ["E4", "E7", "E9", "F"] +ignore = [] + +# Allow fix for all enabled rules (when `--fix`) is provided. +fixable = ["ALL"] +unfixable = [] + +[tool.ruff.format] +# Like Black, use double quotes for strings. +quote-style = "double" + +# Like Black, indent with spaces, rather than tabs. +indent-style = "space" + +# Like Black, respect magic trailing commas. +skip-magic-trailing-comma = false + +# Like Black, automatically detect the appropriate line ending. +line-ending = "auto" + +# Enable auto-formatting of code examples in docstrings. Markdown, +# reStructuredText code/literal blocks and doctests are all supported. +# +# This is currently disabled by default, but it is planned for this +# to be opt-out in the future. +docstring-code-format = false + +# Set the line length limit used when formatting code snippets in +# docstrings. +# +# This only has an effect when the `docstring-code-format` setting is +# enabled. +docstring-code-line-length = "dynamic" \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..68ee1be --- /dev/null +++ b/requirements.txt @@ -0,0 +1,17 @@ +asgiref==3.8.1 +cfgv==3.4.0 +coverage==7.6.4 +distlib==0.3.9 +Django==5.1.2 +djangorestframework==3.15.2 +dnspython==2.7.0 +filelock==3.16.1 +identify==2.6.1 +nodeenv==1.9.1 +platformdirs==4.3.6 +pymongo==4.10.1 +python-dotenv==1.0.1 +PyYAML==6.0.2 +ruff==0.7.1 +sqlparse==0.5.1 +virtualenv==20.27.0 diff --git a/todo/__init__.py b/todo/__init__.py new file mode 100644 index 0000000..84a4d93 --- /dev/null +++ b/todo/__init__.py @@ -0,0 +1 @@ +# Added this because without this file Django isn't able to auto detect the test files diff --git a/todo/apps.py b/todo/apps.py new file mode 100644 index 0000000..b4f06d5 --- /dev/null +++ b/todo/apps.py @@ -0,0 +1,5 @@ +from django.apps import AppConfig + + +class TodoConfig(AppConfig): + name = "todo" diff --git a/todo/constants/health.py b/todo/constants/health.py new file mode 100644 index 0000000..cf58e41 --- /dev/null +++ b/todo/constants/health.py @@ -0,0 +1,15 @@ +from enum import Enum +from rest_framework import status + + +class AppHealthStatus(Enum): + UP = status.HTTP_200_OK + DOWN = status.HTTP_503_SERVICE_UNAVAILABLE + + def __init__(self, http_status): + self.http_status = http_status + + +class ComponentHealthStatus(Enum): + UP = 1 + DOWN = 2 diff --git a/todo/tests/__init__.py b/todo/tests/__init__.py new file mode 100644 index 0000000..84a4d93 --- /dev/null +++ b/todo/tests/__init__.py @@ -0,0 +1 @@ +# Added this because without this file Django isn't able to auto detect the test files diff --git a/todo/tests/unit/__init__.py b/todo/tests/unit/__init__.py new file mode 100644 index 0000000..84a4d93 --- /dev/null +++ b/todo/tests/unit/__init__.py @@ -0,0 +1 @@ +# Added this because without this file Django isn't able to auto detect the test files diff --git a/todo/tests/unit/test_health.py b/todo/tests/unit/test_health.py new file mode 100644 index 0000000..5bca4b3 --- /dev/null +++ b/todo/tests/unit/test_health.py @@ -0,0 +1,26 @@ +from rest_framework.reverse import reverse +from rest_framework.test import APISimpleTestCase, APIClient +from rest_framework import status +from unittest.mock import patch +from todo.constants.health import AppHealthStatus, ComponentHealthStatus + + +class HealthAPITests(APISimpleTestCase): + def setUp(self): + self.client = APIClient() + + @patch(target="todo_project.db.config.DatabaseManager.check_database_health", return_value=True) + def test_health_api_returns_200_when_db_healthy(self, mocked): + url = reverse("health") + response = self.client.get(url) + self.assertEqual(response.status_code, status.HTTP_200_OK) + self.assertEqual(response.data["status"], AppHealthStatus.UP.name) + self.assertEqual(response.data["components"]["db"]["status"], ComponentHealthStatus.UP.name) + + @patch(target="todo_project.db.config.DatabaseManager.check_database_health", return_value=False) + def test_health_api_returns_503_when_db_not_healthy(self, mocked): + url = reverse("health") + response = self.client.get(url) + self.assertEqual(response.status_code, status.HTTP_503_SERVICE_UNAVAILABLE) + self.assertEqual(response.data["status"], AppHealthStatus.DOWN.name) + self.assertEqual(response.data["components"]["db"]["status"], ComponentHealthStatus.DOWN.name) diff --git a/todo/views.py b/todo/views.py new file mode 100644 index 0000000..6487a3c --- /dev/null +++ b/todo/views.py @@ -0,0 +1,21 @@ +from rest_framework.views import APIView +from rest_framework.response import Response +from .constants.health import AppHealthStatus, ComponentHealthStatus +from todo_project.db.config import DatabaseManager + +database_manager = DatabaseManager() + + +class HealthView(APIView): + def get(self, request): + global database_manager + is_db_healthy = database_manager.check_database_health() + db_status = ComponentHealthStatus.UP.name if is_db_healthy else ComponentHealthStatus.DOWN.name + overall_status = AppHealthStatus.UP if is_db_healthy else AppHealthStatus.DOWN + response = { + "status": overall_status.name, + "components": { + "db": {"status": db_status}, + }, + } + return Response(response, overall_status.http_status) diff --git a/todo_project/__init__.py b/todo_project/__init__.py new file mode 100644 index 0000000..84a4d93 --- /dev/null +++ b/todo_project/__init__.py @@ -0,0 +1 @@ +# Added this because without this file Django isn't able to auto detect the test files diff --git a/todo_project/db/config.py b/todo_project/db/config.py new file mode 100644 index 0000000..f7ade23 --- /dev/null +++ b/todo_project/db/config.py @@ -0,0 +1,41 @@ +import logging +from django.conf import settings +from pymongo import MongoClient +from pymongo.errors import ConnectionFailure + +logger = logging.getLogger(__name__) + + +class DatabaseManager: + __instance = None + + def __new__(cls, *args, **kwargs): + if cls.__instance is None: + cls.__instance = super().__new__(cls, *args, **kwargs) + cls.__instance._database_client = None + cls.__instance._db = None + return cls.__instance + + def _get_database_client(self): + if self._database_client is None: + self._database_client = MongoClient(settings.MONGODB_URI) + return self._database_client + + def get_database(self): + if self._db is None: + self._db = self._get_database_client()[settings.DB_NAME] + return self._db + + def get_collection(self, collection_name): + database = self.get_database() + return database[collection_name] + + def check_database_health(self): + try: + db_client = self._get_database_client() + db_client.admin.command("ping") + logger.info("Database connection established successfully") + return True + except ConnectionFailure as e: + logger.error(f"Failed to establish database connection: {e}") + return False diff --git a/todo_project/settings/base.py b/todo_project/settings/base.py new file mode 100644 index 0000000..2d4fd9e --- /dev/null +++ b/todo_project/settings/base.py @@ -0,0 +1,52 @@ +import os +from pathlib import Path + +# Build paths inside the project like this: BASE_DIR / 'subdir'. +BASE_DIR = Path(__file__).resolve().parent.parent + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = os.getenv( + "SECRET_KEY", + "django-insecure-w$a*-^hjqf&snr6qd&jkcq%0*5twb!_)qe0&z(2y-17umjr5tn", +) + +# SECURITY WARNING: don't run with debug turned on in production! +DEBUG = True + +ALLOWED_HOSTS = [] + +MONGODB_URI = os.getenv("MONGODB_URI") +DB_NAME = os.getenv("DB_NAME") +# Application definition + +INSTALLED_APPS = [ + "rest_framework", + "todo", +] + +MIDDLEWARE = [ + "django.middleware.security.SecurityMiddleware", + "django.middleware.common.CommonMiddleware", + "django.middleware.csrf.CsrfViewMiddleware", + "django.middleware.clickjacking.XFrameOptionsMiddleware", +] + +ROOT_URLCONF = "todo_project.urls" + +WSGI_APPLICATION = "todo_project.wsgi.application" + + +LANGUAGE_CODE = "en-us" + +TIME_ZONE = "UTC" + +USE_I18N = True + +USE_TZ = True + +REST_FRAMEWORK = { + "DEFAULT_RENDERER_CLASSES": [ + "rest_framework.renderers.JSONRenderer", + ], + "UNAUTHENTICATED_USER": None, +} diff --git a/todo_project/settings/configure.py b/todo_project/settings/configure.py new file mode 100644 index 0000000..9176531 --- /dev/null +++ b/todo_project/settings/configure.py @@ -0,0 +1,22 @@ +import os +from dotenv import load_dotenv + +load_dotenv() + +ENV_VAR_NAME = "ENV" +PRODUCTION = "PRODUCTION" +DEVELOPMENT = "DEVELOPMENT" +PRODUCTION_SETTINGS = "todo_project.settings.production" +DEVELOPMENT_SETTINGS = "todo_project.settings.development" +DEFAULT_SETTINGS = DEVELOPMENT_SETTINGS + + +def configure_settings_module(): + env = os.getenv(ENV_VAR_NAME, DEVELOPMENT).upper() + + django_settings_module = DEVELOPMENT_SETTINGS + + if env == PRODUCTION: + django_settings_module = PRODUCTION_SETTINGS + + os.environ.setdefault("DJANGO_SETTINGS_MODULE", django_settings_module) diff --git a/todo_project/settings/development.py b/todo_project/settings/development.py new file mode 100644 index 0000000..aa4ee91 --- /dev/null +++ b/todo_project/settings/development.py @@ -0,0 +1,2 @@ +# Add settings for development environment here +from .base import * # noqa: F403 diff --git a/todo_project/settings/production.py b/todo_project/settings/production.py new file mode 100644 index 0000000..dc05bc4 --- /dev/null +++ b/todo_project/settings/production.py @@ -0,0 +1,6 @@ +from .base import * # noqa: F403 +import os + +DEBUG = False + +ALLOWED_HOSTS = os.getenv("ALLOWED_HOSTS").split(",") diff --git a/todo_project/tests/__init__.py b/todo_project/tests/__init__.py new file mode 100644 index 0000000..84a4d93 --- /dev/null +++ b/todo_project/tests/__init__.py @@ -0,0 +1 @@ +# Added this because without this file Django isn't able to auto detect the test files diff --git a/todo_project/tests/unit/__init__.py b/todo_project/tests/unit/__init__.py new file mode 100644 index 0000000..84a4d93 --- /dev/null +++ b/todo_project/tests/unit/__init__.py @@ -0,0 +1 @@ +# Added this because without this file Django isn't able to auto detect the test files diff --git a/todo_project/tests/unit/test_database_manager.py b/todo_project/tests/unit/test_database_manager.py new file mode 100644 index 0000000..2c73969 --- /dev/null +++ b/todo_project/tests/unit/test_database_manager.py @@ -0,0 +1,109 @@ +from unittest import TestCase +from unittest.mock import patch, MagicMock + +from django.conf import settings +from todo_project.db.config import DatabaseManager +from pymongo import MongoClient +from pymongo.database import Database, Collection +from pymongo.errors import ConnectionFailure + + +class DatabaseManagerTests(TestCase): + def setUp(self): + DatabaseManager._DatabaseManager__instance = None + self.database_manager = DatabaseManager() + + def tearDown(self): + self.database_manager = None + + def test_singleton_ensures_single_instance(self): + database_manager1 = DatabaseManager() + database_manager2 = DatabaseManager() + self.assertIs(database_manager1, database_manager2) + + @patch("todo_project.db.config.MongoClient") + def test_initializes_db_client_on_first_call(self, mock_mongo_client): + mock_client_instance = MagicMock(spec=MongoClient) + mock_mongo_client.return_value = mock_client_instance + db_client = self.database_manager._get_database_client() + + mock_mongo_client.assert_called_once_with(settings.MONGODB_URI) + + self.assertIs(db_client, mock_client_instance) + + @patch("todo_project.db.config.MongoClient") + def test_reuses_existing_db_client_on_subsequent_calls(self, mock_mongo_client): + mock_client_instance = MagicMock(spec=MongoClient) + mock_mongo_client.return_value = mock_client_instance + + db_client1 = self.database_manager._get_database_client() + db_client2 = self.database_manager._get_database_client() + + mock_mongo_client.assert_called_once() + self.assertIs(db_client1, db_client2) + + @patch("todo_project.db.config.DatabaseManager._get_database_client") + def test_initializes_db_on_first_call(self, mock_get_database_client): + mock_client = MagicMock(spec=MongoClient) + mock_database_instance = MagicMock(spec=Database) + mock_client.__getitem__.return_value = mock_database_instance + mock_get_database_client.return_value = mock_client + + db_instance = self.database_manager.get_database() + + mock_get_database_client.assert_called_once() + + mock_client.__getitem__.assert_called_once_with(settings.DB_NAME) + + self.assertIs(db_instance, mock_database_instance) + + @patch("todo_project.db.config.DatabaseManager._get_database_client") + def test_reuses_existing_db_on_subsequent_calls(self, mock_get_database_client): + mock_client = MagicMock(spec=MongoClient) + mock_database_instance = MagicMock(spec=Database) + mock_client.__getitem__.return_value = mock_database_instance + mock_get_database_client.return_value = mock_client + + db_instance1 = self.database_manager.get_database() + db_instance2 = self.database_manager.get_database() + + mock_get_database_client.assert_called_once() + + self.assertIs(db_instance1, db_instance2) + + @patch("todo_project.db.config.DatabaseManager.get_database") + def test_get_collection_returns_specified_collection(self, mock_get_database): + mock_database_instance = MagicMock(spec=Database) + mock_collection = MagicMock(spec=Collection) + mock_database_instance.__getitem__.return_value = mock_collection + mock_get_database.return_value = mock_database_instance + + collection_name = "test_collection" + collection = self.database_manager.get_collection(collection_name) + + self.assertEqual(collection, mock_collection) + mock_database_instance.__getitem__.assert_called_once_with(collection_name) + + @patch("todo_project.db.config.DatabaseManager._get_database_client") + def test_check_db_health_returns_true_on_successful_connection(self, mock_get_database_client): + mock_client = MagicMock() + mock_client.admin.command.return_value = {"ok": 1} + mock_get_database_client.return_value = mock_client + + result = self.database_manager.check_database_health() + + self.assertTrue(result) + mock_get_database_client.assert_called_once() + mock_client.admin.command.assert_called_once_with("ping") + + @patch("todo_project.db.config.DatabaseManager._get_database_client") + def test_check_db_health_returns_false_on_connection_failure(self, mock_get_database_client): + mock_client = MagicMock() + mock_client.admin.command.side_effect = ConnectionFailure("Mocked connection failure") + mock_get_database_client.return_value = mock_client + + result = self.database_manager.check_database_health() + + self.assertFalse(result) + mock_get_database_client.assert_called_once() + mock_client.admin.command.assert_called_once_with("ping") diff --git a/todo_project/tests/unit/test_settings_configure.py b/todo_project/tests/unit/test_settings_configure.py new file mode 100644 index 0000000..f4e81e9 --- /dev/null +++ b/todo_project/tests/unit/test_settings_configure.py @@ -0,0 +1,21 @@ +import unittest +import os +from unittest.mock import patch +from todo_project.settings.configure import ( + configure_settings_module, + PRODUCTION_SETTINGS, + DEFAULT_SETTINGS, + ENV_VAR_NAME, + PRODUCTION, +) + + +class SettingModuleConfigTests(unittest.TestCase): + @patch.dict(os.environ, {ENV_VAR_NAME: PRODUCTION}, clear=True) + def test_uses_production_settings_when_env_var_set(self): + configure_settings_module() + self.assertEqual(os.getenv("DJANGO_SETTINGS_MODULE"), PRODUCTION_SETTINGS) + + def test_uses_default_settings_when_env_var_not_set(self): + configure_settings_module() + self.assertEqual(os.getenv("DJANGO_SETTINGS_MODULE"), DEFAULT_SETTINGS) diff --git a/todo_project/urls.py b/todo_project/urls.py new file mode 100644 index 0000000..f5374b4 --- /dev/null +++ b/todo_project/urls.py @@ -0,0 +1,6 @@ +from django.urls import path +from todo import views + +urlpatterns = [ + path("health", views.HealthView.as_view(), name="health"), +] diff --git a/todo_project/wsgi.py b/todo_project/wsgi.py new file mode 100644 index 0000000..fb01815 --- /dev/null +++ b/todo_project/wsgi.py @@ -0,0 +1,16 @@ +""" +WSGI config for todo_project project. + +It exposes the WSGI callable as a module-level variable named ``application``. + +For more information on this file, see +https://docs.djangoproject.com/en/5.1/howto/deployment/wsgi/ +""" + +import os + +from django.core.wsgi import get_wsgi_application + +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "todo_project.settings") + +application = get_wsgi_application()