File size: 2,271 Bytes
511072a
e0f6914
 
511072a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e0f6914
511072a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e0f6914
511072a
 
f68b8b9
511072a
 
e0f6914
f68b8b9
511072a
 
 
 
 
 
 
 
 
e0f6914
511072a
e0f6914
17cdf06
 
 
e0f6914
17cdf06
 
 
 
 
 
511072a
17cdf06
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
from fastapi import FastAPI, HTTPException
from fastapi.staticfiles import StaticFiles
from fastapi.responses import FileResponse
import httpx
import os
from pydantic import BaseModel
import uvicorn

app = FastAPI()

class EarthquakeRequest(BaseModel):
    start_date: str
    end_date: str

@app.post("/api/proxy")
async def proxy_to_lambda(earthquake_request: EarthquakeRequest):
    """Proxy requests to your API Gateway Lambda"""
    try:
        api_key = os.environ.get('API_GATEWAY_KEY')
        api_url = os.environ.get('API_GATEWAY_URL')
        
        if not api_key:
            raise HTTPException(status_code=500, detail="API key not configured")
        if not api_url:
            raise HTTPException(status_code=500, detail="API Gateway URL not configured")
        
        headers = {
            'Content-Type': 'application/json',
            'X-API-Key': api_key
        }
        
        request_body = {
            'start_date': earthquake_request.start_date,
            'end_date': earthquake_request.end_date
        }
        
        async with httpx.AsyncClient(timeout=30.0) as client:
            response = await client.post(api_url, json=request_body, headers=headers)
            
            if response.status_code == 200:
                return response.json()
            else:
                raise HTTPException(
                    status_code=response.status_code,
                    detail=f"API Gateway error: {response.text}"
                )
                
    except httpx.TimeoutException:
        raise HTTPException(status_code=504, detail="Request timeout")
    except Exception as e:
        raise HTTPException(status_code=500, detail=f"Proxy error: {str(e)}")

@app.get("/api/health")
async def health_check():
    return {"status": "healthy"}

# Mount static assets
if os.path.exists("dist/assets"):
    app.mount("/assets", StaticFiles(directory="dist/assets"), name="assets")

# Serve React app
@app.get("/{full_path:path}")
async def serve_react_app(full_path: str = ""):
    if full_path.startswith("api/"):
        raise HTTPException(status_code=404, detail="API endpoint not found")
    return FileResponse("dist/index.html")

if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=7860)