debug logs

This commit is contained in:
Sebastian
2025-03-06 07:45:52 +01:00
parent 4c6bf353ae
commit 110a31e52b

View File

@@ -1,7 +1,11 @@
import os
import logging
from flask import Flask, request, Response
import requests
# Setup logging
logging.basicConfig(level=logging.DEBUG, format="%(asctime)s [%(levelname)s] %(message)s")
app = Flask(__name__)
# Read config from environment variables
@@ -12,7 +16,7 @@ TARGET_URL = f"https://lb.drpc.org/rest/eth-beacon-chain-{NETWORK}"
@app.route('/<path:subpath>', methods=["GET", "POST", "PUT", "DELETE", "PATCH"])
def proxy(subpath):
url = f"{TARGET_URL}/{subpath}"
# Forward query params and add the dkey
params = request.args.to_dict()
params["dkey"] = DKEY
@@ -21,13 +25,29 @@ def proxy(subpath):
headers = {k: v for k, v in request.headers if k.lower() != "host"}
data = request.get_data() if request.method in ["POST", "PUT", "PATCH"] else None
# Forward the request
resp = requests.request(
method=request.method, url=url, params=params, headers=headers, data=data
)
# Debug logs
logging.debug(f"Incoming request: {request.method} {request.path}")
logging.debug(f"Forwarding request to: {url}")
logging.debug(f"Headers: {headers}")
logging.debug(f"Query Params: {params}")
if data:
logging.debug(f"Request Body: {data.decode('utf-8')}")
# Return response with original status and headers
return Response(resp.content, resp.status_code, resp.headers.items())
try:
# Forward the request
resp = requests.request(
method=request.method, url=url, params=params, headers=headers, data=data
)
logging.debug(f"Response Status: {resp.status_code}")
logging.debug(f"Response Headers: {dict(resp.headers)}")
logging.debug(f"Response Body: {resp.text[:500]}") # Limit log size
return Response(resp.content, resp.status_code, resp.headers.items())
except requests.RequestException as e:
logging.error(f"Request failed: {e}")
return Response(f"Error: {str(e)}", status=500)
if __name__ == "__main__":
app.run(host="0.0.0.0", port=80)
logging.info(f"Starting proxy on port 80, forwarding to {TARGET_URL}")
app.run(host="0.0.0.0", port=80, debug=True)