Skip to content

Commit

Permalink
fix: proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
Ali-Salman29 committed Aug 9, 2024
1 parent c2f4f99 commit b6f1ffc
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 78 deletions.
Original file line number Diff line number Diff line change
@@ -1,53 +1,19 @@
# lint-amnesty, pylint: disable=cyclic-import, missing-module-docstring
from django.conf import settings

if hasattr(settings, "COMMENTS_SERVICE_URL"):
SERVICE_HOST = settings.COMMENTS_SERVICE_URL
else:
SERVICE_HOST = "http://localhost:4567"

PREFIX = SERVICE_HOST + "/api/v1"

# V2 url support for differential logging
if hasattr(settings, "COMMENTS_SERVICE_V2_URL"):
SERVICE_HOST_V2 = settings.COMMENTS_SERVICE_V2_URL
if hasattr(settings, "FORUM_V2_SERVICE_URL"):
SERVICE_HOST = settings.FORUM_V2_SERVICE_URL
else:
SERVICE_HOST_V2 = "http://localhost:8000"
SERVICE_HOST = "http://localhost:8000"

PREFIX_V2 = SERVICE_HOST_V2 + "/forum/api/v2"
PROXY_PREFIX_V2 = SERVICE_HOST_V2 + "/forum/forum_proxy/api/v1"
PREFIX = SERVICE_HOST + "/forum/api/v2"

"""
This setting is intended to help route traffic to the appropriate version of the Forum API based on the structure of the URL.

FORUM_V2_REGEXS contains regex patterns for matching ForumV2 app URLs.
The application will use the ForumV2 APIs if a ForumV1 URL matches any of these regex patterns.
The dictionary keys represent the HTTP methods and the values are lists of
regex patternsthat correspond to the endpoints for each method.
# FORUM V1 SERVICE HOST
if hasattr(settings, "COMMENTS_SERVICE_URL"):
COMMENTS_SERVICE_SERVICE_HOST = settings.COMMENTS_SERVICE_URL
else:
COMMENTS_SERVICE_SERVICE_HOST = "http://localhost:4567"

Example:
FORUM_V2_REGEXS = {
"put": [
"^/threads/([a-zA-Z0-9]+)/votes$",
# Example regex for PUT requests to update votes for a specific thread.
],
"delete": [
"^/threads/([a-zA-Z0-9]+)/votes$",
# Example regex for DELETE requests to remove votes for a specific thread.
],
"post": [
# Add regex patterns for POST requests here.
],
"get": [
# Add regex patterns for GET requests here.
],
}
"""
COMMENTS_SERVICE_PREFIX = COMMENTS_SERVICE_SERVICE_HOST + "/api/v1"

FORUM_V2_REGEXS = {
"put": [],
"delete": [],
"post": [],
"get": [],
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import requests
from django.utils.translation import get_language

from .settings import PREFIX, PREFIX_V2, PROXY_PREFIX_V2, FORUM_V2_REGEXS, SERVICE_HOST as COMMENTS_SERVICE
from .settings import PREFIX, COMMENTS_SERVICE_PREFIX, SERVICE_HOST as COMMENTS_SERVICE

log = logging.getLogger(__name__)

Expand All @@ -30,18 +30,9 @@ def extract(dic, keys):
return strip_none({k: dic.get(k) for k in keys})


def _get_forum_v2_url(url):
return url.replace(PREFIX, PREFIX_V2)
def _get_comment_service_url(url):
return url.replace(PREFIX, COMMENTS_SERVICE_PREFIX)

def _get_forum_proxy_v2_url(url):
return url.replace(PREFIX, PROXY_PREFIX_V2)

def should_use_forum_v2_urls(method, url):
path = url.replace(PREFIX, '')
for pattern in FORUM_V2_REGEXS.get(method, []):
if re.match(pattern, path):
return True
return False

def perform_request(method, url, data_or_params=None, raw=False,
metric_action=None, metric_tags=None, paged_results=False):
Expand Down Expand Up @@ -76,10 +67,6 @@ def perform_request(method, url, data_or_params=None, raw=False,
params = data_or_params.copy()
params.update(request_id_dict)

forum_v1_url = url
if should_use_forum_v2_urls(method, url):
url = _get_forum_v2_url(url)

response = requests.request(
method,
url,
Expand All @@ -89,34 +76,36 @@ def perform_request(method, url, data_or_params=None, raw=False,
timeout=config.connection_timeout
)

if method == "get":
forum_v2_url = _get_forum_proxy_v2_url(url)
if url != forum_v1_url:
response_v1 = requests.request(
method,
forum_v1_url,
data=data,
params=params,
headers=headers,
timeout=config.connection_timeout
)
else:
response_v1 = response
log.critical(
"""
======> FORUM <======
response_v2 = requests.request(
method: {method}
url: {url}
params: {params}
data: {data}
response: {response}
======> END <======
""".format(method=method, url=url, params=params, data=data, response=response.json())
)

if method == "get":
forum_v1_url = _get_comment_service_url(url)
forum_v1_response = requests.request(
method,
forum_v2_url,
forum_v1_url,
data=data,
params=params,
headers=headers,
timeout=config.connection_timeout,
)
log.info(f"requested forum proxey url: {url}")
log.info(f"requested forum v1 url: {forum_v1_url}")
log.info(f"requested forum v2 url: {forum_v2_url}")
if response_v2.json() != response_v1.json():
if forum_v1_response.json() != response.json():
log.error(
f"Forum v2 difference, for endpoint {forum_v1_url} with params={params}. \
Expected: {response.json()}. Got: {response_v2.json()}."
Expected: {forum_v1_response.json()}. Got: {response.json()}."
)

metric_tags.append(f'status_code:{response.status_code}')
Expand Down

0 comments on commit b6f1ffc

Please sign in to comment.