A production-ready REST microservice for managing customers and orders with MongoDB integration, built with Python and FastAPI principles. Features comprehensive API endpoints, Docker containerization, and CI/CD workflows.
ZEKA REST Microservice is a scalable backend solution designed to handle customer and order management operations. It demonstrates best practices in microservice architecture, including:
- RESTful API Design: Standard HTTP methods for CRUD operations
- Database Integration: MongoDB with connection pooling
- Containerization: Docker and Docker Compose support
- Testing: Comprehensive test suite with pytest
- CI/CD Pipeline: GitHub Actions workflow for automated testing
- Environment Management: Environment variable configuration
| Component | Technology | Version |
|---|---|---|
| Runtime | Python | 3.8+ |
| Database | MongoDB | 4.0+ |
| Web Framework | Flask/FastAPI-like | - |
| Containerization | Docker | Latest |
| Testing | Pytest | Latest |
| CI/CD | GitHub Actions | - |
β Customer Management
- Create new customers (name, email)
- Retrieve all customers
- Customer validation and error handling
β Order Management
- Create orders for customers
- Track items and amounts
- Link orders to customers
- Order history tracking
β Infrastructure
- Docker containerization for easy deployment
- Docker Compose for local development
- Environment configuration via .env files
- Automated CI/CD workflows
β Testing & Quality
- Unit tests with pytest
- Test isolation and fixtures
- Continuous Integration pipeline
- Python 3.8+
- MongoDB (local or Atlas)
- pip (Python package manager)
- Docker (optional, for containerization)
git clone https://github.com/Vinay0905/REST_microservice.git
cd REST_microserviceCreate a .env file in the project root:
MONGODB_URI="mongodb+srv://<user>:<pass>@zeka.7ijqrc1.mongodb.net/?retryWrites=true&w=majority"
MONGODB_DB="ZEKA"
PYTHON_ENV="development"
PORT=8000# Create virtual environment
python -m venv venv
# Activate virtual environment
# On Windows:
venv\Scripts\activate
# On macOS/Linux:
source venv/bin/activate
# Install dependencies
pip install -r requirements.txtpython run.pyThe server will start at http://localhost:8000
# Build and start services
docker-compose up -d
# View logs
docker-compose logs -f app
# Stop services
docker-compose down# Build image
docker build -t zeka-rest-api .
# Run container
docker run -p 8000:8000 --env-file .env zeka-rest-apihttp://localhost:8000/api
Create Customer
POST /api/customers
Content-Type: application/json
{
"name": "John Doe",
"email": "john@example.com"
}Response (201 Created)
{
"_id": "507f1f77bcf86cd799439011",
"name": "John Doe",
"email": "john@example.com",
"created_at": "2024-01-15T10:30:00Z"
}Get All Customers
GET /api/customersResponse (200 OK)
[
{
"_id": "507f1f77bcf86cd799439011",
"name": "John Doe",
"email": "john@example.com",
"created_at": "2024-01-15T10:30:00Z"
}
]Create Order
POST /api/orders
Content-Type: application/json
{
"customer_id": "507f1f77bcf86cd799439011",
"item": "Laptop",
"amount": 999.99
}Response (201 Created)
{
"_id": "507f1f77bcf86cd799439012",
"customer_id": "507f1f77bcf86cd799439011",
"item": "Laptop",
"amount": 999.99,
"created_at": "2024-01-15T11:00:00Z"
}Run the test suite:
# Run all tests
pytest
# Run with verbose output
pytest -v
# Run specific test file
pytest tests/test_customers.py
# Run with coverage report
pytest --cov=appTest files are located in the tests/ directory.
REST_microservice/
βββ app/ # Application code
β βββ __init__.py
β βββ models.py # Data models
β βββ routes.py # API endpoints
β βββ database.py # Database configuration
βββ tests/ # Test suite
β βββ test_customers.py
β βββ test_orders.py
β βββ conftest.py # Pytest fixtures
βββ .github/
β βββ workflows/ # CI/CD workflows
β βββ tests.yml # Automated testing pipeline
βββ .env.example # Environment template
βββ .dockerignore # Docker ignore rules
βββ .gitignore # Git ignore rules
βββ Dockerfile # Docker image definition
βββ docker-compose.yml # Docker Compose configuration
βββ requirements.txt # Python dependencies
βββ pytest.ini # Pytest configuration
βββ run.py # Application entry point
βββ README.md # This file
| Variable | Description | Required |
|---|---|---|
MONGODB_URI |
MongoDB connection string | Yes |
MONGODB_DB |
Database name | Yes |
PYTHON_ENV |
Environment (development/production) | No |
PORT |
Server port | No (default: 8000) |
LOG_LEVEL |
Logging level | No (default: INFO) |
- Create a MongoDB Atlas account at mongodb.com/atlas
- Create a cluster
- Get connection string:
mongodb+srv://<username>:<password>@<cluster>.mongodb.net/?retryWrites=true&w=majority - Add to
.envfile
The project includes GitHub Actions workflows for:
- Automated Testing: Runs on every push and pull request
- Code Quality: Checks for code standards
- Docker Build: Builds and pushes Docker images (on release)
Workflow file: .github/workflows/tests.yml
Key dependencies in requirements.txt:
Flask/FastAPI-like framework
PyMongo - MongoDB driver
pytest - Testing framework
python-dotenv - Environment configuration
requests - HTTP client
View full list: pip freeze
Problem: MongoAuthenticationError
Solution:
- Verify MongoDB URI in
.envfile - Check MongoDB credentials
- Ensure IP is whitelisted in MongoDB Atlas
- Verify network connectivity
Problem: Address already in use on port 8000
Solution:
# Kill process using port 8000
# On macOS/Linux:
lsof -ti:8000 | xargs kill -9
# On Windows (PowerShell):
netstat -ano | findstr :8000
taskkill /PID <PID> /FProblem: No module named 'app'
Solution:
# Ensure venv is activated
source venv/bin/activate # macOS/Linux
venv\Scripts\activate # Windows
# Reinstall requirements
pip install -r requirements.txtContributions are welcome! Please follow these steps:
- Fork the repository
- Create a feature branch:
git checkout -b feature/your-feature - Commit changes:
git commit -am 'Add new feature' - Push to branch:
git push origin feature/your-feature - Submit a pull request
- Follow PEP 8 Python style guide
- Write tests for new features
- Update documentation
- Keep commits clear and descriptive
This project is licensed under the MIT License - see the LICENSE file for details.
Vinay0905
- GitHub: @Vinay0905
- Focus: Backend Development, Microservices, Python
- MongoDB documentation and community
- Python community best practices
- GitHub Actions for CI/CD
- Contributors and testers
For issues or questions:
- Check existing GitHub Issues
- Create a new issue with detailed information
- Include steps to reproduce for bugs
- Provide environment details
- Add authentication (JWT)
- Implement pagination
- Add logging
- Request validation schemas
- GraphQL endpoint
- Redis caching
- Advanced filtering
- API rate limiting
- Kubernetes deployment
- Message queue integration
- Analytics dashboard
- Payment processing
Happy coding! π