Configuration
This guide covers the configuration options for the Tracker GraphQL API.
Environment Variables
The application uses environment variables for configuration. Create a .env file in the root directory:
# Database
DATABASE_URL=postgresql://user:password@localhost:5432/tracker
DATABASE_POOL_SIZE=10
DATABASE_MAX_OVERFLOW=20
# Redis
REDIS_URL=redis://username:password@localhost:6379/0
REDIS_MAX_CONNECTIONS=50
REDIS_SSL=true
# Authentication
JWT_SECRET=your-secret-key
JWT_ALGORITHM=HS256
JWT_EXPIRATION=3600 # 1 hour in seconds
# Server
HOST=0.0.0.0
PORT=8000
DEBUG=false
LOG_LEVEL=INFO
# Geocoding
GEOCODING_API_KEY=your-api-key
GEOCODING_CACHE_TTL=86400 # 24 hours in seconds
Database Configuration
PostgreSQL Settings
SQLALCHEMY_DATABASE_URI = os.getenv('DATABASE_URL')
SQLALCHEMY_POOL_SIZE = int(os.getenv('DATABASE_POOL_SIZE', 10))
SQLALCHEMY_MAX_OVERFLOW = int(os.getenv('DATABASE_MAX_OVERFLOW', 20))
SQLALCHEMY_TRACK_MODIFICATIONS = False
PostGIS Extension
The database requires PostGIS for geospatial functionality:
CREATE EXTENSION postgis;
Redis Configuration
Redis is used for caching and queue management:
# Basic Redis configuration
REDIS_HOST = os.getenv('REDIS_HOST', 'localhost')
REDIS_PORT = int(os.getenv('REDIS_PORT', 6379))
REDIS_USERNAME = os.getenv('REDIS_USERNAME')
REDIS_PASSWORD = os.getenv('REDIS_PASSWORD')
REDIS_SSL = os.getenv('REDIS_SSL', '').lower() in ('true', '1', 'yes')
REDIS_DB = int(os.getenv('REDIS_DB', 0))
# For URL-based configuration
REDIS_URL = os.getenv('REDIS_URL')
REDIS_MAX_CONNECTIONS = int(os.getenv('REDIS_MAX_CONNECTIONS', 50))
For detailed information about Redis Cluster support, including how we solved the CROSSSLOT error and ensured backward compatibility with standalone Redis, see Redis Cluster Support.
Redis Connection Types
The application supports different Redis deployment types:
Standalone Redis
# Standard Redis connection
redis_client = redis.Redis(
host=REDIS_HOST,
port=REDIS_PORT,
username=REDIS_USERNAME,
password=REDIS_PASSWORD,
ssl=REDIS_SSL,
db=REDIS_DB
)
Redis Cluster (AWS MemoryDB)
# Redis Cluster connection (for AWS MemoryDB)
redis_client = redis.RedisCluster(
host=REDIS_HOST, # e.g., clustercfg.tracker.xxxxx.memorydb.region.amazonaws.com
port=REDIS_PORT,
username=REDIS_USERNAME,
password=REDIS_PASSWORD,
ssl=True # TLS is required for AWS MemoryDB
)
Cache Settings
CACHE_TYPE = 'redis'
CACHE_DEFAULT_TIMEOUT = 300 # 5 minutes
CACHE_KEY_PREFIX = 'tracker:'
Logging Configuration
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'handlers': {
'console': {
'class': 'logging.StreamHandler',
'level': os.getenv('LOG_LEVEL', 'INFO'),
},
},
'root': {
'handlers': ['console'],
'level': os.getenv('LOG_LEVEL', 'INFO'),
},
}
Socket.IO Settings
# For URL-based configuration
SOCKETIO_MESSAGE_QUEUE = os.getenv('REDIS_URL')
# For component-based configuration
SOCKETIO_REDIS_HOST = os.getenv('REDIS_HOST', 'localhost')
SOCKETIO_REDIS_PORT = int(os.getenv('REDIS_PORT', 6379))
SOCKETIO_REDIS_USERNAME = os.getenv('REDIS_USERNAME')
SOCKETIO_REDIS_PASSWORD = os.getenv('REDIS_PASSWORD')
SOCKETIO_REDIS_SSL = os.getenv('REDIS_SSL', '').lower() in ('true', '1', 'yes')
# Socket.IO timing settings
SOCKETIO_PING_TIMEOUT = 60
SOCKETIO_PING_INTERVAL = 25
Geocoding Configuration
GEOCODING_API_KEY = os.getenv('GEOCODING_API_KEY')
GEOCODING_CACHE_TTL = int(os.getenv('GEOCODING_CACHE_TTL', 86400))
GEOCODING_BATCH_SIZE = 100
Production Deployment
For production environments:
- Use strong, unique values for secrets
- Enable HTTPS
- Configure appropriate logging
- Set up monitoring
- Use production-grade servers (e.g., Gunicorn)
Example production settings:
DEBUG=false
LOG_LEVEL=WARNING
WORKERS=4
TIMEOUT=120
Docker Environment
When using Docker, configure these settings in docker-compose.yml:
services:
api:
environment:
- DATABASE_URL=postgresql://user:password@db:5432/tracker
- REDIS_URL=redis://redis:6379
- JWT_SECRET=${JWT_SECRET}
# ... other variables