Production Deployment Settings
When you deploy etchblok-test-api to a production environment, you must use the ProductionConfig class to ensure the application is secure and optimized for performance. Unlike the default development settings, the production configuration enforces strict environment variable requirements and increases cache limits.
Configuring the Application for Production
To run etchblok-test-api in production, you must explicitly pass the ProductionConfig class to the create_app factory and provide a SECRET_KEY via environment variables.
import os
from app import create_app
from app.config import ProductionConfig
# Ensure SECRET_KEY is set in the environment
# os.environ["SECRET_KEY"] = "your-secure-random-key"
app = create_app(config_class=ProductionConfig)
Security Requirements
The ProductionConfig class in app/config.py is designed to prevent the application from starting without a valid secret key. While BaseConfig provides a default "change-me" key, ProductionConfig uses a default_factory that accesses os.environ directly:
@dataclass
class ProductionConfig(BaseConfig):
"""Configuration for production deployments."""
SECRET_KEY: str = field(default_factory=lambda: os.environ["SECRET_KEY"])
# ...
If the SECRET_KEY environment variable is missing, the application will raise a KeyError during initialization, preventing insecure deployments.
Performance Optimizations
ProductionConfig overrides the default caching and pagination settings to handle higher traffic volumes:
- Cache Tuning: The
get_cache_configmethod increases the Time-To-Live (TTL) and the maximum number of entries compared to development settings.- TTL: 600 seconds (10 minutes)
- Max Size: 4096 entries
- Pagination: Sets
PAGE_SIZEtoDEFAULT_PAGE_SIZE(25), ensuring consistent response sizes.
def get_cache_config(self) -> Dict[str, Any]:
return _build_cache_config(ttl=600, max_size=4096)
Initializing with a WSGI Server
When using a WSGI server like Gunicorn or uWSGI, you should create an entry point file (e.g., wsgi.py) that initializes the app with the correct configuration.
# wsgi.py
from app import create_app
from app.config import ProductionConfig
# The WSGI server will look for the 'application' or 'app' object
app = create_app(config_class=ProductionConfig)
You can then run the server pointing to this instance:
export SECRET_KEY="your-production-secret"
gunicorn wsgi:app
Troubleshooting
KeyError: 'SECRET_KEY'
If you see a KeyError: 'SECRET_KEY' during startup, it means the ProductionConfig was loaded but the required environment variable was not found.
Solution: Ensure the environment variable is exported in your shell or defined in your container orchestration configuration (e.g., Kubernetes Secrets or Docker Compose environment section).
Unexpectedly Small Page Sizes
If your production API is returning only 10 items per page instead of 25, the application is likely still running with DevelopmentConfig.
Solution: Check your create_app() call in the entry point. The factory in app/__init__.py defaults to DevelopmentConfig if no argument is provided:
# app/__init__.py
def create_app(config_class=DevelopmentConfig) -> Flask:
# ...
Ensure you are explicitly passing ProductionConfig as shown in the examples above.