Improve base config
This commit is contained in:
392
shared/config/base.py
Normal file
392
shared/config/base.py
Normal file
@@ -0,0 +1,392 @@
|
||||
# shared/config/base.py
|
||||
"""
|
||||
Base configuration for all microservices
|
||||
Provides common settings and patterns
|
||||
"""
|
||||
|
||||
import os
|
||||
from typing import List, Dict, Optional, Any
|
||||
from pydantic_settings import BaseSettings
|
||||
from pydantic import validator
|
||||
|
||||
|
||||
class BaseServiceSettings(BaseSettings):
|
||||
"""
|
||||
Base configuration class for all microservices
|
||||
Provides common settings and validation patterns
|
||||
"""
|
||||
|
||||
# ================================================================
|
||||
# CORE SERVICE SETTINGS
|
||||
# ================================================================
|
||||
|
||||
# Application Identity
|
||||
APP_NAME: str = "Bakery Service"
|
||||
SERVICE_NAME: str = "base-service"
|
||||
VERSION: str = "1.0.0"
|
||||
DESCRIPTION: str = "Base microservice for bakery platform"
|
||||
|
||||
# Environment & Debugging
|
||||
ENVIRONMENT: str = os.getenv("ENVIRONMENT", "development")
|
||||
DEBUG: bool = os.getenv("DEBUG", "false").lower() == "true"
|
||||
LOG_LEVEL: str = os.getenv("LOG_LEVEL", "INFO")
|
||||
|
||||
# Service Discovery & Health
|
||||
SERVICE_HOST: str = os.getenv("SERVICE_HOST", "0.0.0.0")
|
||||
SERVICE_PORT: int = int(os.getenv("SERVICE_PORT", "8000"))
|
||||
HEALTH_CHECK_ENABLED: bool = True
|
||||
METRICS_ENABLED: bool = True
|
||||
|
||||
# ================================================================
|
||||
# DATABASE CONFIGURATION
|
||||
# ================================================================
|
||||
|
||||
# Primary database URL (should be overridden by each service)
|
||||
DATABASE_URL: str = os.getenv("DATABASE_URL", "")
|
||||
|
||||
# Database connection settings
|
||||
DB_POOL_SIZE: int = int(os.getenv("DB_POOL_SIZE", "10"))
|
||||
DB_MAX_OVERFLOW: int = int(os.getenv("DB_MAX_OVERFLOW", "20"))
|
||||
DB_POOL_TIMEOUT: int = int(os.getenv("DB_POOL_TIMEOUT", "30"))
|
||||
DB_POOL_RECYCLE: int = int(os.getenv("DB_POOL_RECYCLE", "3600"))
|
||||
DB_ECHO: bool = os.getenv("DB_ECHO", "false").lower() == "true"
|
||||
|
||||
# ================================================================
|
||||
# REDIS CONFIGURATION
|
||||
# ================================================================
|
||||
|
||||
REDIS_URL: str = os.getenv("REDIS_URL", "redis://redis:6379")
|
||||
REDIS_DB: int = int(os.getenv("REDIS_DB", "0"))
|
||||
REDIS_MAX_CONNECTIONS: int = int(os.getenv("REDIS_MAX_CONNECTIONS", "50"))
|
||||
REDIS_RETRY_ON_TIMEOUT: bool = True
|
||||
REDIS_SOCKET_KEEPALIVE: bool = True
|
||||
REDIS_SOCKET_KEEPALIVE_OPTIONS: Dict[str, int] = {
|
||||
"TCP_KEEPIDLE": 1,
|
||||
"TCP_KEEPINTVL": 3,
|
||||
"TCP_KEEPCNT": 5,
|
||||
}
|
||||
|
||||
@property
|
||||
def REDIS_URL_WITH_DB(self) -> str:
|
||||
"""Get Redis URL with database number"""
|
||||
base_url = self.REDIS_URL.rstrip('/')
|
||||
return f"{base_url}/{self.REDIS_DB}"
|
||||
|
||||
# ================================================================
|
||||
# RABBITMQ CONFIGURATION
|
||||
# ================================================================
|
||||
|
||||
RABBITMQ_URL: str = os.getenv("RABBITMQ_URL", "amqp://bakery:forecast123@rabbitmq:5672/")
|
||||
RABBITMQ_EXCHANGE: str = os.getenv("RABBITMQ_EXCHANGE", "bakery_events")
|
||||
RABBITMQ_QUEUE_PREFIX: str = os.getenv("RABBITMQ_QUEUE_PREFIX", "bakery")
|
||||
RABBITMQ_RETRY_ATTEMPTS: int = int(os.getenv("RABBITMQ_RETRY_ATTEMPTS", "3"))
|
||||
RABBITMQ_RETRY_DELAY: int = int(os.getenv("RABBITMQ_RETRY_DELAY", "5"))
|
||||
|
||||
# ================================================================
|
||||
# AUTHENTICATION & SECURITY
|
||||
# ================================================================
|
||||
|
||||
# JWT Configuration
|
||||
JWT_SECRET_KEY: str = os.getenv("JWT_SECRET_KEY", "change-this-in-production")
|
||||
JWT_ALGORITHM: str = os.getenv("JWT_ALGORITHM", "HS256")
|
||||
JWT_ACCESS_TOKEN_EXPIRE_MINUTES: int = int(os.getenv("JWT_ACCESS_TOKEN_EXPIRE_MINUTES", "30"))
|
||||
JWT_REFRESH_TOKEN_EXPIRE_DAYS: int = int(os.getenv("JWT_REFRESH_TOKEN_EXPIRE_DAYS", "7"))
|
||||
|
||||
# Service-to-Service Authentication
|
||||
SERVICE_API_KEY: str = os.getenv("SERVICE_API_KEY", "service-api-key-change-in-production")
|
||||
ENABLE_SERVICE_AUTH: bool = os.getenv("ENABLE_SERVICE_AUTH", "false").lower() == "true"
|
||||
|
||||
# Password Requirements
|
||||
PASSWORD_MIN_LENGTH: int = int(os.getenv("PASSWORD_MIN_LENGTH", "8"))
|
||||
PASSWORD_REQUIRE_UPPERCASE: bool = os.getenv("PASSWORD_REQUIRE_UPPERCASE", "true").lower() == "true"
|
||||
PASSWORD_REQUIRE_LOWERCASE: bool = os.getenv("PASSWORD_REQUIRE_LOWERCASE", "true").lower() == "true"
|
||||
PASSWORD_REQUIRE_NUMBERS: bool = os.getenv("PASSWORD_REQUIRE_NUMBERS", "true").lower() == "true"
|
||||
PASSWORD_REQUIRE_SYMBOLS: bool = os.getenv("PASSWORD_REQUIRE_SYMBOLS", "false").lower() == "true"
|
||||
|
||||
# Security Settings
|
||||
BCRYPT_ROUNDS: int = int(os.getenv("BCRYPT_ROUNDS", "12"))
|
||||
MAX_LOGIN_ATTEMPTS: int = int(os.getenv("MAX_LOGIN_ATTEMPTS", "5"))
|
||||
LOCKOUT_DURATION_MINUTES: int = int(os.getenv("LOCKOUT_DURATION_MINUTES", "30"))
|
||||
|
||||
# ================================================================
|
||||
# INTER-SERVICE COMMUNICATION
|
||||
# ================================================================
|
||||
|
||||
# Service URLs (can be overridden by environment variables)
|
||||
GATEWAY_URL: str = os.getenv("GATEWAY_URL", "http://gateway:8000")
|
||||
AUTH_SERVICE_URL: str = os.getenv("AUTH_SERVICE_URL", "http://auth-service:8000")
|
||||
TRAINING_SERVICE_URL: str = os.getenv("TRAINING_SERVICE_URL", "http://training-service:8000")
|
||||
FORECASTING_SERVICE_URL: str = os.getenv("FORECASTING_SERVICE_URL", "http://forecasting-service:8000")
|
||||
DATA_SERVICE_URL: str = os.getenv("DATA_SERVICE_URL", "http://data-service:8000")
|
||||
TENANT_SERVICE_URL: str = os.getenv("TENANT_SERVICE_URL", "http://tenant-service:8000")
|
||||
NOTIFICATION_SERVICE_URL: str = os.getenv("NOTIFICATION_SERVICE_URL", "http://notification-service:8000")
|
||||
|
||||
# HTTP Client Settings
|
||||
HTTP_TIMEOUT: int = int(os.getenv("HTTP_TIMEOUT", "30"))
|
||||
HTTP_RETRIES: int = int(os.getenv("HTTP_RETRIES", "3"))
|
||||
HTTP_RETRY_DELAY: float = float(os.getenv("HTTP_RETRY_DELAY", "1.0"))
|
||||
|
||||
# ================================================================
|
||||
# CORS & API CONFIGURATION
|
||||
# ================================================================
|
||||
|
||||
CORS_ORIGINS: str = os.getenv("CORS_ORIGINS", "http://localhost:3000,http://localhost:3001")
|
||||
CORS_ALLOW_CREDENTIALS: bool = os.getenv("CORS_ALLOW_CREDENTIALS", "true").lower() == "true"
|
||||
CORS_ALLOW_METHODS: List[str] = ["GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"]
|
||||
CORS_ALLOW_HEADERS: List[str] = ["*"]
|
||||
|
||||
@property
|
||||
def CORS_ORIGINS_LIST(self) -> List[str]:
|
||||
"""Get CORS origins as list"""
|
||||
return [origin.strip() for origin in self.CORS_ORIGINS.split(",") if origin.strip()]
|
||||
|
||||
# Rate Limiting
|
||||
RATE_LIMIT_ENABLED: bool = os.getenv("RATE_LIMIT_ENABLED", "true").lower() == "true"
|
||||
RATE_LIMIT_REQUESTS: int = int(os.getenv("RATE_LIMIT_REQUESTS", "100"))
|
||||
RATE_LIMIT_WINDOW: int = int(os.getenv("RATE_LIMIT_WINDOW", "60"))
|
||||
RATE_LIMIT_BURST: int = int(os.getenv("RATE_LIMIT_BURST", "10"))
|
||||
|
||||
# API Documentation
|
||||
API_DOCS_ENABLED: bool = os.getenv("API_DOCS_ENABLED", "true").lower() == "true"
|
||||
API_DOCS_URL: str = "/docs"
|
||||
API_REDOC_URL: str = "/redoc"
|
||||
API_OPENAPI_URL: str = "/openapi.json"
|
||||
|
||||
# ================================================================
|
||||
# EXTERNAL APIS & INTEGRATIONS
|
||||
# ================================================================
|
||||
|
||||
# Weather API (AEMET - Spanish Weather Service)
|
||||
AEMET_API_KEY: str = os.getenv("AEMET_API_KEY", "")
|
||||
AEMET_BASE_URL: str = "https://opendata.aemet.es/opendata"
|
||||
AEMET_TIMEOUT: int = int(os.getenv("AEMET_TIMEOUT", "30"))
|
||||
|
||||
# Madrid Open Data
|
||||
MADRID_OPENDATA_API_KEY: str = os.getenv("MADRID_OPENDATA_API_KEY", "")
|
||||
MADRID_OPENDATA_BASE_URL: str = "https://datos.madrid.es"
|
||||
MADRID_OPENDATA_TIMEOUT: int = int(os.getenv("MADRID_OPENDATA_TIMEOUT", "30"))
|
||||
|
||||
# Email Configuration
|
||||
SMTP_HOST: str = os.getenv("SMTP_HOST", "smtp.gmail.com")
|
||||
SMTP_PORT: int = int(os.getenv("SMTP_PORT", "587"))
|
||||
SMTP_USER: str = os.getenv("SMTP_USER", "")
|
||||
SMTP_PASSWORD: str = os.getenv("SMTP_PASSWORD", "")
|
||||
SMTP_TLS: bool = os.getenv("SMTP_TLS", "true").lower() == "true"
|
||||
SMTP_SSL: bool = os.getenv("SMTP_SSL", "false").lower() == "true"
|
||||
|
||||
# WhatsApp API
|
||||
WHATSAPP_API_KEY: str = os.getenv("WHATSAPP_API_KEY", "")
|
||||
WHATSAPP_BASE_URL: str = os.getenv("WHATSAPP_BASE_URL", "https://api.twilio.com")
|
||||
WHATSAPP_FROM_NUMBER: str = os.getenv("WHATSAPP_FROM_NUMBER", "")
|
||||
|
||||
# ================================================================
|
||||
# ML & AI CONFIGURATION
|
||||
# ================================================================
|
||||
|
||||
# Model Storage
|
||||
MODEL_STORAGE_PATH: str = os.getenv("MODEL_STORAGE_PATH", "/app/models")
|
||||
MODEL_STORAGE_BACKEND: str = os.getenv("MODEL_STORAGE_BACKEND", "local") # local, s3, gcs
|
||||
|
||||
# Training Configuration
|
||||
MAX_TRAINING_TIME_MINUTES: int = int(os.getenv("MAX_TRAINING_TIME_MINUTES", "30"))
|
||||
MIN_TRAINING_DATA_DAYS: int = int(os.getenv("MIN_TRAINING_DATA_DAYS", "30"))
|
||||
TRAINING_BATCH_SIZE: int = int(os.getenv("TRAINING_BATCH_SIZE", "1000"))
|
||||
|
||||
# Prophet Configuration
|
||||
PROPHET_SEASONALITY_MODE: str = os.getenv("PROPHET_SEASONALITY_MODE", "additive")
|
||||
PROPHET_CHANGEPOINT_PRIOR_SCALE: float = float(os.getenv("PROPHET_CHANGEPOINT_PRIOR_SCALE", "0.05"))
|
||||
PROPHET_SEASONALITY_PRIOR_SCALE: float = float(os.getenv("PROPHET_SEASONALITY_PRIOR_SCALE", "10.0"))
|
||||
|
||||
# Prediction Caching
|
||||
PREDICTION_CACHE_TTL_HOURS: int = int(os.getenv("PREDICTION_CACHE_TTL_HOURS", "6"))
|
||||
WEATHER_CACHE_TTL_HOURS: int = int(os.getenv("WEATHER_CACHE_TTL_HOURS", "1"))
|
||||
TRAFFIC_CACHE_TTL_HOURS: int = int(os.getenv("TRAFFIC_CACHE_TTL_HOURS", "1"))
|
||||
|
||||
# ================================================================
|
||||
# MONITORING & OBSERVABILITY
|
||||
# ================================================================
|
||||
|
||||
# Logging Configuration
|
||||
LOG_FORMAT: str = os.getenv("LOG_FORMAT", "json") # json, text
|
||||
LOG_FILE_ENABLED: bool = os.getenv("LOG_FILE_ENABLED", "false").lower() == "true"
|
||||
LOG_FILE_PATH: str = os.getenv("LOG_FILE_PATH", "/app/logs")
|
||||
LOG_ROTATION_SIZE: str = os.getenv("LOG_ROTATION_SIZE", "100MB")
|
||||
LOG_RETENTION_DAYS: int = int(os.getenv("LOG_RETENTION_DAYS", "30"))
|
||||
|
||||
# Metrics & Monitoring
|
||||
PROMETHEUS_ENABLED: bool = os.getenv("PROMETHEUS_ENABLED", "true").lower() == "true"
|
||||
PROMETHEUS_PORT: int = int(os.getenv("PROMETHEUS_PORT", "9090"))
|
||||
PROMETHEUS_PATH: str = "/metrics"
|
||||
|
||||
# Tracing
|
||||
JAEGER_ENABLED: bool = os.getenv("JAEGER_ENABLED", "false").lower() == "true"
|
||||
JAEGER_AGENT_HOST: str = os.getenv("JAEGER_AGENT_HOST", "localhost")
|
||||
JAEGER_AGENT_PORT: int = int(os.getenv("JAEGER_AGENT_PORT", "6831"))
|
||||
|
||||
# Health Checks
|
||||
HEALTH_CHECK_TIMEOUT: int = int(os.getenv("HEALTH_CHECK_TIMEOUT", "30"))
|
||||
HEALTH_CHECK_INTERVAL: int = int(os.getenv("HEALTH_CHECK_INTERVAL", "30"))
|
||||
|
||||
# ================================================================
|
||||
# DATA RETENTION & CLEANUP
|
||||
# ================================================================
|
||||
|
||||
DATA_RETENTION_DAYS: int = int(os.getenv("DATA_RETENTION_DAYS", "365"))
|
||||
LOG_RETENTION_DAYS: int = int(os.getenv("LOG_RETENTION_DAYS", "90"))
|
||||
METRIC_RETENTION_DAYS: int = int(os.getenv("METRIC_RETENTION_DAYS", "90"))
|
||||
TEMP_FILE_CLEANUP_HOURS: int = int(os.getenv("TEMP_FILE_CLEANUP_HOURS", "24"))
|
||||
|
||||
# ================================================================
|
||||
# BUSINESS RULES & CONSTRAINTS
|
||||
# ================================================================
|
||||
|
||||
# Forecasting Business Rules
|
||||
MAX_FORECAST_DAYS: int = int(os.getenv("MAX_FORECAST_DAYS", "30"))
|
||||
MIN_HISTORICAL_DAYS: int = int(os.getenv("MIN_HISTORICAL_DAYS", "60"))
|
||||
CONFIDENCE_THRESHOLD: float = float(os.getenv("CONFIDENCE_THRESHOLD", "0.8"))
|
||||
|
||||
# Spanish Business Context
|
||||
TIMEZONE: str = os.getenv("TIMEZONE", "Europe/Madrid")
|
||||
LOCALE: str = os.getenv("LOCALE", "es_ES.UTF-8")
|
||||
CURRENCY: str = os.getenv("CURRENCY", "EUR")
|
||||
|
||||
# Business Hours (24-hour format)
|
||||
BUSINESS_HOUR_START: int = int(os.getenv("BUSINESS_HOUR_START", "7"))
|
||||
BUSINESS_HOUR_END: int = int(os.getenv("BUSINESS_HOUR_END", "20"))
|
||||
|
||||
# Spanish Holidays & Seasonal Adjustments
|
||||
ENABLE_SPANISH_HOLIDAYS: bool = os.getenv("ENABLE_SPANISH_HOLIDAYS", "true").lower() == "true"
|
||||
ENABLE_MADRID_HOLIDAYS: bool = os.getenv("ENABLE_MADRID_HOLIDAYS", "true").lower() == "true"
|
||||
SCHOOL_CALENDAR_ENABLED: bool = os.getenv("SCHOOL_CALENDAR_ENABLED", "true").lower() == "true"
|
||||
|
||||
# ================================================================
|
||||
# DEVELOPMENT & TESTING
|
||||
# ================================================================
|
||||
|
||||
# Testing Configuration
|
||||
TESTING: bool = os.getenv("TESTING", "false").lower() == "true"
|
||||
TEST_DATABASE_URL: str = os.getenv("TEST_DATABASE_URL", "")
|
||||
MOCK_EXTERNAL_APIS: bool = os.getenv("MOCK_EXTERNAL_APIS", "false").lower() == "true"
|
||||
|
||||
# Development Features
|
||||
AUTO_RELOAD: bool = os.getenv("AUTO_RELOAD", "false").lower() == "true"
|
||||
PROFILING_ENABLED: bool = os.getenv("PROFILING_ENABLED", "false").lower() == "true"
|
||||
|
||||
# ================================================================
|
||||
# VALIDATORS
|
||||
# ================================================================
|
||||
|
||||
@validator('JWT_SECRET_KEY')
|
||||
def validate_jwt_secret(cls, v):
|
||||
if v == "change-this-in-production" and os.getenv("ENVIRONMENT") == "production":
|
||||
raise ValueError("JWT_SECRET_KEY must be changed in production")
|
||||
if len(v) < 32:
|
||||
raise ValueError("JWT_SECRET_KEY must be at least 32 characters long")
|
||||
return v
|
||||
|
||||
@validator('DATABASE_URL')
|
||||
def validate_database_url(cls, v):
|
||||
if not v:
|
||||
raise ValueError("DATABASE_URL is required")
|
||||
if not v.startswith(('postgresql://', 'postgresql+asyncpg://')):
|
||||
raise ValueError("DATABASE_URL must be a PostgreSQL URL")
|
||||
return v
|
||||
|
||||
@validator('LOG_LEVEL')
|
||||
def validate_log_level(cls, v):
|
||||
valid_levels = ['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL']
|
||||
if v.upper() not in valid_levels:
|
||||
raise ValueError(f"LOG_LEVEL must be one of: {valid_levels}")
|
||||
return v.upper()
|
||||
|
||||
@validator('ENVIRONMENT')
|
||||
def validate_environment(cls, v):
|
||||
valid_envs = ['development', 'staging', 'production', 'testing']
|
||||
if v.lower() not in valid_envs:
|
||||
raise ValueError(f"ENVIRONMENT must be one of: {valid_envs}")
|
||||
return v.lower()
|
||||
|
||||
# ================================================================
|
||||
# COMPUTED PROPERTIES
|
||||
# ================================================================
|
||||
|
||||
@property
|
||||
def IS_PRODUCTION(self) -> bool:
|
||||
"""Check if running in production"""
|
||||
return self.ENVIRONMENT == "production"
|
||||
|
||||
@property
|
||||
def IS_DEVELOPMENT(self) -> bool:
|
||||
"""Check if running in development"""
|
||||
return self.ENVIRONMENT == "development"
|
||||
|
||||
@property
|
||||
def IS_TESTING(self) -> bool:
|
||||
"""Check if running tests"""
|
||||
return self.TESTING or self.ENVIRONMENT == "testing"
|
||||
|
||||
@property
|
||||
def SERVICE_REGISTRY(self) -> Dict[str, str]:
|
||||
"""Get all service URLs"""
|
||||
return {
|
||||
"gateway": self.GATEWAY_URL,
|
||||
"auth": self.AUTH_SERVICE_URL,
|
||||
"training": self.TRAINING_SERVICE_URL,
|
||||
"forecasting": self.FORECASTING_SERVICE_URL,
|
||||
"data": self.DATA_SERVICE_URL,
|
||||
"tenant": self.TENANT_SERVICE_URL,
|
||||
"notification": self.NOTIFICATION_SERVICE_URL,
|
||||
}
|
||||
|
||||
@property
|
||||
def DATABASE_CONFIG(self) -> Dict[str, Any]:
|
||||
"""Get database configuration for SQLAlchemy"""
|
||||
return {
|
||||
"url": self.DATABASE_URL,
|
||||
"pool_size": self.DB_POOL_SIZE,
|
||||
"max_overflow": self.DB_MAX_OVERFLOW,
|
||||
"pool_timeout": self.DB_POOL_TIMEOUT,
|
||||
"pool_recycle": self.DB_POOL_RECYCLE,
|
||||
"echo": self.DB_ECHO,
|
||||
}
|
||||
|
||||
@property
|
||||
def REDIS_CONFIG(self) -> Dict[str, Any]:
|
||||
"""Get Redis configuration"""
|
||||
return {
|
||||
"url": self.REDIS_URL_WITH_DB,
|
||||
"max_connections": self.REDIS_MAX_CONNECTIONS,
|
||||
"retry_on_timeout": self.REDIS_RETRY_ON_TIMEOUT,
|
||||
"socket_keepalive": self.REDIS_SOCKET_KEEPALIVE,
|
||||
"socket_keepalive_options": self.REDIS_SOCKET_KEEPALIVE_OPTIONS,
|
||||
}
|
||||
|
||||
# ================================================================
|
||||
# CONFIGURATION LOADING
|
||||
# ================================================================
|
||||
|
||||
class Config:
|
||||
env_file = ".env"
|
||||
env_file_encoding = 'utf-8'
|
||||
case_sensitive = True
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
super().__init__(**kwargs)
|
||||
|
||||
# Validate critical settings in production
|
||||
if self.IS_PRODUCTION:
|
||||
self._validate_production_settings()
|
||||
|
||||
def _validate_production_settings(self):
|
||||
"""Validate production-specific settings"""
|
||||
critical_settings = [
|
||||
'JWT_SECRET_KEY',
|
||||
'DATABASE_URL',
|
||||
'REDIS_URL',
|
||||
'RABBITMQ_URL'
|
||||
]
|
||||
|
||||
for setting in critical_settings:
|
||||
value = getattr(self, setting)
|
||||
if not value or 'change' in value.lower() or 'default' in value.lower():
|
||||
raise ValueError(f"{setting} must be properly configured for production")
|
||||
Reference in New Issue
Block a user