-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
47 lines (41 loc) · 1.48 KB
/
main.py
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
from fastapi import FastAPI, Request
from fastapi.middleware.cors import CORSMiddleware
import logging
app = FastAPI()
# Configure logging
logging.basicConfig(level=logging.INFO)
# Configure CORS
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # Allow all origins
allow_credentials=True,
allow_methods=["*"], # Allow all methods
allow_headers=["*"], # Allow all headers
)
@app.middleware("http")
async def log_requests(request: Request, call_next):
"""Log details of each request."""
# Log the request details
logging.info(f"Request: {request.method} {request.url}")
logging.info(f"Headers: {request.headers}")
logging.info(f"Query Params: {request.query_params}")
logging.info(f"Body: {await request.body()}")
logging.info(f"IP: {request.client.host}")
# Process the request
response = await call_next(request)
# Log the response status code
logging.info(f"Response status: {response.status_code}")
return response
@app.api_route("/{path:path}", methods=["GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS", "HEAD"])
async def catch_all(path: str, request: Request):
"""Handle any path and method."""
# Optionally return details about the request
return {
"message": "Request received",
"method": request.method,
"path": path,
"headers": dict(request.headers),
"query_params": dict(request.query_params),
"body": await request.body(),
"ip": request.client.host
}