Update app.py
Browse files
app.py
CHANGED
@@ -1,32 +1,35 @@
|
|
1 |
-
import
|
2 |
-
import
|
|
|
3 |
import logging
|
|
|
4 |
|
5 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
6 |
logger = logging.getLogger(__name__)
|
7 |
|
8 |
-
|
|
|
|
|
|
|
9 |
log_data = {
|
10 |
-
"method": method,
|
11 |
-
"url": url,
|
12 |
-
"headers":
|
13 |
-
"
|
|
|
14 |
}
|
15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
logger.info(log_message)
|
17 |
-
return log_message
|
18 |
|
19 |
-
|
20 |
-
fn=log_request,
|
21 |
-
inputs=[
|
22 |
-
gr.Dropdown(["GET", "POST", "PUT", "DELETE", "OPTIONS", "HEAD", "PATCH"], label="Method"),
|
23 |
-
gr.Textbox(label="URL"),
|
24 |
-
gr.Textbox(label="Headers (JSON format)"),
|
25 |
-
gr.Textbox(label="Body (JSON format)"),
|
26 |
-
],
|
27 |
-
outputs="text",
|
28 |
-
title="Request Logger",
|
29 |
-
description="Enter request details to see how they would be logged."
|
30 |
-
)
|
31 |
|
32 |
-
|
|
|
|
1 |
+
from fastapi import FastAPI, Request
|
2 |
+
from fastapi.responses import JSONResponse
|
3 |
+
import uvicorn
|
4 |
import logging
|
5 |
+
import json
|
6 |
|
7 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
8 |
logger = logging.getLogger(__name__)
|
9 |
|
10 |
+
app = FastAPI()
|
11 |
+
|
12 |
+
@app.api_route("/{full_path:path}", methods=["GET", "POST", "PUT", "DELETE", "OPTIONS", "HEAD", "PATCH"])
|
13 |
+
async def proxy(request: Request, full_path: str):
|
14 |
log_data = {
|
15 |
+
"method": request.method,
|
16 |
+
"url": str(request.url),
|
17 |
+
"headers": dict(request.headers),
|
18 |
+
"query_params": dict(request.query_params),
|
19 |
+
"client_host": request.client.host,
|
20 |
}
|
21 |
+
|
22 |
+
body = await request.body()
|
23 |
+
if body:
|
24 |
+
try:
|
25 |
+
log_data["body"] = json.loads(body)
|
26 |
+
except json.JSONDecodeError:
|
27 |
+
log_data["body"] = body.decode()
|
28 |
+
|
29 |
+
log_message = f"Intercepted request: {json.dumps(log_data, indent=2)}"
|
30 |
logger.info(log_message)
|
|
|
31 |
|
32 |
+
return JSONResponse(content={"message": "Request logged", "data": log_data})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
|
34 |
+
if __name__ == "__main__":
|
35 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|