Skip to content

Commit

Permalink
cloud: Add kcidb_cache_redirect function
Browse files Browse the repository at this point in the history
  • Loading branch information
octonawish-akcodes authored and spbnick committed Aug 6, 2023
1 parent 7b25056 commit 5415713
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 0 deletions.
11 changes: 11 additions & 0 deletions cloud
Original file line number Diff line number Diff line change
Expand Up @@ -1474,6 +1474,17 @@ function cloud_functions_deploy() {
--max-instances=1 \
--timeout 540

cloud_function_deploy "$project" "${prefix}cache_redirect" \
"$source" \
--entry-point kcidb_cache_redirect \
--env-vars-file "$env_yaml_file" \
--runtime python37 \
--trigger-http \
--allow-unauthenticated \
--memory 256MB \
--max-instances=1 \
--timeout 540

cloud_function_deploy "$project" "${prefix}load_queue" \
"$source" \
--entry-point kcidb_load_queue \
Expand Down
47 changes: 47 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import datetime
import logging
import smtplib
from urllib.parse import unquote
import functions_framework
import kcidb

# Name of the Google Cloud project we're deployed in
Expand Down Expand Up @@ -357,3 +359,48 @@ def kcidb_cache_urls(event, context):
# Cache each URL
for url in pubsub_message.splitlines():
cache.store(url)


@functions_framework.http
def kcidb_cache_redirect(request):
"""
Handle the cache redirection for incoming HTTP GET requests.
This function takes an HTTP request and processes it for
cache redirection. If the request is a GET request,
it extracts the URL from the request, checks if the URL exists
in the cache, and performs a redirect if necessary.
Args:
request (object): The HTTP request object.
Returns:
tuple: A tuple containing the response body, status code,
and headers epresenting the redirect response.
"""
if request.method == 'GET':
url_to_fetch = unquote(request.query_string.decode("ascii"))
LOGGER.debug("URL %s", url_to_fetch)

if not url_to_fetch:
# If the URL is empty, return a 400 (Bad Request) error
response_body = "Put a valid URL to query from \
the caching system."
return (response_body, 400, {})

# Check if the URL is in the cache
cache_client = get_cache_client()
cache = cache_client.map(url_to_fetch)
if cache:
LOGGER.debug("Redirecting to the cache at %s", cache)
# Redirect to the cached URL if it exists
return ("", 302, {"Location": cache})

# If the URL is not in the cache or not provided,
# redirect to the original URL
LOGGER.debug("Redirecting to the origin at %s", url_to_fetch)
return ("", 302, {"Location": url_to_fetch})

# If the request method is not GET, return 405 (Method Not Allowed) error
response_body = "Method not allowed."
return (response_body, 405, {'Allow': 'GET'})
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ google-cloud-firestore
google-cloud-storage
google-cloud-secret-manager
psycopg2
functions-framework
importlib_metadata
jsonschema[format]
requests
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"google-cloud-bigquery",
"google-cloud-pubsub",
"google-cloud-storage",
"functions-framework",
"google-cloud-firestore",
"google-cloud-secret-manager",
"psycopg2",
Expand Down

0 comments on commit 5415713

Please sign in to comment.