Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,4 @@
|
|
|
|
1 |
from fastapi import FastAPI, Request
|
2 |
from fastapi.middleware.cors import CORSMiddleware
|
3 |
import logging
|
@@ -12,19 +13,14 @@ app = FastAPI()
|
|
12 |
# CORS (Cross-Origin Resource Sharing) middleware
|
13 |
app.add_middleware(
|
14 |
CORSMiddleware,
|
15 |
-
allow_origins=["*"],
|
16 |
allow_credentials=True,
|
17 |
-
allow_methods=["*"],
|
18 |
-
allow_headers=["*"],
|
19 |
)
|
20 |
|
21 |
@app.api_route("/{full_path:path}", methods=["GET", "POST", "PUT", "DELETE", "OPTIONS", "HEAD", "PATCH"])
|
22 |
async def logging_proxy(request: Request, full_path: str):
|
23 |
-
"""
|
24 |
-
Logs all incoming requests and their details, then forwards the request.
|
25 |
-
"""
|
26 |
-
|
27 |
-
# Log the request details
|
28 |
log_data = {
|
29 |
"method": request.method,
|
30 |
"url": str(request.url),
|
@@ -34,15 +30,12 @@ async def logging_proxy(request: Request, full_path: str):
|
|
34 |
}
|
35 |
|
36 |
try:
|
37 |
-
# Get the request body (if any) and log it as well
|
38 |
body = await request.body()
|
39 |
if body:
|
40 |
try:
|
41 |
-
# Attempt to decode JSON body for better logging
|
42 |
json_body = json.loads(body.decode())
|
43 |
log_data["body"] = json_body
|
44 |
except json.JSONDecodeError:
|
45 |
-
# Log the raw body if it's not JSON
|
46 |
log_data["body"] = body.decode()
|
47 |
|
48 |
logger.info(f"Incoming request: {json.dumps(log_data, indent=2)}")
|
@@ -54,4 +47,29 @@ async def logging_proxy(request: Request, full_path: str):
|
|
54 |
|
55 |
except Exception as e:
|
56 |
logger.error(f"Error processing request: {e}")
|
57 |
-
return {"error": "An error occurred while processing the request"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
from fastapi import FastAPI, Request
|
3 |
from fastapi.middleware.cors import CORSMiddleware
|
4 |
import logging
|
|
|
13 |
# CORS (Cross-Origin Resource Sharing) middleware
|
14 |
app.add_middleware(
|
15 |
CORSMiddleware,
|
16 |
+
allow_origins=["*"],
|
17 |
allow_credentials=True,
|
18 |
+
allow_methods=["*"],
|
19 |
+
allow_headers=["*"],
|
20 |
)
|
21 |
|
22 |
@app.api_route("/{full_path:path}", methods=["GET", "POST", "PUT", "DELETE", "OPTIONS", "HEAD", "PATCH"])
|
23 |
async def logging_proxy(request: Request, full_path: str):
|
|
|
|
|
|
|
|
|
|
|
24 |
log_data = {
|
25 |
"method": request.method,
|
26 |
"url": str(request.url),
|
|
|
30 |
}
|
31 |
|
32 |
try:
|
|
|
33 |
body = await request.body()
|
34 |
if body:
|
35 |
try:
|
|
|
36 |
json_body = json.loads(body.decode())
|
37 |
log_data["body"] = json_body
|
38 |
except json.JSONDecodeError:
|
|
|
39 |
log_data["body"] = body.decode()
|
40 |
|
41 |
logger.info(f"Incoming request: {json.dumps(log_data, indent=2)}")
|
|
|
47 |
|
48 |
except Exception as e:
|
49 |
logger.error(f"Error processing request: {e}")
|
50 |
+
return {"error": "An error occurred while processing the request"}
|
51 |
+
|
52 |
+
def gradio_interface(method, url, headers, body):
|
53 |
+
# Simulate a request to the FastAPI app
|
54 |
+
from fastapi.testclient import TestClient
|
55 |
+
client = TestClient(app)
|
56 |
+
|
57 |
+
headers_dict = json.loads(headers) if headers else {}
|
58 |
+
response = client.request(method, url, headers=headers_dict, data=body)
|
59 |
+
|
60 |
+
return response.json()
|
61 |
+
|
62 |
+
iface = gr.Interface(
|
63 |
+
fn=gradio_interface,
|
64 |
+
inputs=[
|
65 |
+
gr.Dropdown(["GET", "POST", "PUT", "DELETE", "OPTIONS", "HEAD", "PATCH"], label="Method"),
|
66 |
+
gr.Textbox(label="URL"),
|
67 |
+
gr.Textbox(label="Headers (JSON format)"),
|
68 |
+
gr.Textbox(label="Body"),
|
69 |
+
],
|
70 |
+
outputs="json",
|
71 |
+
title="FastAPI Logging Proxy",
|
72 |
+
description="Enter request details to see how they would be logged by the proxy."
|
73 |
+
)
|
74 |
+
|
75 |
+
gr.mount_gradio_app(app, iface, path="/")
|