Update backend/app.py
Browse files- backend/app.py +62 -41
backend/app.py
CHANGED
|
@@ -1,8 +1,7 @@
|
|
| 1 |
-
from fastapi import FastAPI, HTTPException,
|
| 2 |
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
-
from fastapi.responses import FileResponse
|
| 4 |
from fastapi.staticfiles import StaticFiles
|
| 5 |
-
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
|
| 6 |
import os
|
| 7 |
from dotenv import load_dotenv
|
| 8 |
|
|
@@ -13,30 +12,8 @@ load_dotenv()
|
|
| 13 |
|
| 14 |
app = FastAPI(title="Gemini Multimodal API")
|
| 15 |
|
| 16 |
-
#
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
# Get API key from environment variable
|
| 20 |
-
API_KEY = os.environ.get("API_KEY", None)
|
| 21 |
-
|
| 22 |
-
def verify_api_key(credentials: HTTPAuthorizationCredentials = Depends(security)):
|
| 23 |
-
"""Verify API key from Authorization header"""
|
| 24 |
-
if not API_KEY:
|
| 25 |
-
return True # No key set, allow all
|
| 26 |
-
if not credentials:
|
| 27 |
-
raise HTTPException(status_code=401, detail="API key required")
|
| 28 |
-
if credentials.credentials != API_KEY:
|
| 29 |
-
raise HTTPException(status_code=403, detail="Invalid API key")
|
| 30 |
-
return True
|
| 31 |
-
|
| 32 |
-
async def verify_websocket_api_key(websocket, api_key: str = None):
|
| 33 |
-
"""Verify API key for WebSocket connections (from query param)"""
|
| 34 |
-
if not API_KEY:
|
| 35 |
-
return True
|
| 36 |
-
if not api_key or api_key != API_KEY:
|
| 37 |
-
await websocket.close(code=1008, reason="Invalid or missing API key")
|
| 38 |
-
return False
|
| 39 |
-
return True
|
| 40 |
|
| 41 |
# Add CORS middleware
|
| 42 |
app.add_middleware(
|
|
@@ -47,23 +24,67 @@ app.add_middleware(
|
|
| 47 |
allow_headers=["*"],
|
| 48 |
)
|
| 49 |
|
| 50 |
-
#
|
| 51 |
-
app.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
-
#
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
"""WebSocket endpoint with API key validation"""
|
| 61 |
-
if not await verify_websocket_api_key(websocket, api_key):
|
| 62 |
-
return
|
| 63 |
-
from routes.websocket import websocket_endpoint
|
| 64 |
-
await websocket_endpoint(websocket, client_id)
|
| 65 |
|
| 66 |
-
#
|
| 67 |
app.include_router(websocket_router)
|
| 68 |
|
| 69 |
if __name__ == "__main__":
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException, Request
|
| 2 |
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
+
from fastapi.responses import FileResponse, HTMLResponse
|
| 4 |
from fastapi.staticfiles import StaticFiles
|
|
|
|
| 5 |
import os
|
| 6 |
from dotenv import load_dotenv
|
| 7 |
|
|
|
|
| 12 |
|
| 13 |
app = FastAPI(title="Gemini Multimodal API")
|
| 14 |
|
| 15 |
+
# --- 1. Get your secret API key ---
|
| 16 |
+
APP_API_KEY = os.environ.get("API_KEY", None)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
# Add CORS middleware
|
| 19 |
app.add_middleware(
|
|
|
|
| 24 |
allow_headers=["*"],
|
| 25 |
)
|
| 26 |
|
| 27 |
+
# --- 2. A simple, password-protected landing page ---
|
| 28 |
+
@app.get("/", response_class=HTMLResponse)
|
| 29 |
+
async def landing_page(request: Request):
|
| 30 |
+
# If no API key is set in the environment, allow everyone.
|
| 31 |
+
if not APP_API_KEY:
|
| 32 |
+
return FileResponse("../frontend/index.html")
|
| 33 |
+
|
| 34 |
+
# Get the key from the URL's query parameters
|
| 35 |
+
api_key = request.query_params.get("api_key")
|
| 36 |
|
| 37 |
+
# Check if the provided key is valid
|
| 38 |
+
if api_key and api_key == APP_API_KEY:
|
| 39 |
+
# Key is valid! Serve the real application.
|
| 40 |
+
return FileResponse("../frontend/index.html")
|
| 41 |
+
else:
|
| 42 |
+
# Key is missing or invalid. Show a simple login page.
|
| 43 |
+
return """
|
| 44 |
+
<!DOCTYPE html>
|
| 45 |
+
<html>
|
| 46 |
+
<head>
|
| 47 |
+
<title>Access Required</title>
|
| 48 |
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
| 49 |
+
<style>
|
| 50 |
+
body { font-family: system-ui, sans-serif; display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; background: #f0f0f0; }
|
| 51 |
+
.card { background: white; padding: 2rem; border-radius: 1rem; box-shadow: 0 4px 6px rgba(0,0,0,0.1); text-align: center; }
|
| 52 |
+
input, button { padding: 0.5rem; margin: 0.5rem; font-size: 1rem; }
|
| 53 |
+
.error { color: red; }
|
| 54 |
+
</style>
|
| 55 |
+
</head>
|
| 56 |
+
<body>
|
| 57 |
+
<div class="card">
|
| 58 |
+
<h2>🔐 Access to Voice Assistant</h2>
|
| 59 |
+
<p>This application is protected. Please enter the access key.</p>
|
| 60 |
+
<input type="password" id="apiKeyInput" placeholder="Enter API Key" autofocus>
|
| 61 |
+
<button onclick="login()">Submit</button>
|
| 62 |
+
<div id="errorMsg" class="error"></div>
|
| 63 |
+
</div>
|
| 64 |
+
<script>
|
| 65 |
+
function login() {
|
| 66 |
+
const key = document.getElementById('apiKeyInput').value;
|
| 67 |
+
if (key) {
|
| 68 |
+
// Redirect to the same page with the key in the URL
|
| 69 |
+
window.location.href = window.location.pathname + '?api_key=' + encodeURIComponent(key);
|
| 70 |
+
} else {
|
| 71 |
+
document.getElementById('errorMsg').innerText = 'Please enter a key.';
|
| 72 |
+
}
|
| 73 |
+
}
|
| 74 |
+
// Allow pressing "Enter" key to submit
|
| 75 |
+
document.getElementById('apiKeyInput').addEventListener('keypress', function(e) {
|
| 76 |
+
if (e.key === 'Enter') login();
|
| 77 |
+
});
|
| 78 |
+
</script>
|
| 79 |
+
</body>
|
| 80 |
+
</html>
|
| 81 |
+
"""
|
| 82 |
|
| 83 |
+
# --- 3. Mount static files (these are now "inside" the protected zone) ---
|
| 84 |
+
# Users must have a valid key to load CSS, JS, etc., because these routes are checked by the middleware.
|
| 85 |
+
app.mount("/static", StaticFiles(directory="../frontend"), name="static")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
+
# --- 4. Your existing WebSocket route (already protected by your previous logic) ---
|
| 88 |
app.include_router(websocket_router)
|
| 89 |
|
| 90 |
if __name__ == "__main__":
|