Corvius commited on
Commit
b3b7b46
·
verified ·
1 Parent(s): a0cc9d8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -57
app.py CHANGED
@@ -1,75 +1,31 @@
1
  import gradio as gr
2
- from fastapi import FastAPI, Request
3
- from fastapi.middleware.cors import CORSMiddleware
4
- import logging
5
  import json
 
6
 
7
- # Configure logging
8
  logging.basicConfig(level=logging.INFO)
9
  logger = logging.getLogger(__name__)
10
 
11
- app = FastAPI()
12
-
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),
27
- "headers": dict(request.headers),
28
- "query_params": dict(request.query_params),
29
- "client_host": request.client.host,
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)}")
42
-
43
- return {
44
- "message": "Request logged successfully",
45
- "logged_data": log_data
46
- }
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="/")
 
1
  import gradio as gr
 
 
 
2
  import json
3
+ import logging
4
 
 
5
  logging.basicConfig(level=logging.INFO)
6
  logger = logging.getLogger(__name__)
7
 
8
+ def log_request(method, url, headers, body):
 
 
 
 
 
 
 
 
 
 
 
 
9
  log_data = {
10
+ "method": method,
11
+ "url": url,
12
+ "headers": json.loads(headers) if headers else {},
13
+ "body": json.loads(body) if body else None
 
14
  }
15
+ logger.info(f"Incoming request: {json.dumps(log_data, indent=2)}")
16
+ return json.dumps(log_data, indent=2)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
 
18
  iface = gr.Interface(
19
+ fn=log_request,
20
  inputs=[
21
  gr.Dropdown(["GET", "POST", "PUT", "DELETE", "OPTIONS", "HEAD", "PATCH"], label="Method"),
22
  gr.Textbox(label="URL"),
23
  gr.Textbox(label="Headers (JSON format)"),
24
+ gr.Textbox(label="Body (JSON format)"),
25
  ],
26
  outputs="json",
27
+ title="Request Logger",
28
+ description="Enter request details to see how they would be logged."
29
  )
30
 
31
+ iface.launch()