https://github.com/gitericsson/natours_django
Rebuilt Natours using Pythonβs Django Rest Framework (DRF) to improve scalability and security, while integrating advanced DevOps features. In this iteration, I introduced alternative services, addressed key challenges for seamless functionality, and implemented strong security measures with continuous integration/deployment pipelines.
https://github.com/gitericsson/natours_django
aws-ec2 aws-ecr ci-cd circleci containerization django-rest-framework docker jwt nginx postgis postgresql python3 redis
Last synced: 7 months ago
JSON representation
Rebuilt Natours using Pythonβs Django Rest Framework (DRF) to improve scalability and security, while integrating advanced DevOps features. In this iteration, I introduced alternative services, addressed key challenges for seamless functionality, and implemented strong security measures with continuous integration/deployment pipelines.
- Host: GitHub
- URL: https://github.com/gitericsson/natours_django
- Owner: gitEricsson
- Created: 2024-09-03T16:24:15.000Z (about 1 year ago)
- Default Branch: master
- Last Pushed: 2024-11-04T22:10:39.000Z (11 months ago)
- Last Synced: 2025-01-03T19:28:48.321Z (9 months ago)
- Topics: aws-ec2, aws-ecr, ci-cd, circleci, containerization, django-rest-framework, docker, jwt, nginx, postgis, postgresql, python3, redis
- Language: Python
- Homepage:
- Size: 8.16 MB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# Natours Django
Natours Django is a robust tour booking application built with Django and Docker. It provides a comprehensive backend for managing tours, bookings, user accounts, and more.
## Table of Contents
1. [π Features](#-features)
2. [π οΈ Tech Stack](#οΈ-tech-stack)
3. [π Prerequisites](#-prerequisites)
4. [π Project Structure](#-project-structure)
5. [π§ Setup and Installation](#-setup-and-installation)
6. [π Running the Application](#-running-the-application)
7. [π§ͺ Testing](#-testing)
8. [π API Documentation](#-api-documentation)
9. [π Database](#-database)
10. [π Environment Variables](#-environment-variables)
11. [π³ Docker Configuration](#-docker-configuration)
12. [π Deployment](#-deployment)
13. [π€ Contributing](#-contributing)
14. [π License](#-license)
15. [π₯ Authors](#-authors)
16. [π Acknowledgments](#-acknowledgments)
17. [π§ Contact](#-contact)## π Features
- User authentication and authorization
- Tour management with geospatial features
- Booking system
- Review system
- Appointment scheduling
- Social authentication
- Email notifications (using Brevo)
- API documentation with Swagger## π οΈ Tech Stack
- **Backend**: Django, Django REST Framework
- **Database**: PostgreSQL with PostGIS extension, RDS
- **Containerization**: Docker
- **Caching**: Redis
- **Task Queue**: Celery
- **Reverse Proxy**: Nginx
- **Cloud Platform**: AWS (ECR & EC2)
- **Continous Integration/Continous Deployment(CI/CD)**: CircleCI
- **Authentication**: JWT## π Prerequisites
- Docker and Docker Compose
- Python 3.11+
- PostgreSQL with PostGIS
- GDAL library
- AWS Account with appropriate permissions
- AWS CLI installed and configured
- Domain name (optional, but recommended for production)## π Project Structure
```
natours_django/
βββ appointments/ # Tour appointment management
βββ bookings/ # Booking logic and models
βββ dev-data/ # Development data and loaders
βββ reviews/ # Tour review system
βββ social_auth/ # Social authentication
βββ tours/ # Tour management and geospatial features
βββ users/ # User management
βββ Natours_Django/ # Main project settings
βββ nginx/ # Nginx configuration for production
βββ docker-compose.yml # Docker Compose configuration
βββ Dockerfile # Docker configuration for development
βββ Dockerfile.prod # Docker configuration for production
βββ requirements.txt # Python dependencies
βββ manage.py # Django management script
```## π§ Setup and Installation
1. Clone the repository:
```
git clone https://github.com/your-username/natours-django.git
cd natours-django
```2. Create environment files:
Create `.env`:
```
SECRET_KEY=your_secret_key
DJANGO_ALLOWED_HOSTS=localhost 127.0.0.1 [::1]
SQL_ENGINE=django.contrib.gis.db.backends.postgis
SQL_DATABASE=natours_dev
SQL_USER=natours_user
SQL_PASSWORD=natours_password
SQL_HOST=db
SQL_PORT=5432
DATABASE=postgres
DJANGO_INITIALIZE_DATA=true
```Create `.env.db`:
```
POSTGRES_USER=natours_user
POSTGRES_PASSWORD=natours_password
POSTGRES_DB=natours_dev
```3. Build and run with Docker:
```
docker-compose up --build
```4. Initialize the database (first time only):
```
docker-compose exec web python manage.py migrate
docker-compose exec web python dev-data/data/data_loader.py --import
docker-compose exec web python dev-data/data/data_loader.py --importDates
```## π Running the Application
1. Set up AWS RDS with PostGIS:
- Create PostgreSQL instance
- Enable PostGIS extension
- Configure security groups2. Update environment variables for production:
- Set DEBUG=0
- Update DATABASE_URL with RDS credentials
- Configure allowed hosts3. Deploy using Docker:
- For development:
```
docker-compose -f docker-compose.dev.yml up --build
```- For production:
```
docker-compose -f docker-compose.prod.yml up --build
```4. Access development server:
- Main application: http://localhost:8000
- Admin interface: http://localhost:8000/adminThe application will be available at `http://localhost:8000` for development and `http://localhost:80` for production.
## π§ͺ Testing
Run tests using:
```bash
docker-compose exec web python manage.py test
```## π API Documentation
API documentation is available using Swagger. After running the application, visit:
```
http://localhost:8000/swagger/
```## π Database
The project uses PostgreSQL with PostGIS extension for geospatial features. The database configuration can be found in the `DATABASES` setting in `settings.py`:
```133:142:Natours_Django/settings.py
DATABASES = {
'default': {
'ENGINE': 'django.contrib.gis.db.backends.postgis',
'NAME': os.environ.get('SQL_DATABASE', 'Natours_Django'),
'USER': os.environ.get('SQL_USER', 'postgres'),
'PASSWORD': os.environ.get('SQL_PASSWORD', ''),
'HOST': os.environ.get('SQL_HOST', 'localhost'),
'PORT': os.environ.get('SQL_PORT', '5432'),
}
}
```## π Environment Variables
Key environment variables are stored in the `.env` and `.env.db` files. Make sure to keep these files secure and never commit them to version control.
## π³ Docker Configuration
The project includes separate Dockerfiles for development and production:
- `Dockerfile`: Development configuration
- `Dockerfile.prod`: Production configurationDocker Compose files:
- `docker-compose.dev.yml`: Development setup
- `docker-compose.prod.yml`: Production setup## π Deployment
For AWS production deployment:
1. Update the `DJANGO_ALLOWED_HOSTS` in your `.env` file with your domain.
2. Build the Docker images:
```bash
docker-compose -f docker-compose.staging.yml build
```3. Log in to AWS ECR repository:
```bash
aws ecr get-login-password --region | docker login --username AWS --password-stdin .dkr.ecr..amazonaws.com
```4. Tag and Push images to ECR:
```bash
docker tag natours-django_web:latest .dkr.ecr..amazonaws.com/natours-django:web
docker tag natours-django_nginx:latest .dkr.ecr..amazonaws.com/natours-django:nginx
docker push .dkr.ecr..amazonaws.com/natours-django:web
docker push .dkr.ecr..amazonaws.com/natours-django:nginx
```5. On your EC2 instance, copy required files:
```bash
scp -i /path/to/your/key.pem \
-r $(pwd)/{app,nginx,.env.staging,.env.staging.proxy-companion,docker-compose.staging.yml} \
ubuntu@:/path/to/project
```6. SSH into your EC2 instance:
```bash
ssh -i /path/to/your/key.pem ubuntu@
cd /path/to/project
```7. Log in to ECR and pull images:
```bash
aws ecr get-login-password --region | docker login --username AWS --password-stdin .dkr.ecr..amazonaws.com
docker pull .dkr.ecr..amazonaws.com/django-ec2:web
docker pull .dkr.ecr..amazonaws.com/django-ec2:nginx-proxy
```8. Run the containers:
```bash
docker-compose -f docker-compose.staging.yml up -d
```Note: When first accessing your domain, you may see a certificate security warning. This is expected if using a staging certificate. Click "Advanced" and then "Proceed" to access your application.
## π€ Contributing
1. Fork the repository
2. Create your feature branch (`git checkout -b feature/AmazingFeature`)
3. Commit your changes (`git commit -m 'Add some AmazingFeature'`)
4. Push to the branch (`git push origin feature/AmazingFeature`)
5. Open a Pull Request## π License
This project is licensed under the MIT License.
## π₯ Authors
- Ericsson Raphael - Initial work
## π Acknowledgments
- Natours project inspiration
## π§ Contact
For questions and support, please email: ericssonraphael@gmail.com