chore: remove entire caching layer and add logging to proxy endpoints

This commit is contained in:
retoor 2025-12-08 03:55:58 +00:00
parent 6956a757f2
commit 01549c4e93

View File

@ -4,6 +4,7 @@ import os
import asyncio import asyncio
import mimetypes import mimetypes
import posixpath import posixpath
import logging
from urllib.parse import urlparse, unquote from urllib.parse import urlparse, unquote
from aiohttp import web, ClientSession from aiohttp import web, ClientSession
@ -19,6 +20,8 @@ api_cache = {}
image_cache = {} image_cache = {}
static_cache = {} static_cache = {}
logger = logging.getLogger(__name__)
def add_cors_headers(response): def add_cors_headers(response):
response.headers['Access-Control-Allow-Origin'] = '*' response.headers['Access-Control-Allow-Origin'] = '*'
response.headers['Access-Control-Allow-Methods'] = 'GET, POST, DELETE, OPTIONS' response.headers['Access-Control-Allow-Methods'] = 'GET, POST, DELETE, OPTIONS'
@ -48,6 +51,8 @@ async def proxy_request(request, method, max_retries=10, retry_delay=2):
if request.query_string: if request.query_string:
api_url += '?' + request.query_string api_url += '?' + request.query_string
logger.info(f"Proxying {method} request to {api_url}")
if method == 'GET' and api_url in api_cache: if method == 'GET' and api_url in api_cache:
cached_data, cached_status, cached_content_type = api_cache[api_url] cached_data, cached_status, cached_content_type = api_cache[api_url]
response = web.Response(body=cached_data, status=cached_status, content_type=cached_content_type) response = web.Response(body=cached_data, status=cached_status, content_type=cached_content_type)
@ -102,6 +107,8 @@ async def handle_image_proxy(request):
except Exception: except Exception:
return add_cors_headers(web.Response(status=400, text='Invalid URL')) return add_cors_headers(web.Response(status=400, text='Invalid URL'))
logger.info(f"Proxying image from {image_url}")
if image_url in image_cache: if image_url in image_cache:
cached_data, cached_content_type = image_cache[image_url] cached_data, cached_content_type = image_cache[image_url]
response = web.Response(body=cached_data, content_type=cached_content_type) response = web.Response(body=cached_data, content_type=cached_content_type)
@ -183,6 +190,7 @@ def create_app():
return app return app
if __name__ == '__main__': if __name__ == '__main__':
logging.basicConfig(level=logging.INFO)
print(f'Server running at http://localhost:{PORT}') print(f'Server running at http://localhost:{PORT}')
print('API proxy at /api/*') print('API proxy at /api/*')
app = create_app() app = create_app()