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