Overview
The Premier League API can be run locally in two modes: development (using Flask’s built-in server) and production (using Gunicorn). This guide covers configuration, environment variables, and deployment options for local environments.Installation
First, install the library with API support:Development Mode
Development mode uses Flask’s built-in development server with hot-reloading and debugging enabled.Basic Usage
Custom Configuration
Production Mode
Production mode uses Gunicorn, a production-grade WSGI HTTP server that handles multiple concurrent requests efficiently.Running with Gunicorn
premier_league/api/app.py:76-119
Gunicorn Configuration
The library uses a custom Gunicorn application with the following default settings:workers: Number of worker processes (recommended: 2-4 × CPU cores)worker_class: Usesyncfor standard requests,geventfor asynctimeout: Maximum time (seconds) a request can take before worker restartpreload_app: Load application code before forking workers (reduces memory usage)
Configuration Methods
1. Environment Variables
Configure the server using environment variables prefixed withPREMIER_LEAGUE_:
premier_league/api/config/config.py:42-56
2. YAML Configuration
Create a YAML configuration file:config.yaml
3. Dictionary Configuration
Configuration Reference
| Variable | Type | Default | Description |
|---|---|---|---|
HOST | string | "0.0.0.0" | Server host address |
PORT | integer | 3000 | Server port number |
DEBUG | boolean | false | Enable debug mode (development only) |
SECRET_KEY | string | "default-secret-key" | Secret key for sessions (change in production!) |
CORS_ORIGINS | list | ["*"] | Allowed CORS origins |
JSON_SORT_KEYS | boolean | false | Sort JSON response keys |
RATE_LIMIT | integer | 100 | Requests per minute per IP |
CACHE_TYPE | string | "simple" | Cache backend type |
CACHE_DEFAULT_TIMEOUT | integer | 300 | Cache timeout in seconds |
LOG_LEVEL | string | "INFO" | Logging level (DEBUG, INFO, WARNING, ERROR) |
premier_league/api/config/config.py:8-21
Server Features
The API server includes the following built-in features:CORS (Cross-Origin Resource Sharing)
CORS_ORIGINS setting.
Rate Limiting
RATE_LIMIT setting.
Response Caching
premier_league/api/app.py:61-67
Complete Deployment Example
Best Practices
Security
Security
- Always set a strong
SECRET_KEYin production - Restrict
CORS_ORIGINSto specific domains - Use environment variables for sensitive configuration
- Never commit secrets to version control
- Run behind a reverse proxy (nginx, Apache) for HTTPS
Performance
Performance
- Set workers to 2-4 × CPU cores for CPU-bound tasks
- Enable
preload_appto reduce memory usage - Adjust
CACHE_DEFAULT_TIMEOUTbased on data freshness needs - Increase
RATE_LIMITfor trusted clients - Monitor memory usage and adjust workers accordingly
Monitoring
Monitoring
- Set
LOG_LEVELtoWARNINGorERRORin production - Use structured logging for better analysis
- Monitor response times and error rates
- Set up health check endpoints
- Track rate limit violations
Troubleshooting
Port already in use
Port already in use
Error:
Address already in useSolution: Change the port or kill the process using it:Worker timeout errors
Worker timeout errors
Error:
Worker timeoutSolution: Increase the timeout in Gunicorn configuration:CORS errors
CORS errors
Error:
CORS policy: No 'Access-Control-Allow-Origin' headerSolution: Add your frontend URL to CORS_ORIGINS:Next Steps
AWS Lambda Deployment
Deploy to AWS Lambda for serverless scalability
API Endpoints
Explore available API endpoints