Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Request
|
| 2 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
+
import uvicorn
|
| 4 |
+
import logging
|
| 5 |
+
import requests
|
| 6 |
+
import json
|
| 7 |
+
|
| 8 |
+
# Configure logging
|
| 9 |
+
logging.basicConfig(level=logging.INFO)
|
| 10 |
+
logger = logging.getLogger(__name__)
|
| 11 |
+
|
| 12 |
+
app = FastAPI()
|
| 13 |
+
|
| 14 |
+
# CORS (Cross-Origin Resource Sharing) middleware
|
| 15 |
+
app.add_middleware(
|
| 16 |
+
CORSMiddleware,
|
| 17 |
+
allow_origins=["*"], # Allow requests from any origin
|
| 18 |
+
allow_credentials=True,
|
| 19 |
+
allow_methods=["*"], # Allow all HTTP methods
|
| 20 |
+
allow_headers=["*"], # Allow all headers
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
@app.api_route("/{full_path:path}", methods=["GET", "POST", "PUT", "DELETE", "OPTIONS", "HEAD", "PATCH"])
|
| 24 |
+
async def logging_proxy(request: Request, full_path: str):
|
| 25 |
+
"""
|
| 26 |
+
Logs all incoming requests and their details, then forwards the request.
|
| 27 |
+
"""
|
| 28 |
+
|
| 29 |
+
# Log the request details
|
| 30 |
+
log_data = {
|
| 31 |
+
"method": request.method,
|
| 32 |
+
"url": str(request.url),
|
| 33 |
+
"headers": dict(request.headers),
|
| 34 |
+
"query_params": dict(request.query_params),
|
| 35 |
+
"client_host": request.client.host,
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
try:
|
| 39 |
+
# Get the request body (if any) and log it as well
|
| 40 |
+
body = await request.body()
|
| 41 |
+
if body:
|
| 42 |
+
try:
|
| 43 |
+
# Attempt to decode JSON body for better logging
|
| 44 |
+
json_body = json.loads(body.decode())
|
| 45 |
+
log_data["body"] = json_body
|
| 46 |
+
except json.JSONDecodeError:
|
| 47 |
+
# Log the raw body if it's not JSON
|
| 48 |
+
log_data["body"] = body.decode()
|
| 49 |
+
|
| 50 |
+
logger.info(f"Incoming request: {json.dumps(log_data, indent=2)}")
|
| 51 |
+
|
| 52 |
+
# Forward the request (replace with your desired forwarding logic)
|
| 53 |
+
# In this example, we just log the request and return a simple response.
|
| 54 |
+
# You can modify this part to forward to a specific URL or service.
|
| 55 |
+
# Example (modify as needed):
|
| 56 |
+
# target_url = f"https://some-backend-service.com/{full_path}"
|
| 57 |
+
# response = requests.request(
|
| 58 |
+
# method=request.method,
|
| 59 |
+
# url=target_url,
|
| 60 |
+
# headers=dict(request.headers),
|
| 61 |
+
# params=dict(request.query_params),
|
| 62 |
+
# data=body
|
| 63 |
+
# )
|
| 64 |
+
# return Response(content=response.content, status_code=response.status_code)
|
| 65 |
+
|
| 66 |
+
return {
|
| 67 |
+
"message": "Request logged successfully",
|
| 68 |
+
"logged_data": log_data
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
except Exception as e:
|
| 72 |
+
logger.error(f"Error processing request: {e}")
|
| 73 |
+
return {"error": "An error occurred while processing the request"}
|
| 74 |
+
|
| 75 |
+
if __name__ == "__main__":
|
| 76 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|