A production-ready FastAPI REST template with streamlined API design, comprehensive server management, enterprise-grade security, and plug-and-play architecture. Built with clean architecture principles, featuring 8 focused endpoints, dynamic endpoint discovery, and enterprise-grade configuration handling.
- π Streamlined API: 8 production-ready endpoints with clear utility and purpose
- π§ Advanced Configuration: YAML-based configuration with environment variable overrides and validation
- π Process Management: Both stateful and stateless process management with automatic cleanup
- ποΈ Server Management: Comprehensive server lifecycle with graceful shutdown and signal handling
- π Structured Logging: Advanced logging with file rotation and configurable levels
- π Enterprise Security: JWT authentication, API key management, hybrid auth, rate limiting, and CORS configuration
- π‘οΈ Auto-Protection Middleware: Automatically secures endpoints based on configuration
- π Multiple Auth Methods: JWT Bearer tokens, API Keys, and Hybrid authentication support
- π File Management: Intelligent file tracking and cleanup with orphaned file detection
- π― Clean Architecture: Modular design with clear separation of concerns
- β‘ Performance Monitoring: Built-in metrics and health checks
- π Plugin Architecture: Ready for database, monitoring, and cloud service plugins
- π οΈ Development Tools: Comprehensive testing setup and development utilities
- Quick Start - Get up and running in minutes
- Project Structure - Understanding the codebase
- Configuration - Comprehensive configuration guide
- API Documentation - Production-ready API endpoints
- Security & Authentication - Enterprise-grade security system
- Advanced Usage - Custom services and process management
- Environment Variables - Configuration reference
- Production Deployment - Production considerations
- Monitoring & Metrics - Built-in monitoring capabilities
- Testing - Comprehensive testing guide
- Troubleshooting - Common issues and solutions
- Contributing - How to contribute to the project
- Security - Security policy and best practices
- License - MIT License information
- Acknowledgments - Credits and inspiration
- .github/CONTRIBUTING.md - Detailed contribution guidelines
- .github/CODE_OF_CONDUCT.md - Community standards
- .github/SECURITY.md - Security policy and reporting
# Clone the repository
git clone <your-repo-url>
cd template-fastapi-rest
# Install dependencies using uv (recommended)
uv sync
# Or using pip
pip install -e .Create a .env file with your configuration:
# Application Configuration
ENVIRONMENT=development
APP_DEBUG=true
APP_NAME="My FastAPI Application"
# Server Configuration
SERVER_HOST=0.0.0.0
SERVER_PORT=8000
SERVER_RELOAD=true
# Security Configuration (Required for authentication)
JWT_SECRET_KEY=your_super_secret_jwt_key_change_this_in_production
# API Keys (for external services)
OPENAI_API_KEY=your_openai_key
ANTHROPIC_API_KEY=your_anthropic_key
GOOGLE_API_KEY=your_google_key
CUSTOM_API_KEY=your_custom_key
# Logging Configuration
LOG_LEVEL=INFOThe application uses a sophisticated configuration system with YAML files and environment variable overrides:
# src/utils/config/config.yaml
app:
name: "fastapi-rest-template"
version: "1.0.0"
description: "FastAPI AI Server with AI Processing"
debug: false
environment: "development"
# API Configuration (universal for the whole app)
api_prefix: "/api/v1"
api_tags: ["API"]
server:
host: "0.0.0.0"
port: 8000
reload: false
workers: 1
# Security Configuration
security:
enabled: true
auth_type: "jwt" # jwt, api_key, hybrid, none
jwt_secret_key: "${JWT_SECRET_KEY}"
jwt_algorithm: "HS256"
jwt_access_token_expire_minutes: 30
jwt_refresh_token_expire_days: 7
rate_limit_enabled: true
rate_limit_requests_per_minute: 60
excluded_paths: ["/docs", "/health", "/metrics"]
protected_paths: ["/api/v1"]
# Process Manager Configuration
process_manager:
type: "stateful" # stateful or stateless
cleanup:
enabled: true
interval: 300 # 5 minutes
process_ttl: 1800 # 30 minutes
# Server Manager Configuration
server_manager:
app:
name: "FastAPI AI Server"
version: "1.0.0"
housekeeping:
enabled: true
interval: 600 # 10 minutes
services:
# Core API services
item_service:
enabled: true
config:
max_items: 10000
auto_cleanup: true
document_service:
enabled: true
config:
max_file_size: 104857600 # 100MB
allowed_extensions: [".pdf", ".doc", ".docx", ".txt", ".md", ".jpg", ".png"]# Start the FastAPI server
python app.py
# Or using uv
uv run python app.py
# The server will start on http://localhost:8000
# Visit http://localhost:8000/docs for interactive API documentation
# Visit http://localhost:8000/ for comprehensive template informationtemplate-fastapi-rest/
βββ app.py # FastAPI application entry point
βββ pyproject.toml # Project configuration and dependencies
βββ README.md # This file
βββ LICENSE # MIT License
βββ docs/ # Documentation
β βββ AUTH.md # Comprehensive security documentation
βββ src/
β βββ api/ # Streamlined API layer (8 endpoints)
β β βββ routes.py # Production-ready REST endpoints
β β βββ handlers.py # Request handlers with session tracking
β β βββ models.py # Essential data models (5 models)
β βββ core/ # Core application engine
β β βββ server_manager.py # Server lifecycle management
β β βββ process_manager.py # Process orchestration
β β βββ managers.py # Manager utilities
β βββ services/ # Business logic services
β β βββ services.py # Generic service implementation
β βββ utils/ # Utilities and helpers
β βββ config/ # Configuration management
β β βββ config.yaml # Default configuration
β β βββ settings.py # Settings manager
β β βββ modules/ # Configuration modules
β βββ auth/ # Enterprise-grade security module
β β βββ __init__.py # Security manager and factory
β β βββ auth_factory.py # Authentication factory
β β βββ middleware.py # Auto-protection middleware
β β βββ models.py # Security models and types
β β βββ example_integration.py # Integration examples
β β βββ test_security.py # Security testing utilities
β β βββ handlers/ # Authentication handlers
β β βββ __init__.py
β β βββ jwt_handler.py # JWT Bearer token auth
β β βββ api_key_handler.py # API Key authentication
β β βββ hybrid_handler.py # Hybrid JWT + API Key auth
β βββ resources/ # Resource management
β β βββ logger.py # Logging configuration
β β βββ downloader.py # Resource downloader
β β βββ helper.py # Helper utilities
β βββ io/ # Input/Output utilities
β βββ reader.py # File readers
β βββ writer.py # File writers
βββ runtime/ # Application data directory
β βββ temp/ # Temporary files
β βββ outputs/ # Output files
β βββ logs/ # Log files
β βββ models/ # AI models
β βββ uploads/ # Uploaded files
β βββ assets/ # Static assets
βββ tests/ # Test suite
β βββ conftest.py # Test configuration
β βββ unit/ # Unit tests
β βββ integration/ # Integration tests
β βββ e2e/ # End-to-end tests
βββ .github/ # GitHub templates and workflows
βββ CONTRIBUTING.md # Contribution guidelines
βββ CODE_OF_CONDUCT.md # Community standards
βββ SECURITY.md # Security policy
The template includes a comprehensive, enterprise-grade security system with multiple authentication methods and automatic endpoint protection.
- π Multiple Authentication Methods: JWT Bearer tokens, API Keys, and Hybrid authentication
- π‘οΈ Auto-Protection Middleware: Automatically secures endpoints based on configuration
- β‘ Rate Limiting: Built-in rate limiting with configurable limits
- π― Easy Configuration: Configure everything via
config.yaml- no code changes needed - π Production Ready: Secure by default with proper error handling and logging
- π Flexible: Easy to enable/disable and customize for different environments
- Set JWT Secret Key:
export JWT_SECRET_KEY="your-super-secret-jwt-key-change-this-in-production"- Configure Security (in
config.yaml):
security:
enabled: true
auth_type: "jwt" # jwt, api_key, hybrid, none
jwt_secret_key: "${JWT_SECRET_KEY}"
rate_limit_enabled: true
excluded_paths: ["/docs", "/health", "/metrics"]
protected_paths: ["/api/v1"]- That's it! Your API is now automatically secured.
# Login to get JWT token
curl -X POST "http://localhost:8000/api/v1/auth/login" \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "username=admin&password=admin123"
# Use token in requests
curl -X GET "http://localhost:8000/api/v1/items" \
-H "Authorization: Bearer YOUR_JWT_TOKEN"# Use API key in header
curl -X GET "http://localhost:8000/api/v1/items" \
-H "X-API-Key: your_api_key_here"
# Or as query parameter
curl -X GET "http://localhost:8000/api/v1/items?api_key=your_api_key_here"Supports both JWT and API Key authentication - tries JWT first, falls back to API Key.
POST /api/v1/auth/login- Login and get JWT tokenGET /api/v1/auth/refresh- Refresh JWT tokenGET /api/v1/security/stats- Security statistics (requires auth)
For testing purposes:
- Username:
admin - Password:
admin123 - Roles:
["admin", "user"] - Permissions:
["read", "write", "admin"]
For detailed security configuration, advanced usage, troubleshooting, and API reference, see:
π docs/AUTH.md - Complete security module documentation
This includes:
- Detailed configuration options
- Advanced authentication patterns
- Security context usage
- Error handling and troubleshooting
- Production security considerations
- Complete API reference
The template provides 8 focused, production-ready endpoints:
GET /api/v1/health- Service health check with detailed statusGET /api/v1/status- System status and metricsGET /api/v1/info- API information and available endpoints
POST /api/v1/data- Create data itemGET /api/v1/data/{id}- Get data item by IDPUT /api/v1/data/{id}- Update data itemDELETE /api/v1/data/{id}- Delete data item
POST /api/v1/upload- Upload file with validation
GET /- Comprehensive template information with dynamic endpoint discovery
- Session Tracking: Every request includes a unique session ID for debugging
- Error Handling: Proper HTTP status codes and error responses
- File Upload: Local file storage with validation
- Dynamic Discovery: Automatic endpoint discovery and categorization
- Health Monitoring: Comprehensive service status and metrics
- π Automatic Security: All endpoints are automatically protected based on configuration
# Health check (public)
curl -X GET "http://localhost:8000/api/v1/health"
# Create data (requires authentication)
curl -X POST "http://localhost:8000/api/v1/data" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_JWT_TOKEN" \
-d '{"data": {"name": "example", "value": 123}, "project_id": "my-project"}'
# Upload file (requires authentication)
curl -X POST "http://localhost:8000/api/v1/upload" \
-H "Authorization: Bearer YOUR_JWT_TOKEN" \
-F "file=@document.pdf" \
-F "project_id=my-project"
# Get template information (public)
curl -X GET "http://localhost:8000/"The application uses a hybrid configuration approach with YAML files and environment variables:
# Application Settings
app:
name: "fastapi-rest-template"
version: "1.0.0"
description: "FastAPI AI Server with AI Processing"
debug: false
environment: "development"
temp_dir: "runtime/temp"
assets_dir: "runtime/assets"
max_file_size: "10MB"
allowed_file_types: ["jpg", "jpeg", "png", "pdf", "txt"]
# API Configuration (universal for the whole app)
api_prefix: "/api/v1"
api_tags: ["API"]
# Server Configuration
server:
host: "0.0.0.0"
port: 8000
reload: false
workers: 1
timeout: 30
# Security Configuration
security:
enabled: true
auth_type: "jwt" # jwt, api_key, hybrid, none
jwt_secret_key: "${JWT_SECRET_KEY}"
jwt_algorithm: "HS256"
jwt_access_token_expire_minutes: 30
jwt_refresh_token_expire_days: 7
rate_limit_enabled: true
rate_limit_requests_per_minute: 60
excluded_paths: ["/docs", "/health", "/metrics"]
protected_paths: ["/api/v1"]
# Process Manager Configuration
process_manager:
type: "stateful" # stateful or stateless
stateful:
max_processes: 1000
enable_memory_optimization: true
stateless:
cache_ttl: 300
enable_persistence: false
cleanup:
enabled: true
interval: 300
process_ttl: 1800
orphaned_files_ttl: 3600
# Server Manager Configuration
server_manager:
app:
name: "FastAPI AI Server"
version: "1.0.0"
directories:
temp: "./runtime/temp"
outputs: "./runtime/outputs"
logs: "./runtime/logs"
housekeeping:
enabled: true
interval: 600
cleanup:
force_on_startup: true
force_on_shutdown: true
services:
# Core API services
item_service:
enabled: true
config:
max_items: 10000
auto_cleanup: true
document_service:
enabled: true
config:
max_file_size: 104857600 # 100MB
allowed_extensions: [".pdf", ".doc", ".docx", ".txt", ".md", ".jpg", ".png"]- Memory-based: Maintains process state in memory for fast access
- Performance: Optimized for single-instance applications
- Cleanup: Automatic cleanup of old processes and files
- Distributed: Suitable for distributed systems
- Memory-efficient: Minimal memory usage
- Scalable: Can be deployed across multiple instances
- Create your service class:
# src/services/my_service.py
from src.core.server_manager import AIService, ServiceConfig
class MyCustomService(AIService):
def __init__(self, config: ServiceConfig, device: Optional[str] = None):
super().__init__(config, device)
async def initialize(self) -> bool:
# Initialize your service
self.is_initialized = True
return True
async def shutdown(self) -> None:
# Cleanup your service
self.is_shutting_down = True
def get_status(self) -> Dict[str, Any]:
return {
"name": self.config.name,
"initialized": self.is_initialized
}- Register your service:
# In your application startup
from src.core.server_manager import create_server_manager
from src.services.my_service import MyCustomService, ServiceConfig
server_manager = create_server_manager()
# Create and register service
service_config = ServiceConfig(
name="my_service",
enabled=True,
config={"custom_param": "value"}
)
service = MyCustomService(service_config)
server_manager.register_service(service)# Create and manage processes
from src.core.process_manager import create_process_manager, ProcessState
process_manager = create_process_manager()
# Create a new process
process_id = process_manager.create_process(
"image_processing",
input_file="image.jpg",
output_format="png"
)
# Update process status
process_manager.update_process(
process_id,
status=ProcessState.RUNNING,
started_at=time.time()
)
# Track files for cleanup
process_manager.track_file(process_id, "/path/to/output.png")
# Complete the process
process_manager.update_process(
process_id,
status=ProcessState.COMPLETED,
completed_at=time.time()
)# Access configuration
from src.utils.config.settings import settings
# Get configuration values
app_name = settings.get("app.name")
server_port = settings.get("server.port", 8000)
# Get section-specific configuration
server_config = settings.get_server_config()
process_config = settings.get_process_manager_config()
# Check environment
if settings.is_development():
# Development-specific logic
pass
# Print configuration summary
settings.print_config_summary()The system tracks comprehensive metrics:
- Process Metrics: Created, completed, failed processes
- File Management: Files cleaned, orphaned files detected
- Server Health: Component status, resource usage
- Performance: Response times, cleanup efficiency
- π Security Metrics: Authentication attempts, rate limit hits, security events
# Get process manager metrics
from src.core.managers import get_process_manager
process_mgr = get_process_manager(request)
metrics = process_mgr.get_metrics()
# Get server manager status
from src.core.managers import get_server_manager
server_mgr = get_server_manager(request)
status = server_mgr.get_server_status()
# Get security statistics
from src.utils.auth import get_security_manager
security_mgr = get_security_manager()
security_stats = security_mgr.get_stats()| Variable | Description | Default | Required |
|---|---|---|---|
ENVIRONMENT |
Application environment | production | No |
APP_DEBUG |
Enable debug mode | false | No |
APP_NAME |
Application name | fastapi-rest-template | No |
SERVER_HOST |
Server host address | 0.0.0.0 | No |
SERVER_PORT |
Server port | 8000 | No |
SERVER_RELOAD |
Enable auto-reload | false | No |
JWT_SECRET_KEY |
JWT secret key | - | Yes (for auth) |
OPENAI_API_KEY |
OpenAI API key | - | No |
ANTHROPIC_API_KEY |
Anthropic API key | - | No |
LOG_LEVEL |
Logging level | INFO | No |
-
Process Manager Selection:
- Use Stateful for single-instance deployments
- Use Stateless for distributed/microservice architectures
-
Resource Allocation:
- CPU: 2+ cores recommended for concurrent processing
- RAM: 4GB+ for process management and caching
- Storage: SSD recommended for file operations
-
Scaling Options:
- Horizontal: Deploy multiple instances behind load balancer
- Vertical: Increase resources for single instance
- Process Management: Configure appropriate TTL and cleanup intervals
# Production environment variables
export ENVIRONMENT=production
export APP_DEBUG=false
export LOG_LEVEL=WARNING
export JWT_SECRET_KEY=your_secure_secret_key
# Generate secure JWT secret
export JWT_SECRET_KEY=$(python -c "import secrets; print(secrets.token_urlsafe(32))")
# Configure CORS for production
# Update config.yaml CORS settings# Set up health check monitoring
curl -f http://localhost:8000/api/v1/health || exit 1
# Monitor process metrics
curl http://localhost:8000/status | jq '.process_metrics'
# Get comprehensive template information
curl http://localhost:8000/ | jq '.template_info'
# Check security statistics
curl -H "Authorization: Bearer YOUR_TOKEN" http://localhost:8000/api/v1/security/stats-
Configuration Issues:
- Verify YAML syntax in config files
- Check environment variable names and values
- Ensure all required directories exist
-
Security Issues:
- "JWT secret key not configured": Set the
JWT_SECRET_KEYenvironment variable - "Authentication required": Check that your endpoint path is in
protected_paths - "Rate limit exceeded": Adjust
rate_limit_requests_per_minutein config - "Invalid token": Check token format and expiration
- "JWT secret key not configured": Set the
-
Process Management Issues:
- Check process TTL settings
- Verify cleanup intervals
- Monitor file tracking configuration
-
Server Management Issues:
- Verify signal handling setup
- Check housekeeping intervals
- Monitor service initialization order
# Enable detailed logging
export LOG_LEVEL=DEBUG
export APP_DEBUG=true
# Run with verbose output
python app.py# Monitor metrics in real-time
watch -n 5 'curl -s http://localhost:8000/status | jq .'
# Check process manager status
curl http://localhost:8000/status | jq '.process_metrics'
# Get dynamic endpoint information
curl http://localhost:8000/ | jq '.api.endpoints'
# Test authentication
curl -X POST "http://localhost:8000/api/v1/auth/login" \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "username=admin&password=admin123"# Run all tests
pytest
# Run specific test types
pytest tests/unit/
pytest tests/integration/
pytest tests/e2e/
# Run with coverage
pytest --cov=src tests/
# Test security module specifically
pytest src/utils/auth/test_security.pytests/
βββ conftest.py # Test configuration
βββ unit/ # Unit tests
β βββ test_config.py # Configuration tests
β βββ test_process_manager.py
β βββ test_server_manager.py
β βββ test_auth.py # Security tests
βββ integration/ # Integration tests
β βββ test_api.py # API integration tests
β βββ test_services.py # Service integration tests
β βββ test_auth_integration.py # Security integration tests
βββ e2e/ # End-to-end tests
βββ test_workflows.py # Complete workflow tests
We welcome contributions from the community! This project follows a comprehensive contribution process to ensure quality and maintainability.
- Fork and Clone: Fork the repository and clone your fork
- Setup Environment: Follow the development setup in CONTRIBUTING.md
- Create Branch: Create a feature branch for your changes
- Make Changes: Implement your changes with tests
- Submit PR: Create a pull request following our guidelines
- Code of Conduct: Please read and follow our Code of Conduct
- Security: Report security vulnerabilities privately - see Security Policy
- Testing: Maintain test coverage and follow testing guidelines
- Documentation: Update documentation for any API or configuration changes
# Install development dependencies
uv sync --dev
# Install pre-commit hooks
pre-commit install
# Run tests
pytest
# Format code
black src/ tests/
isort src/ tests/
# Test security integration
python src/utils/auth/example_integration.pyFor comprehensive contribution guidelines, development setup, and community standards, please refer to:
- CONTRIBUTING.md - Complete contribution guide with development setup, code standards, and PR process
- CODE_OF_CONDUCT.md - Community standards and enforcement guidelines
- SECURITY.md - Security policy and vulnerability reporting procedures
We take security seriously and have established comprehensive security measures:
- Vulnerability Reporting: Report security issues privately via email (see Security Policy)
- Security Updates: Regular dependency updates and security patches
- Code Review: All changes require code review and security assessment
- Access Control: Proper authentication and authorization mechanisms
- Enterprise-Grade Security: JWT, API Keys, rate limiting, and automatic protection
- Keep dependencies updated
- Use strong authentication methods (JWT with secure secrets)
- Follow secure coding practices
- Never commit secrets or sensitive data
- Report vulnerabilities responsibly
- Use HTTPS in production
- Configure appropriate rate limits
- Monitor authentication failures
For detailed security information, incident response procedures, and vulnerability reporting, see our Security Policy.
This project is licensed under the MIT License - see the LICENSE file for details.
- Built with FastAPI for high-performance web APIs
- Configuration management inspired by modern microservice patterns
- Process management designed for both monolithic and distributed architectures
- Enterprise-grade security system with multiple authentication methods
- Logging and monitoring following enterprise best practices
- Enterprise-grade Security Module: JWT, API Keys, Hybrid auth, rate limiting
- Auto-Protection Middleware: Automatic endpoint security
- Github Actions: Testing, Deployment, AutoDocumentation
- Detailed Documentation with jupyter-notebook tutorials
- Detailed Testing for the API system
- Plugin Architecture: Database, monitoring, and cloud service plugins
- Kubernetes deployment manifests
- Advanced metrics and monitoring dashboard
- Database integration examples
- Advanced caching strategies
- Rate limiting implementation
- API versioning examples