Removed cache.
Some checks failed
CI / test (3.10) (push) Failing after 23s
CI / build (push) Failing after 23s
CI / test (3.8) (push) Failing after 22s
CI / test (3.12) (push) Failing after 22s
CI / lint (push) Failing after 23s
CI / test (3.11) (push) Failing after 41s
CI / test (3.9) (push) Failing after 41s
Some checks failed
CI / test (3.10) (push) Failing after 23s
CI / build (push) Failing after 23s
CI / test (3.8) (push) Failing after 22s
CI / test (3.12) (push) Failing after 22s
CI / lint (push) Failing after 23s
CI / test (3.11) (push) Failing after 41s
CI / test (3.9) (push) Failing after 41s
This commit is contained in:
parent
bb66801c09
commit
2078c1115a
8
proxy.py
8
proxy.py
@ -4,6 +4,7 @@ import os
|
||||
import asyncio
|
||||
import mimetypes
|
||||
import posixpath
|
||||
import logging
|
||||
from urllib.parse import urlparse, unquote
|
||||
from aiohttp import web, ClientSession
|
||||
|
||||
@ -19,6 +20,8 @@ api_cache = {}
|
||||
image_cache = {}
|
||||
static_cache = {}
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
def add_cors_headers(response):
|
||||
response.headers['Access-Control-Allow-Origin'] = '*'
|
||||
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:
|
||||
api_url += '?' + request.query_string
|
||||
|
||||
logger.info(f"Proxying {method} request to {api_url}")
|
||||
|
||||
if method == 'GET' and api_url in api_cache:
|
||||
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)
|
||||
@ -102,6 +107,8 @@ async def handle_image_proxy(request):
|
||||
except Exception:
|
||||
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:
|
||||
cached_data, cached_content_type = image_cache[image_url]
|
||||
response = web.Response(body=cached_data, content_type=cached_content_type)
|
||||
@ -183,6 +190,7 @@ def create_app():
|
||||
return app
|
||||
|
||||
if __name__ == '__main__':
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
print(f'Server running at http://localhost:{PORT}')
|
||||
print('API proxy at /api/*')
|
||||
app = create_app()
|
||||
|
||||
Loading…
Reference in New Issue
Block a user