2025-07-19 21:44:52 +02:00
|
|
|
# shared/config/base.py
|
|
|
|
|
"""
|
|
|
|
|
Base configuration for all microservices
|
|
|
|
|
Provides common settings and patterns
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
import os
|
2025-10-23 07:44:54 +02:00
|
|
|
from typing import List, Dict, Optional, Any, Set
|
2025-07-19 21:44:52 +02:00
|
|
|
from pydantic_settings import BaseSettings
|
2025-10-01 12:17:59 +02:00
|
|
|
from pydantic import validator, Field
|
2025-07-19 21:44:52 +02:00
|
|
|
|
|
|
|
|
|
2025-10-23 07:44:54 +02:00
|
|
|
# ================================================================
|
|
|
|
|
# INTERNAL SERVICE REGISTRY
|
|
|
|
|
# ================================================================
|
|
|
|
|
|
|
|
|
|
# Central registry of all internal microservices that should have
|
|
|
|
|
# automatic access to tenant resources without user membership
|
|
|
|
|
# Service names should match the naming convention used in JWT tokens
|
|
|
|
|
INTERNAL_SERVICES: Set[str] = {
|
|
|
|
|
# Core services
|
|
|
|
|
"auth-service",
|
|
|
|
|
"tenant-service",
|
2026-01-12 14:24:14 +01:00
|
|
|
"gateway", # API Gateway
|
|
|
|
|
"gateway-service", # Alternative name for gateway
|
2025-10-23 07:44:54 +02:00
|
|
|
|
|
|
|
|
# Business logic services
|
|
|
|
|
"inventory-service",
|
|
|
|
|
"production-service",
|
|
|
|
|
"recipes-service",
|
|
|
|
|
"suppliers-service",
|
|
|
|
|
"pos-service",
|
|
|
|
|
"orders-service",
|
|
|
|
|
"sales-service",
|
2026-01-12 14:24:14 +01:00
|
|
|
"procurement-service",
|
2025-10-23 07:44:54 +02:00
|
|
|
|
|
|
|
|
# ML and analytics services
|
|
|
|
|
"training-service",
|
|
|
|
|
"forecasting-service",
|
2026-01-12 14:24:14 +01:00
|
|
|
"ai-insights-service",
|
|
|
|
|
|
|
|
|
|
# Orchestration services
|
|
|
|
|
"orchestrator-service",
|
2025-10-23 07:44:54 +02:00
|
|
|
|
|
|
|
|
# Support services
|
|
|
|
|
"notification-service",
|
|
|
|
|
"alert-service",
|
|
|
|
|
"alert-processor-service",
|
2026-01-12 14:24:14 +01:00
|
|
|
"alert-processor", # Alternative name (from k8s service name)
|
2025-10-23 07:44:54 +02:00
|
|
|
"demo-session-service",
|
2025-12-14 16:04:16 +01:00
|
|
|
"demo-service", # Alternative name for demo session service
|
2025-10-23 07:44:54 +02:00
|
|
|
"external-service",
|
|
|
|
|
|
2025-11-30 09:12:40 +01:00
|
|
|
# Enterprise services
|
|
|
|
|
"distribution-service",
|
2025-10-23 07:44:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def is_internal_service(service_identifier: str) -> bool:
|
|
|
|
|
"""
|
|
|
|
|
Check if a service identifier represents an internal service.
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
service_identifier: Service name (e.g., 'production-service')
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
bool: True if the identifier is a recognized internal service
|
|
|
|
|
"""
|
|
|
|
|
return service_identifier in INTERNAL_SERVICES
|
|
|
|
|
|
|
|
|
|
|
2025-07-19 21:44:52 +02:00
|
|
|
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
|
|
|
|
|
# ================================================================
|
2025-09-27 12:10:43 +02:00
|
|
|
|
|
|
|
|
# Note: DATABASE_URL is defined as a property in each service-specific config
|
|
|
|
|
# to construct the URL from secure environment variables
|
2025-07-19 21:44:52 +02:00
|
|
|
|
|
|
|
|
# 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"))
|
2025-10-09 14:11:02 +02:00
|
|
|
DB_POOL_PRE_PING: bool = os.getenv("DB_POOL_PRE_PING", "true").lower() == "true"
|
2025-07-19 21:44:52 +02:00
|
|
|
DB_ECHO: bool = os.getenv("DB_ECHO", "false").lower() == "true"
|
|
|
|
|
|
|
|
|
|
# ================================================================
|
|
|
|
|
# REDIS CONFIGURATION
|
|
|
|
|
# ================================================================
|
2025-10-01 12:17:59 +02:00
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def REDIS_URL(self) -> str:
|
2025-10-19 19:22:37 +02:00
|
|
|
"""Build Redis URL from secure components with TLS support"""
|
2025-10-01 12:17:59 +02:00
|
|
|
# Try complete URL first (for backward compatibility)
|
|
|
|
|
complete_url = os.getenv("REDIS_URL")
|
|
|
|
|
if complete_url:
|
2025-10-19 19:22:37 +02:00
|
|
|
# Upgrade to TLS if not already
|
|
|
|
|
if complete_url.startswith("redis://") and "tls" not in complete_url.lower():
|
|
|
|
|
complete_url = complete_url.replace("redis://", "rediss://", 1)
|
2025-10-01 12:17:59 +02:00
|
|
|
return complete_url
|
|
|
|
|
|
2025-10-19 19:22:37 +02:00
|
|
|
# Build from components (secure approach with TLS)
|
2025-10-01 12:17:59 +02:00
|
|
|
password = os.getenv("REDIS_PASSWORD", "")
|
|
|
|
|
host = os.getenv("REDIS_HOST", "redis-service")
|
|
|
|
|
port = os.getenv("REDIS_PORT", "6379")
|
2025-10-19 19:22:37 +02:00
|
|
|
use_tls = os.getenv("REDIS_TLS_ENABLED", "true").lower() == "true"
|
|
|
|
|
|
|
|
|
|
# Use rediss:// for TLS, redis:// for non-TLS
|
|
|
|
|
protocol = "rediss" if use_tls else "redis"
|
2025-10-01 12:17:59 +02:00
|
|
|
|
|
|
|
|
# DEBUG: print what we're using
|
|
|
|
|
import sys
|
2025-10-19 19:22:37 +02:00
|
|
|
print(f"[DEBUG REDIS_URL] password={repr(password)}, host={host}, port={port}, tls={use_tls}", file=sys.stderr)
|
2025-10-01 12:17:59 +02:00
|
|
|
|
|
|
|
|
if password:
|
2025-10-19 19:22:37 +02:00
|
|
|
url = f"{protocol}://:{password}@{host}:{port}"
|
|
|
|
|
if use_tls:
|
|
|
|
|
# Use ssl_cert_reqs=none for self-signed certs in internal cluster
|
|
|
|
|
# Still encrypted, just skips cert validation
|
|
|
|
|
url += "?ssl_cert_reqs=none"
|
|
|
|
|
print(f"[DEBUG REDIS_URL] Returning URL with auth and TLS: {url}", file=sys.stderr)
|
2025-10-01 12:17:59 +02:00
|
|
|
return url
|
2025-10-19 19:22:37 +02:00
|
|
|
url = f"{protocol}://{host}:{port}"
|
|
|
|
|
if use_tls:
|
|
|
|
|
# Use ssl_cert_reqs=none for self-signed certs in internal cluster
|
|
|
|
|
url += "?ssl_cert_reqs=none"
|
2025-10-01 12:17:59 +02:00
|
|
|
print(f"[DEBUG REDIS_URL] Returning URL without auth: {url}", file=sys.stderr)
|
|
|
|
|
return url
|
|
|
|
|
|
2025-07-19 21:44:52 +02:00
|
|
|
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,
|
|
|
|
|
}
|
2025-10-01 12:17:59 +02:00
|
|
|
|
2025-07-19 21:44:52 +02:00
|
|
|
@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
|
|
|
|
|
# ================================================================
|
2025-09-27 12:10:43 +02:00
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def RABBITMQ_URL(self) -> str:
|
|
|
|
|
"""Build RabbitMQ URL from secure components"""
|
|
|
|
|
# Try complete URL first (for backward compatibility)
|
|
|
|
|
complete_url = os.getenv("RABBITMQ_URL")
|
|
|
|
|
if complete_url:
|
|
|
|
|
return complete_url
|
|
|
|
|
|
|
|
|
|
# Build from components (secure approach)
|
|
|
|
|
user = os.getenv("RABBITMQ_USER", "bakery")
|
|
|
|
|
password = os.getenv("RABBITMQ_PASSWORD", "forecast123")
|
|
|
|
|
host = os.getenv("RABBITMQ_HOST", "rabbitmq-service")
|
|
|
|
|
port = os.getenv("RABBITMQ_PORT", "5672")
|
|
|
|
|
vhost = os.getenv("RABBITMQ_VHOST", "/")
|
|
|
|
|
|
|
|
|
|
return f"amqp://{user}:{password}@{host}:{port}{vhost}"
|
2025-07-19 21:44:52 +02:00
|
|
|
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
|
2026-01-12 14:24:14 +01:00
|
|
|
# ✅ FIXED: Use production JWT secret key to match auth service
|
|
|
|
|
# Must be same across all services for inter-service communication
|
|
|
|
|
JWT_SECRET_KEY: str = os.getenv("JWT_SECRET_KEY", "usMHw9kQCQoyrc7wPmMi3bClr0lTY9wvzZmcTbADvL0=")
|
2025-07-19 21:44:52 +02:00
|
|
|
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"))
|
|
|
|
|
|
2026-01-12 14:24:14 +01:00
|
|
|
|
2025-07-19 21:44:52 +02:00
|
|
|
|
|
|
|
|
# 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)
|
2025-09-28 13:54:28 +02:00
|
|
|
GATEWAY_URL: str = os.getenv("GATEWAY_URL", "http://gateway-service:8000")
|
2025-07-19 21:44:52 +02:00
|
|
|
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")
|
2025-08-12 18:17:30 +02:00
|
|
|
SALES_SERVICE_URL: str = os.getenv("SALES_SERVICE_URL", "http://sales-service:8000")
|
|
|
|
|
EXTERNAL_SERVICE_URL: str = os.getenv("EXTERNAL_SERVICE_URL", "http://external-service:8000")
|
2025-07-19 21:44:52 +02:00
|
|
|
TENANT_SERVICE_URL: str = os.getenv("TENANT_SERVICE_URL", "http://tenant-service:8000")
|
2025-08-14 13:26:59 +02:00
|
|
|
INVENTORY_SERVICE_URL: str = os.getenv("INVENTORY_SERVICE_URL", "http://inventory-service:8000")
|
2025-07-19 21:44:52 +02:00
|
|
|
NOTIFICATION_SERVICE_URL: str = os.getenv("NOTIFICATION_SERVICE_URL", "http://notification-service:8000")
|
2025-08-23 16:30:45 +02:00
|
|
|
PRODUCTION_SERVICE_URL: str = os.getenv("PRODUCTION_SERVICE_URL", "http://bakery-production-service:8000")
|
|
|
|
|
ORDERS_SERVICE_URL: str = os.getenv("ORDERS_SERVICE_URL", "http://bakery-orders-service:8000")
|
2025-09-04 23:19:53 +02:00
|
|
|
SUPPLIERS_SERVICE_URL: str = os.getenv("SUPPLIERS_SERVICE_URL", "http://bakery-suppliers-service:8000")
|
2025-09-19 21:39:04 +02:00
|
|
|
RECIPES_SERVICE_URL: str = os.getenv("RECIPES_SERVICE_URL", "http://recipes-service:8000")
|
2025-10-07 07:15:07 +02:00
|
|
|
POS_SERVICE_URL: str = os.getenv("POS_SERVICE_URL", "http://pos-service:8000")
|
2025-07-22 17:01:12 +02:00
|
|
|
NOMINATIM_SERVICE_URL: str = os.getenv("NOMINATIM_SERVICE_URL", "http://nominatim:8080")
|
2025-10-03 14:09:34 +02:00
|
|
|
DEMO_SESSION_SERVICE_URL: str = os.getenv("DEMO_SESSION_SERVICE_URL", "http://demo-session-service:8000")
|
2025-12-05 20:07:01 +01:00
|
|
|
ALERT_PROCESSOR_SERVICE_URL: str = os.getenv("ALERT_PROCESSOR_SERVICE_URL", "http://alert-processor:8000")
|
2025-10-30 21:08:07 +01:00
|
|
|
PROCUREMENT_SERVICE_URL: str = os.getenv("PROCUREMENT_SERVICE_URL", "http://procurement-service:8000")
|
2025-10-31 11:54:19 +01:00
|
|
|
ORCHESTRATOR_SERVICE_URL: str = os.getenv("ORCHESTRATOR_SERVICE_URL", "http://orchestrator-service:8000")
|
2025-11-05 13:34:56 +01:00
|
|
|
AI_INSIGHTS_SERVICE_URL: str = os.getenv("AI_INSIGHTS_SERVICE_URL", "http://ai-insights-service:8000")
|
2025-11-30 09:12:40 +01:00
|
|
|
DISTRIBUTION_SERVICE_URL: str = os.getenv("DISTRIBUTION_SERVICE_URL", "http://distribution-service:8000")
|
2025-10-21 19:50:07 +02:00
|
|
|
|
2025-07-19 21:44:52 +02:00
|
|
|
# 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", "")
|
|
|
|
|
|
2025-09-25 14:30:47 +02:00
|
|
|
# Stripe Payment Configuration
|
|
|
|
|
STRIPE_PUBLISHABLE_KEY: str = os.getenv("STRIPE_PUBLISHABLE_KEY", "")
|
|
|
|
|
STRIPE_SECRET_KEY: str = os.getenv("STRIPE_SECRET_KEY", "")
|
|
|
|
|
STRIPE_WEBHOOK_SECRET: str = os.getenv("STRIPE_WEBHOOK_SECRET", "")
|
|
|
|
|
|
2025-07-19 21:44:52 +02:00
|
|
|
# ================================================================
|
|
|
|
|
# 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"
|
2025-10-21 19:50:07 +02:00
|
|
|
|
|
|
|
|
# ================================================================
|
|
|
|
|
# PROCUREMENT AUTOMATION
|
|
|
|
|
# ================================================================
|
|
|
|
|
|
2025-10-23 07:44:54 +02:00
|
|
|
# NOTE: Tenant-specific procurement settings (auto-approval thresholds, supplier scores,
|
|
|
|
|
# approval rules, lead times, forecast days, etc.) have been moved to TenantSettings.
|
|
|
|
|
# Services should fetch these using TenantSettingsClient from shared/utils/tenant_settings_client.py
|
2025-10-21 19:50:07 +02:00
|
|
|
|
2025-10-23 07:44:54 +02:00
|
|
|
# System-level procurement settings (apply to all tenants):
|
|
|
|
|
AUTO_CREATE_POS_FROM_PLAN: bool = os.getenv("AUTO_CREATE_POS_FROM_PLAN", "true").lower() == "true"
|
|
|
|
|
PROCUREMENT_TEST_MODE: bool = os.getenv("PROCUREMENT_TEST_MODE", "false").lower() == "true"
|
2025-10-21 19:50:07 +02:00
|
|
|
SEND_AUTO_APPROVAL_SUMMARY: bool = os.getenv("SEND_AUTO_APPROVAL_SUMMARY", "true").lower() == "true"
|
|
|
|
|
AUTO_APPROVAL_SUMMARY_TIME_HOUR: int = int(os.getenv("AUTO_APPROVAL_SUMMARY_TIME_HOUR", "18"))
|
|
|
|
|
|
2025-07-19 21:44:52 +02:00
|
|
|
# ================================================================
|
|
|
|
|
# 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('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,
|
2025-08-12 18:17:30 +02:00
|
|
|
"sales": self.SALES_SERVICE_URL,
|
|
|
|
|
"external": self.EXTERNAL_SERVICE_URL,
|
2025-07-19 21:44:52 +02:00
|
|
|
"tenant": self.TENANT_SERVICE_URL,
|
2025-08-14 13:26:59 +02:00
|
|
|
"inventory": self.INVENTORY_SERVICE_URL,
|
2025-07-19 21:44:52 +02:00
|
|
|
"notification": self.NOTIFICATION_SERVICE_URL,
|
2025-08-23 16:30:45 +02:00
|
|
|
"production": self.PRODUCTION_SERVICE_URL,
|
|
|
|
|
"orders": self.ORDERS_SERVICE_URL,
|
2025-09-04 23:19:53 +02:00
|
|
|
"suppliers": self.SUPPLIERS_SERVICE_URL,
|
2025-09-19 21:39:04 +02:00
|
|
|
"recipes": self.RECIPES_SERVICE_URL,
|
2025-07-19 21:44:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@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,
|
2025-10-09 14:11:02 +02:00
|
|
|
"pool_pre_ping": self.DB_POOL_PRE_PING,
|
2025-07-19 21:44:52 +02:00
|
|
|
"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():
|
2025-09-25 14:30:47 +02:00
|
|
|
raise ValueError(f"{setting} must be properly configured for production")
|