HealthNutrition-Backend
HealthNutrition-Backend copied to clipboard
Django / DRF based app for comparing the nutritional value of products.
📖 Table of contents
- Description
- Demonstration
- Features
- Local installation
- Deployment on a server
- Pre-Commit hooks
📃 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.
Frontend part: https://github.com/FCTL3314/StoreTracker-Frontend
🌄 Demonstration
Product categories
Aminations
Products
Product detail
Comparison groups
Compared products
Profile
Settings
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
-
Clone or download the repository.
-
Activate the Poetry virtual environment:
poetry shell -
Install dependencies:
poetry install -
Create an .env file or rename .env.dist in .env and populate it only with development variables:
-
Run docker services for development:
docker-compose -f docker/local/docker-compose.yaml up -
Apply migrations:
python manage.py makemigrationsandpython manage.py migrate -
Run the development server:
python manage.py runserver
🐳 Deployment on a server
Initial Deployment:
-
Clone or download the repository and go to its directory.
-
Create an .env file or rename .env.dist in .env and populate it with all variables from .env.dist file.
-
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.
-
Grant executable rights to the entrypoint.sh and celery_entrypoint.sh script:
chmod +x docker/production/entrypoint.sh && chmod +x docker/celery_entrypoint.sh -
Start the services:
docker-compose -f docker/local/docker-compose.yaml -f docker/production/docker-compose.yaml up -d
Obtaining an ssl certificate:
-
Access nginx container:
docker exec -it <nginx-container-id> bin/sh -
Get ssl certificate:
certbot --nginx -
Done ! Now you can exit from nginx container:
exit
Configure CI/CD:
- Generate ssh keys in your local computer:
ssh-keygen -t rsa -b 4096 -C "[email protected]" - Copy the content of your private and public keys to clipboard:
- Linux: Copy the result of commands:
cat ~/.ssh/id_rsacat ~/.ssh/id_rsa.pub
- Windows: Copy the contents of the files:
C:/users/user/.ssh/id_rsaC:/users/user/.ssh/id_rsa.pub
- Linux: Copy the result of commands:
- 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.
- Access your remote host and add your public key there:
- Execute
nano ~/.ssh/authorized_keysand paste your copied public key to the next line.
- Execute
⚒️ Testing
-
Complete all the steps in the 💽 Local installation section
-
Run tests:
pytest .
🪝 Pre-Commit hooks
-
Install:
pre-commit install -
Check:
pre-commit run --all-files