An open API service indexing awesome lists of open source software.

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.

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 groups

2. Update environment variables for production:

- Set DEBUG=0
- Update DATABASE_URL with RDS credentials
- Configure allowed hosts

3. 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/admin

The 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 configuration

Docker 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