Update proxy_server.py
Browse files- proxy_server.py +8 -22
proxy_server.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
from fastapi import FastAPI, HTTPException
|
| 2 |
-
from fastapi.staticfiles import StaticFiles
|
| 3 |
-
from fastapi.responses import FileResponse
|
| 4 |
import httpx
|
| 5 |
import os
|
| 6 |
from pydantic import BaseModel
|
|
@@ -8,7 +8,6 @@ import uvicorn
|
|
| 8 |
|
| 9 |
app = FastAPI()
|
| 10 |
|
| 11 |
-
# Pydantic model for earthquake request
|
| 12 |
class EarthquakeRequest(BaseModel):
|
| 13 |
start_date: str
|
| 14 |
end_date: str
|
|
@@ -17,44 +16,32 @@ class EarthquakeRequest(BaseModel):
|
|
| 17 |
async def proxy_to_lambda(earthquake_request: EarthquakeRequest):
|
| 18 |
"""Proxy requests to your API Gateway Lambda"""
|
| 19 |
try:
|
| 20 |
-
# Get API key from environment variables
|
| 21 |
api_key = os.environ.get('API_GATEWAY_KEY')
|
|
|
|
| 22 |
|
| 23 |
if not api_key:
|
| 24 |
raise HTTPException(status_code=500, detail="API key not configured")
|
| 25 |
-
|
| 26 |
-
# Your actual API Gateway URL
|
| 27 |
-
api_url = os.environ.get('API_GATEWAY_URL')
|
| 28 |
-
|
| 29 |
if not api_url:
|
| 30 |
raise HTTPException(status_code=500, detail="API Gateway URL not configured")
|
| 31 |
|
| 32 |
-
# Headers to send to your API Gateway
|
| 33 |
headers = {
|
| 34 |
'Content-Type': 'application/json',
|
| 35 |
'X-API-Key': api_key
|
| 36 |
}
|
| 37 |
|
| 38 |
-
# Request body
|
| 39 |
request_body = {
|
| 40 |
'start_date': earthquake_request.start_date,
|
| 41 |
'end_date': earthquake_request.end_date
|
| 42 |
}
|
| 43 |
|
| 44 |
-
# Make request to your API Gateway
|
| 45 |
async with httpx.AsyncClient(timeout=30.0) as client:
|
| 46 |
-
response = await client.post(
|
| 47 |
-
api_url,
|
| 48 |
-
json=request_body,
|
| 49 |
-
headers=headers
|
| 50 |
-
)
|
| 51 |
|
| 52 |
-
# Return the response from your API Gateway
|
| 53 |
if response.status_code == 200:
|
| 54 |
return response.json()
|
| 55 |
else:
|
| 56 |
raise HTTPException(
|
| 57 |
-
status_code=response.status_code,
|
| 58 |
detail=f"API Gateway error: {response.text}"
|
| 59 |
)
|
| 60 |
|
|
@@ -63,16 +50,15 @@ async def proxy_to_lambda(earthquake_request: EarthquakeRequest):
|
|
| 63 |
except Exception as e:
|
| 64 |
raise HTTPException(status_code=500, detail=f"Proxy error: {str(e)}")
|
| 65 |
|
| 66 |
-
# Health check endpoint
|
| 67 |
@app.get("/api/health")
|
| 68 |
async def health_check():
|
| 69 |
-
return {"status": "healthy"
|
| 70 |
|
| 71 |
-
# Mount static
|
| 72 |
if os.path.exists("dist/assets"):
|
| 73 |
app.mount("/assets", StaticFiles(directory="dist/assets"), name="assets")
|
| 74 |
|
| 75 |
-
# Serve React app
|
| 76 |
@app.get("/{full_path:path}")
|
| 77 |
async def serve_react_app(full_path: str = ""):
|
| 78 |
if full_path.startswith("api/"):
|
|
|
|
| 1 |
from fastapi import FastAPI, HTTPException
|
| 2 |
+
from fastapi.staticfiles import StaticFiles
|
| 3 |
+
from fastapi.responses import FileResponse
|
| 4 |
import httpx
|
| 5 |
import os
|
| 6 |
from pydantic import BaseModel
|
|
|
|
| 8 |
|
| 9 |
app = FastAPI()
|
| 10 |
|
|
|
|
| 11 |
class EarthquakeRequest(BaseModel):
|
| 12 |
start_date: str
|
| 13 |
end_date: str
|
|
|
|
| 16 |
async def proxy_to_lambda(earthquake_request: EarthquakeRequest):
|
| 17 |
"""Proxy requests to your API Gateway Lambda"""
|
| 18 |
try:
|
|
|
|
| 19 |
api_key = os.environ.get('API_GATEWAY_KEY')
|
| 20 |
+
api_url = os.environ.get('API_GATEWAY_URL')
|
| 21 |
|
| 22 |
if not api_key:
|
| 23 |
raise HTTPException(status_code=500, detail="API key not configured")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
if not api_url:
|
| 25 |
raise HTTPException(status_code=500, detail="API Gateway URL not configured")
|
| 26 |
|
|
|
|
| 27 |
headers = {
|
| 28 |
'Content-Type': 'application/json',
|
| 29 |
'X-API-Key': api_key
|
| 30 |
}
|
| 31 |
|
|
|
|
| 32 |
request_body = {
|
| 33 |
'start_date': earthquake_request.start_date,
|
| 34 |
'end_date': earthquake_request.end_date
|
| 35 |
}
|
| 36 |
|
|
|
|
| 37 |
async with httpx.AsyncClient(timeout=30.0) as client:
|
| 38 |
+
response = await client.post(api_url, json=request_body, headers=headers)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
|
|
|
| 40 |
if response.status_code == 200:
|
| 41 |
return response.json()
|
| 42 |
else:
|
| 43 |
raise HTTPException(
|
| 44 |
+
status_code=response.status_code,
|
| 45 |
detail=f"API Gateway error: {response.text}"
|
| 46 |
)
|
| 47 |
|
|
|
|
| 50 |
except Exception as e:
|
| 51 |
raise HTTPException(status_code=500, detail=f"Proxy error: {str(e)}")
|
| 52 |
|
|
|
|
| 53 |
@app.get("/api/health")
|
| 54 |
async def health_check():
|
| 55 |
+
return {"status": "healthy"}
|
| 56 |
|
| 57 |
+
# Mount static assets
|
| 58 |
if os.path.exists("dist/assets"):
|
| 59 |
app.mount("/assets", StaticFiles(directory="dist/assets"), name="assets")
|
| 60 |
|
| 61 |
+
# Serve React app
|
| 62 |
@app.get("/{full_path:path}")
|
| 63 |
async def serve_react_app(full_path: str = ""):
|
| 64 |
if full_path.startswith("api/"):
|