/HealthNutrition-Backend

Django / DRF based app for comparing the nutritional value of products.

Primary LanguagePythonApache License 2.0Apache-2.0

Health Nutrition - Backend

Django / DRF based app for comparing the nutritional value of products.

Python Django Rest-framework Poetry Pytest Black

📖 Table of contents

📃 Description

This Django application provides a comprehensive API for comparing nutritional values of food products, aiding users in making informed dietary decisions.

This API empowers developers to integrate nutritional comparison functionality into various applications, enabling users to search for food items, explore nutritional categories, view detailed product information, and ultimately make health-conscious dietary choices. It simplifies the process of comparing nutritional values across different food items, enhancing the user's experience with meal planning and supporting them in achieving their nutritional goals.

Note

The project was created for educational purposes, simulating fictitious products without real value.

🌄 Demonstration

Product categories

Aminations

compared-products-deleting comparison-group-deleting

Products

Product detail

firefox_9HVqv9g46p firefox_hrfbO4yzoT

Comparison groups

firefox_TgwCqy76Wf

Compared products

Profile

Settings

firefox_TwCMDgqiKa

Authorization

Responsive design

🔥 Features

  • RESTful API
  • Domain Driven Design
  • CI/CD
  • Comments reply / Tree structure (Django MPTT)
  • Custom objects ordering (Like Drag & Drop sort)
  • Celery / Postponed Tasks
  • Email Verification
  • JWT Authentication / Authorization
  • Code Documentation
  • Tests (PyTest)

❕ Peculiarities

Architecture:

  • Project services are divided into 2 levels:
    • Domain - Services that are in no way dependent on the current infrastructure, that is, the framework.
    • Infrastructure - Services that can call domain services and interact with the project infrastructure.

Abbreviations:

  • EV - EmailVerification

💽 Local installation

  1. Clone or download the repository.

  2. Activate the Poetry virtual environment: poetry shell

  3. Install dependencies: poetry install

  4. Create an .env file or rename .env.dist in .env and populate it only with development variables:

    Env-Variables-Example
  5. Run docker services for development: docker-compose -f docker/local/docker-compose.yml up

  6. Apply migrations: python manage.py makemigrations and python manage.py migrate

  7. Run the development server: python manage.py runserver

🐳 Deployment on a server

Initial Deployment:

  1. Clone or download the repository and go to its directory.

  2. Create an .env file or rename .env.dist in .env and populate it with all variables from .env.dist file.

  3. Create a nginx.conf file in the docker/production/nginx/conf.d/ directory and fill it with the code below:

    upstream core {
        server django-gunicorn:8000;
    }
    
    server {
        listen 80;
        server_name example.com www.example.com;
        server_tokens off;
    
        client_max_body_size 20M;
    
        location = /favicon.ico { access_log off; log_not_found off; }
    
        location /static/ {
            alias /opt/HealthNutrition-Backend/static/;
        }
    
        location /media/ {
            alias /opt/HealthNutrition-Backend/media/;
        }
    
        location /.well-known/acme-challenge/ {
            root /var/www/certbot/;
        }
    
        location / {
            proxy_pass http://core;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header Host $host;
                proxy_redirect off;
        }
    }

Change example.com and www.example.com to your domains.

  1. Grant executable rights to the entrypoint.sh and celery_entrypoint.sh script: chmod +x docker/production/entrypoint.sh && chmod +x docker/celery_entrypoint.sh

  2. Start the services: docker-compose -f docker/local/docker-compose.yaml -f docker/production/docker-compose.yaml up -d

Obtaining an ssl certificate:

  1. Access nginx container: docker exec -it <nginx-container-id> bin/sh

  2. Get ssl certificate: certbot --nginx

  3. Done ! Now you can exit from nginx container: exit

Configure CI/CD:

  1. Generate ssh keys in your local computer: ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
  2. Copy the content of your private and public keys to clipboard:
    • Linux: Copy the result of commands:
      • cat ~/.ssh/id_rsa
      • cat ~/.ssh/id_rsa.pub
    • Windows: Copy the contents of the files:
      • C:/users/user/.ssh/id_rsa
      • C:/users/user/.ssh/id_rsa.pub
  3. Create GitHub repository secrets:
    • SSH_HOST: Your remote server host / IP.
    • SSH_LOGIN: Your remote server login / username.
    • SSH_PORT: Your remote server port.
    • SSH_PRIVATE_KEY: Copied SSH private key.
  4. Access your remote host and add your public key there:
    • Execute nano ~/.ssh/authorized_keys and paste your copied public key to the next line.

⚒️ Testing

  1. Complete all the steps in the 💽 Local installation section

  2. Run tests: pytest .

🪝 Pre-Commit hooks

  1. Install: pre-commit install

  2. Check: pre-commit run --all-files