A FastAPI-based wrapper for the Rick and Morty API with additional filtering capabilities and caching.
- Additional character filtering with combined filters
- Redis caching for improved performance
- Server-side pagination
- Comprehensive error handling
- Full test coverage
- Containerized deployment to AWS Lambda
- FastAPI: Modern Python web framework
- Redis: In-memory caching
- aiohttp: Async HTTP client for API calls
- pytest: Testing framework
- Docker: Containerization
- AWS Lambda: Serverless deployment
- Amazon ECR: Container registry
rick-morty-backend/
├── src/
│ ├── main.py # FastAPI application entry point
│ ├── api.py # Rick and Morty API client
│ ├── cache.py # Redis cache implementation
│ └── config.py # Configuration settings
├── tests/
│ ├── test_api.py # API client tests
│ ├── test_cache.py # Cache layer tests
│ └── test_main.py # Endpoint tests
├── deploy_ecr.sh # AWS ECR deployment script
├── Dockerfile # Container definition
├── lambda_function.py # AWS Lambda handler
└── pytest.ini # Pytest configuration
- Python 3.9+
- Redis
- Docker
- AWS CLI configured with appropriate permissions
- Clone the repository
git clone https://github.com/aghwotu/rick-morty-backend.git
cd rick-morty-backend- Create and activate virtual environment
# Create virtual environment
python3 -m venv venv
# Activate virtual environment
# On macOS/Linux:
source venv/bin/activate
# On Windows:
.\venv\Scripts\activate- Install dependencies
pip install -r requirements.txt- Copy environment template and update variables
cp .env.example .env
# Update .env with your configurations- Set up Redis
Redis needs to be installed and running on your system:
# On macOS using Homebrew:
brew services start redis
# On Linux (Ubuntu/Debian):
sudo service redis-server start
# On Windows:
# Redis for Windows requires Windows Subsystem for Linux (WSL) or Docker
# Please refer to Redis documentation for Windows installationNote: For detailed Redis installation instructions for your platform, visit the Redis documentation.
Run the application locally:
uvicorn src.main:app --reloadpytest -v- AWS CLI installed and configured
- Docker installed and running
- Appropriate AWS permissions for ECR and Lambda
- Make the deployment script executable:
chmod +x deploy_ecr.sh- Run the deployment script:
./deploy_ecr.shThis will:
- Create an ECR repository if it doesn't exist
- Build the Docker image
- Push the image to ECR
The API documentation is available at the following endpoints:
- Swagger UI:
/docs - OpenAPI Schema:
/openapi.json
Example:
If your API is deployed at https://api.example.com/prod, the Swagger UI would be available at https://api.example.com/prod/docs
GET /health: Health checkGET /characters: Get characters with filteringGET /cache/stats: Get cache statisticsPOST /cache/invalidate: Invalidate cache
- Uses async/await for non-blocking operations
- Implements caching with 1-hour expiration
- Handles Redis connection failures gracefully
- Complete test coverage with mocked dependencies
- AWS Lambda deployment ready
This project is licensed under the MIT License - see the LICENSE file for details.