Spaces:
Running
Running
umer6016
commited on
Commit
·
674f2e1
1
Parent(s):
c57cfaf
Inject Supabase env vars at runtime for Docker deployment
Browse files- backend/app/main.py +28 -1
- frontend/src/supabaseClient.js +15 -3
backend/app/main.py
CHANGED
|
@@ -39,6 +39,8 @@ def get_application() -> FastAPI:
|
|
| 39 |
app.mount("/assets", StaticFiles(directory=os.path.join(DIST_DIR, "assets")), name="assets")
|
| 40 |
|
| 41 |
# Catch-all for SPA routing (must be last)
|
|
|
|
|
|
|
| 42 |
@app.get("/{full_path:path}")
|
| 43 |
async def serve_spa(full_path: str):
|
| 44 |
# If API request fell through to here, return 404
|
|
@@ -48,7 +50,32 @@ def get_application() -> FastAPI:
|
|
| 48 |
# Use index.html for all other routes (SPA)
|
| 49 |
index_path = os.path.join(DIST_DIR, "index.html")
|
| 50 |
if os.path.exists(index_path):
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
return {"detail": "Frontend not found (build missing)"}
|
| 53 |
else:
|
| 54 |
print(f"⚠️ Frontend dist not found at {DIST_DIR}. Serving API only.")
|
|
|
|
| 39 |
app.mount("/assets", StaticFiles(directory=os.path.join(DIST_DIR, "assets")), name="assets")
|
| 40 |
|
| 41 |
# Catch-all for SPA routing (must be last)
|
| 42 |
+
from fastapi.responses import HTMLResponse
|
| 43 |
+
|
| 44 |
@app.get("/{full_path:path}")
|
| 45 |
async def serve_spa(full_path: str):
|
| 46 |
# If API request fell through to here, return 404
|
|
|
|
| 50 |
# Use index.html for all other routes (SPA)
|
| 51 |
index_path = os.path.join(DIST_DIR, "index.html")
|
| 52 |
if os.path.exists(index_path):
|
| 53 |
+
# READ and INJECT runtime env vars
|
| 54 |
+
try:
|
| 55 |
+
with open(index_path, "r", encoding="utf-8") as f:
|
| 56 |
+
content = f.read()
|
| 57 |
+
|
| 58 |
+
# Prepare env vars to inject
|
| 59 |
+
# Note: We map SECRET env vars to VITE_ PUBLIC vars for the frontend
|
| 60 |
+
supabase_url = os.getenv("SUPABASE_URL", "")
|
| 61 |
+
supabase_key = os.getenv("SUPABASE_ANON_KEY", "")
|
| 62 |
+
|
| 63 |
+
env_script = f"""
|
| 64 |
+
<script>
|
| 65 |
+
window.__ENV__ = {{
|
| 66 |
+
VITE_SUPABASE_URL: "{supabase_url}",
|
| 67 |
+
VITE_SUPABASE_ANON_KEY: "{supabase_key}"
|
| 68 |
+
}};
|
| 69 |
+
</script>
|
| 70 |
+
"""
|
| 71 |
+
|
| 72 |
+
# Inject before </head>
|
| 73 |
+
content = content.replace("</head>", f"{env_script}</head>")
|
| 74 |
+
return HTMLResponse(content=content)
|
| 75 |
+
except Exception as e:
|
| 76 |
+
print(f"Error serving SPA: {e}")
|
| 77 |
+
return FileResponse(index_path) # Fallback
|
| 78 |
+
|
| 79 |
return {"detail": "Frontend not found (build missing)"}
|
| 80 |
else:
|
| 81 |
print(f"⚠️ Frontend dist not found at {DIST_DIR}. Serving API only.")
|
frontend/src/supabaseClient.js
CHANGED
|
@@ -1,6 +1,18 @@
|
|
| 1 |
import { createClient } from "@supabase/supabase-js";
|
| 2 |
|
| 3 |
-
|
| 4 |
-
const
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import { createClient } from "@supabase/supabase-js";
|
| 2 |
|
| 3 |
+
// Check for runtime config (injected by backend) or build-time config (local dev)
|
| 4 |
+
const getEnv = (key) => {
|
| 5 |
+
if (typeof window !== "undefined" && window.__ENV__ && window.__ENV__[key]) {
|
| 6 |
+
return window.__ENV__[key];
|
| 7 |
+
}
|
| 8 |
+
return import.meta.env[key];
|
| 9 |
+
};
|
| 10 |
|
| 11 |
+
const supabaseUrl = getEnv("VITE_SUPABASE_URL");
|
| 12 |
+
const supabaseKey = getEnv("VITE_SUPABASE_ANON_KEY");
|
| 13 |
+
|
| 14 |
+
if (!supabaseUrl || !supabaseKey) {
|
| 15 |
+
console.warn("Supabase credentials missing! App may fail.");
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
export const supabase = createClient(supabaseUrl || "", supabaseKey || "");
|