Skip to content

Commit

Permalink
Setup the backend for local development (#4)
Browse files Browse the repository at this point in the history
* Initialize DRF project and app

test

* Add a health api

* Install ruff for linting for formatting

* Format code and fix lint errors

* Fix directory structure for todo app

Move todo app outside todo_project directory

* Add ruff pre-commit hook

* Configure env wise settings

* Setup MongoDB and dockerize the app

* Add a check for mongo connection

* Update readme for windows commands

* Remove the admin.py file

Not required since we are not using admin panel

* Removed the settings not relevant to our usecases

* Add volume to persist mongo data across restarts

* Remove redundant create mongo client method

* Remove dummy-variable-rgx ruff config

Not required

* Remove slash from health url

Endpoints don't have / at the end

* Remove all the __init__.py files

Not required

* Remove error hint in MongoDB check

* Setup file logging

* Log mongo check's success and failures

* Pick django secret key from env variables

* Add a sample test for health api

* Add github workflow to check linting and run tests

* Remove empty models.py file

* Remove database connection check

* Return database status in health API

* Change line length to 120 in lint config

* Refactor and add tests for database config

* Add success case for db health check util

* Return 503 status code from health api if DB is down

* Move configure settings logic to settings module

* Rename HealthStatus enum and it's values

* Remove comments from settings and gitignore file

* Remove mongo timeout configs

* Remove logging config

Will create a separate ticket for it

* Remove DJANGO prefix from env variable names

* Omit files from coverage testing

* Refactor database config to a class

* Remove timeout config from github workflow

* Pick allowed hosts from env variable

* Update test and lint commands in readme

* Rename health view tests

* Rename settings config tests

* Rename database manager tests

* Add comment in empty init files

* Rename check db health method

* Remove the pre-commit hook

* Remove unused format argument in health view

* Refactor database manager tests

- Add test for singleton behaviour
- Remove unused settings mock
- Use assertIs to compare object reference

* Remove redundant comments

* Refactor method name in DatabaseManager

* Rename health test file

* Skip manage.py from coverage tests

This file is generated by Django

* Rename settings configure test file

* Add tests for get collection method
  • Loading branch information
samarpan1738 authored Nov 22, 2024
1 parent 434f9cb commit 672b64f
Show file tree
Hide file tree
Showing 30 changed files with 761 additions and 2 deletions.
6 changes: 6 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -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'
41 changes: 41 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -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
107 changes: 107 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions .python-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.12.7
17 changes: 17 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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/
98 changes: 96 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
```
24 changes: 24 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -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
25 changes: 25 additions & 0 deletions manage.py
Original file line number Diff line number Diff line change
@@ -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()
Loading

0 comments on commit 672b64f

Please sign in to comment.