Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# FastAPI Multi-Database API Configuration
# Copy this file to .env and update with your database credentials

# API Settings
APP_NAME=FastAPI Multi-Database API
APP_VERSION=1.0.0
DEBUG=true

# MongoDB Settings
MONGODB_URL=mongodb://localhost:27017
MONGODB_DATABASE=fastapi_db

# MySQL Settings
MYSQL_HOST=localhost
MYSQL_PORT=3306
MYSQL_USER=root
MYSQL_PASSWORD=password
MYSQL_DATABASE=fastapi_db

# PostgreSQL Settings
POSTGRESQL_HOST=localhost
POSTGRESQL_PORT=5432
POSTGRESQL_USER=postgres
POSTGRESQL_PASSWORD=password
POSTGRESQL_DATABASE=fastapi_db

# Security
SECRET_KEY=your-secret-key-change-this-in-production
ALGORITHM=HS256
ACCESS_TOKEN_EXPIRE_MINUTES=30
27 changes: 27 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Changelog

## [1.0.0] - 2024-12-XX

### Added
- Initial FastAPI template with multi-database support
- MongoDB integration with Motor (async driver)
- MySQL integration with aiomysql (async driver)
- PostgreSQL integration with asyncpg (async driver)
- Comprehensive CRUD operations for all databases
- Pydantic models for data validation
- Health check endpoints for all databases
- Docker and Docker Compose configuration
- Auto-generated API documentation
- Environment-based configuration management
- Comprehensive error handling and logging
- Production-ready project structure

### Features
- **Async/Await Support**: Full asynchronous operation support
- **Multiple Databases**: MongoDB, MySQL, PostgreSQL examples
- **Auto Documentation**: Swagger UI and ReDoc integration
- **Docker Ready**: Complete containerization setup
- **Type Safety**: Full type hints with Pydantic
- **Configuration Management**: Environment variable support
- **Error Handling**: Comprehensive error responses
- **Health Monitoring**: Database connectivity checks
21 changes: 21 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
FROM python:3.11-slim

WORKDIR /app

# Install system dependencies
RUN apt-get update && apt-get install -y \
gcc \
&& rm -rf /var/lib/apt/lists/*

# Copy requirements first for better caching
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

# Copy application code
COPY . .

# Expose port
EXPOSE 8000

# Run the application
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
50 changes: 50 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
.PHONY: help install install-dev run test test-basic clean format lint docker-up docker-down

help: ## Show this help
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-20s\033[0m %s\n", $$1, $$2}'

install: ## Install production dependencies
pip install -r requirements.txt

install-dev: ## Install development dependencies
pip install -r requirements-dev.txt

run: ## Run the FastAPI application
python main.py

test-basic: ## Run basic structure validation
python test_basic.py

test: ## Run full test suite (requires dev dependencies)
pytest

format: ## Format code with black and isort
black .
isort .

lint: ## Run linting with flake8
flake8 .

clean: ## Clean up Python cache files
find . -type f -name "*.pyc" -delete
find . -type d -name "__pycache__" -delete
find . -type d -name "*.egg-info" -exec rm -rf {} +

docker-up: ## Start all services with Docker Compose
docker-compose up -d

docker-down: ## Stop all Docker services
docker-compose down

docker-logs: ## View Docker logs
docker-compose logs -f

# Database specific commands
db-mongo: ## Start only MongoDB
docker-compose up -d mongodb

db-mysql: ## Start only MySQL
docker-compose up -d mysql

db-postgres: ## Start only PostgreSQL
docker-compose up -d postgresql
51 changes: 51 additions & 0 deletions QUICKSTART.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# FastAPI Multi-Database Template

## Development Setup

### Prerequisites
- Python 3.11+
- Docker and Docker Compose (for database services)

### Quick Start
1. Clone the repository
2. Install dependencies: `pip install -r requirements.txt`
3. Copy environment variables: `cp .env.example .env`
4. Start databases: `docker-compose up -d mongodb mysql postgresql`
5. Run the application: `python main.py`

### Testing the Template
Run the basic validation test: `python test_basic.py`

### API Documentation
Once running, visit:
- http://localhost:8000/docs (Swagger UI)
- http://localhost:8000/redoc (ReDoc)

### Database Models

#### MongoDB (Users)
- Create user: `POST /mongodb/users`
- Get users: `GET /mongodb/users`
- Get user: `GET /mongodb/users/{id}`
- Update user: `PUT /mongodb/users/{id}`
- Delete user: `DELETE /mongodb/users/{id}`

#### MySQL (Products)
- Create product: `POST /mysql/products`
- Get products: `GET /mysql/products`
- Get product: `GET /mysql/products/{id}`
- Update product: `PUT /mysql/products/{id}`
- Delete product: `DELETE /mysql/products/{id}`

#### PostgreSQL (Orders)
- Create order: `POST /postgresql/orders`
- Get orders: `GET /postgresql/orders`
- Get order: `GET /postgresql/orders/{id}`
- Update order: `PUT /postgresql/orders/{id}`
- Delete order: `DELETE /postgresql/orders/{id}`

### Health Checks
- Overall: `GET /health`
- MongoDB: `GET /mongodb/health`
- MySQL: `GET /mysql/health`
- PostgreSQL: `GET /postgresql/health`
Loading