1
- from flask import request , jsonify
1
+ from flask import request , jsonify , current_app as app
2
2
import requests
3
3
import json
4
4
@@ -26,21 +26,32 @@ def forward_request(request, sub, config):
26
26
:param sub: The additional path after the base URL.
27
27
:return: Response from the destination service.
28
28
"""
29
+ # Build the destination URL based on the proxy configuration and subpath
29
30
if sub is None :
30
31
destination_url = f"http://{ config ['destination' ]['host' ]} :{ config ['destination' ]['port' ]} { config ['destination' ]['path' ]} "
31
32
else :
32
- destination_url = f"http://{ config ['destination' ]['host' ]} :{ config ['destination' ]['port' ]} { config ['destination' ]['path' ]} /{ sub } "
33
-
34
- if request .method == 'GET' :
35
- response = requests .get (destination_url , headers = request .headers , params = request .args )
36
- elif request .method == 'POST' :
37
- response = requests .post (destination_url , headers = request .headers , json = request .get_json ())
38
- elif request .method == 'PUT' :
39
- response = requests .put (destination_url , headers = request .headers , json = request .get_json ())
40
- elif request .method == 'DELETE' :
41
- response = requests .delete (destination_url , headers = request .headers )
42
-
43
- return response
33
+ destination_url = f"http://{ config ['destination' ]['host' ]} :{ config ['destination' ]['port' ]} { config ['destination' ]['path' ]} { sub } "
34
+
35
+ app .logger .info (f"Forwarding { request .method } request to { destination_url } " ) # Log request forwarding
36
+
37
+ # Forward the request based on its method
38
+ try :
39
+ if request .method == 'GET' :
40
+ response = requests .get (destination_url , headers = request .headers , params = request .args )
41
+ elif request .method == 'POST' :
42
+ response = requests .post (destination_url , headers = request .headers , json = request .get_json ())
43
+ elif request .method == 'PUT' :
44
+ response = requests .put (destination_url , headers = request .headers , json = request .get_json ())
45
+ elif request .method == 'DELETE' :
46
+ response = requests .delete (destination_url , headers = request .headers )
47
+
48
+ app .logger .info (f"Received { response .status_code } from { destination_url } " ) # Log response status
49
+ return response
50
+
51
+ except requests .exceptions .RequestException as e :
52
+ # Log any exception that occurs during forwarding
53
+ app .logger .error (f"Error forwarding request to { destination_url } : { str (e )} " )
54
+ raise
44
55
45
56
def handle_proxy (config , rules_config , validate_method , validate_json , sub = None ):
46
57
"""
@@ -56,18 +67,21 @@ def handle_proxy(config, rules_config, validate_method, validate_json, sub=None)
56
67
# First, validate the method
57
68
is_valid , validation_response = validate_method ()
58
69
if not is_valid :
59
- return validation_response # Return the error response if the method is not allowed
70
+ app .logger .warning ("Method validation failed" ) # Log method validation failure
71
+ return validation_response
60
72
61
73
# Then, validate the request's JSON (if applicable)
62
74
is_valid , validation_response = validate_json ()
63
75
if not is_valid :
64
- return validation_response # Return the error response if the JSON is invalid
76
+ app .logger .warning ("JSON validation failed" ) # Log JSON validation failure
77
+ return validation_response
65
78
66
79
# If both validations pass, forward the request
67
80
try :
68
81
response = forward_request (request , sub , config )
69
82
return (response .content , response .status_code , response .headers .items ())
70
83
71
84
except Exception as e :
72
- # Handle any exception during the forwarding process
73
- return jsonify ({'error' : str (e )}), 500
85
+ # Handle any exception during the forwarding process and log it
86
+ app .logger .error (f"Error during request forwarding: { str (e )} " )
87
+ return jsonify ({'error' : str (e )}), 500
0 commit comments