Skip to content

Commit 8464527

Browse files
committed
fix: align supabase_service.py env var fallback to SUPABASE_ANON_KEY
supabase_service.py was falling back to SUPABASE_KEY which we just removed from docker-compose. Updated to fall back to SUPABASE_ANON_KEY which is now the standardized name across all config. Closes OPE-13 (startup validation already existed, just needed alignment)
1 parent 4462c6f commit 8464527

3 files changed

Lines changed: 56 additions & 4 deletions

File tree

backend/config/startup_checks.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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, required)
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")

backend/main.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
# Import API config (single source of truth for versioning)
1818
from config.api import API_PREFIX, API_VERSION
19+
from config.startup_checks import validate_environment
1920

2021
# Import routers
2122
from routes.auth import router as auth_router
@@ -36,7 +37,7 @@
3637
# Lifespan context manager for startup/shutdown
3738
@asynccontextmanager
3839
async def lifespan(app: FastAPI):
39-
# Startup
40+
validate_environment()
4041
await load_demo_repos()
4142
yield
4243
# Shutdown (cleanup if needed)

backend/services/supabase_service.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ class SupabaseService:
1919

2020
def __init__(self):
2121
supabase_url = os.getenv("SUPABASE_URL")
22-
# Use service role key to bypass RLS for backend operations
23-
supabase_key = os.getenv("SUPABASE_SERVICE_ROLE_KEY") or os.getenv("SUPABASE_KEY")
22+
# prefer service role key for backend (bypasses RLS), fall back to anon key
23+
supabase_key = os.getenv("SUPABASE_SERVICE_ROLE_KEY") or os.getenv("SUPABASE_ANON_KEY")
2424

2525
if not supabase_url or not supabase_key:
26-
raise ValueError("SUPABASE_URL and SUPABASE_SERVICE_ROLE_KEY (or SUPABASE_KEY) must be set")
26+
raise ValueError("SUPABASE_URL and SUPABASE_SERVICE_ROLE_KEY (or SUPABASE_ANON_KEY) must be set")
2727

2828
# Create client with options to avoid auth cleanup issues
2929
options = ClientOptions(

0 commit comments

Comments
 (0)