A Django REST API for efficiently managing customers and orders, featuring OpenID Connect authentication, SMS notifications for order updates, comprehensive unit testing, and seamless CI/CD integration.
- 🎯 Features
- 🛠️ Technologies
- 🗂 Database Schema
- 📦 Installation
- 📡 Endpoints
- 📊 Swagger UI
- 📖 Usage
- 🧪 Testing
- 📜 Logging Setup
- 🌐 Nginx Configuration
- ⚙️ CI/CD Setup
- 🔄 CI/CD Configuration
- 🚀 Deployment
- 📜 License
- 🔐 Google OAuth2 Login: Users can authenticate using their Google accounts.
- 👥 Customer Management: Easily manage customer records.
- 📦 Order Management: Track and manage orders.
- 📲 SMS Notifications: Send notifications via SMS API (Africa's Talking) when orders are created.
- Framework: Django 🦄
- API: Django REST Framework ⚙️
- Database: PostgreSQL 🗄
- Containerization: Docker 🐳
- CI/CD: GitHub Actions 🔄
- Provisioning: Ansible 📜
- Code Coverage: Codecov 📊
Below is the database schema for the project, showing the relationships between the User
, Customer
, and Order
tables:
🗂 Database Schema
Below is the database schema for the project, showing the relationships between the User
, Customer
, and Order
tables.
- 🐳 Docker: Ensure that Docker is installed and running on your machine. For installation instructions, visit the Docker installation guide.
-
Clone the Repository:
git clone https://github.com/Laban254/customer-management-api.git
cd customer-management-api
-
Create Environment Variables: Create a
.env
file in the project root and add the necessary variables:GOOGLE_CLIENT_ID=your_google_client_id GOOGLE_CLIENT_SECRET=your_google_client_secret DATABASE_URL=your_database_url AFRICAS_TALKING_USERNAME=your_africas_talking_username AFRICAS_TALKING_API_KEY=your_africas_talking_api_key DEBUG=False ALLOWED_HOSTS=your_allowed_hosts
-
Run the Application with Docker:
docker-compose up --build
-
Run Database Migrations:
docker-compose exec web python manage.py migrate
-
Create a Superuser (Optional):
docker-compose exec web python manage.py createsuperuser
-
Access the Application: Open your browser and navigate to http://127.0.0.1:8000.
GET /auth/google/login/
- Initiates the Google OAuth2 login flow.GET /auth/google/callback/
- Handles the Google OAuth2 callback and exchanges the authorization code for tokens.POST /auth/token/refresh/
- Refreshes the access token using the refresh token.POST /auth/token/
- Exchanges credentials for a new access token..GET /api/customers/
- Retrieves the list of customers associated with the authenticated user.POST /api/customers/
- Creates a new customer associated with the authenticated user.GET /api/orders/
- Retrieves the list of orders associated with the authenticated user.POST /api/orders/
- Creates a new order and sends an SMS notification.
The application provides a Swagger UI for API documentation. You can access it at:
- Swagger UI: http://localhost:8000/swagger-ui/
- ReDoc: http://localhost:8000/redoc/
Follow these steps to use the application:
-
Initiate Google Login: To start the Google login process, open your web browser and navigate to the following URL:
GET /auth/google/login/
This action will redirect you to Google’s authentication page, where you can log in with your Google account. -
Authenticate: Log in using your Google account. After authentication, you will be redirected to the callback URL with an authorization code.
-
Access Token: The response will include both an access token and a refresh token. The access token is used to authorize requests to secured endpoints, while the refresh token can be used to obtain a new access token when the current one expires.
-
Authorize Requests:
- To use the access token in Swagger UI, locate the endpoints requiring authentication (e.g., /api/customers/, /api/orders/) and click "Authorize".
- Enter the access token in the "Value" field and click "Authorize".
-
Using Other Tools: You can also leverage the access token with tools like Postman to access the secured endpoints.
Ensure everything works as expected by running the tests defined within your Django application. To execute tests and check code coverage, run:
pytest --cov=your_app_name
Ensure that your test environment is properly configured and that the database is available.
The Django application is configured to log messages to both the console and a log file. The logging configuration is defined in the settings.py
file.
📝 Logs will be written to myAppLogs.log, and console logs will be visible in the Docker logs.
Nginx is configured to serve the Django application and handle static files. The Nginx configuration file is located at nginx/nginx.conf.
📂 Access logs are stored in /var/log/nginx/mydjangoapp_access.log.
To configure your AWS EC2 instance, follow these steps:
- Configure AWS EC2:
- Ensure your EC2 instance is running.
- Update the Ansible inventory file located at
ansible/inventory/hosts.ini
with your server's IP address. - Modify the Ansible playbook at
ansible/playbooks/initialSetup.yml
as necessary to suit your configuration.
- Trigger the Initial Setup Workflow:
- In GitHub Actions, trigger the
initial-setup.yml
workflow to automatically configure your EC2 instance.
- In GitHub Actions, trigger the
-
Continuous Integration (CI): Managed through GitHub Actions, this automatically runs tests for every pull request using the
ci.yml
workflow to ensure code quality. -
Continuous Deployment (CD): A separate workflow,
cd.yml
is responsible for deploying your application whenever changes are pushed to the specified branch. -
Repository Secrets: Set up the following GitHub secrets to enable seamless CI/CD operations: .env.sample
AFRICAS_TALKING_API_KEY
AFRICAS_TALKING_USERNAME
ANSIBLE_USER
CODECOV_TOKEN
DATABASE_URL
EC2_PRIVATE_KEY
EC2_USER
GOOGLE_CLIENT_ID
GOOGLE_CLIENT_SECRET
SERVER_IP
The application is automatically deployed to your server through the cd.yml
workflow upon pushing changes to the specified branch.
This project is licensed under the MIT License.