|
| 1 | +""" |
| 2 | +Startup environment validation. |
| 3 | +Fails fast with clear error messages if required config is missing. |
| 4 | +""" |
| 5 | +import os |
| 6 | +import sys |
| 7 | +from typing import List, Tuple |
| 8 | + |
| 9 | +from services.observability import logger |
| 10 | + |
| 11 | + |
| 12 | +# (env_var_name, description) |
| 13 | +REQUIRED_VARS: List[Tuple[str, str]] = [ |
| 14 | + ("SUPABASE_URL", "Supabase project URL"), |
| 15 | + ("SUPABASE_ANON_KEY", "Supabase anon/public key"), |
| 16 | + ("SUPABASE_JWT_SECRET", "Supabase JWT secret for token verification"), |
| 17 | + ("OPENAI_API_KEY", "OpenAI API key for embeddings"), |
| 18 | + ("PINECONE_API_KEY", "Pinecone API key for vector storage"), |
| 19 | +] |
| 20 | + |
| 21 | +OPTIONAL_VARS: List[Tuple[str, str, str]] = [ |
| 22 | + ("SUPABASE_SERVICE_ROLE_KEY", "Supabase service role key", "Using anon key as fallback"), |
| 23 | + ("COHERE_API_KEY", "Cohere API key for reranking", "Search reranking disabled"), |
| 24 | + ("VOYAGE_API_KEY", "Voyage AI key for code embeddings", "Using OpenAI embeddings"), |
| 25 | + ("SENTRY_DSN", "Sentry DSN for error tracking", "Error tracking disabled"), |
| 26 | + ("REDIS_HOST", "Redis host for caching", "Using default localhost"), |
| 27 | +] |
| 28 | + |
| 29 | + |
| 30 | +def validate_environment() -> None: |
| 31 | + """Check required env vars exist. Log warnings for optional ones.""" |
| 32 | + missing: List[str] = [] |
| 33 | + |
| 34 | + for var_name, description in REQUIRED_VARS: |
| 35 | + value = os.getenv(var_name) |
| 36 | + if not value: |
| 37 | + missing.append(f" {var_name} -- {description}") |
| 38 | + |
| 39 | + if missing: |
| 40 | + msg = "Missing required environment variables:\n" + "\n".join(missing) |
| 41 | + msg += "\n\nSee .env.example for configuration reference." |
| 42 | + logger.error(msg) |
| 43 | + print(f"\n[FATAL] {msg}\n", file=sys.stderr) |
| 44 | + sys.exit(1) |
| 45 | + |
| 46 | + # warn about optional vars |
| 47 | + for var_name, description, fallback_msg in OPTIONAL_VARS: |
| 48 | + if not os.getenv(var_name): |
| 49 | + logger.warning(f"{var_name} not set ({description}). {fallback_msg}") |
| 50 | + |
| 51 | + logger.info("Environment validation passed") |
0 commit comments