From e22daf115697811c30b634c7d8f2061fc13b8595 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antti=20M=C3=A4ki?= Date: Fri, 27 Sep 2024 12:16:05 +0300 Subject: [PATCH] Support mirror CDNs for chunked package list index download Calls to the API endpoint can indicate that a mirror CDN is preferred by including a query parameter in the request. The endpoint will return an URL to the preferred CDN only if the CDN is allowlisted in the config. This feature is intended to mitigate the issues of the CDN provider. --- django/thunderstore/repository/api/v1/views/listing_index.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/django/thunderstore/repository/api/v1/views/listing_index.py b/django/thunderstore/repository/api/v1/views/listing_index.py index c9ff535b1..159dfa7a4 100644 --- a/django/thunderstore/repository/api/v1/views/listing_index.py +++ b/django/thunderstore/repository/api/v1/views/listing_index.py @@ -5,6 +5,7 @@ from rest_framework.views import APIView from thunderstore.community.models import Community +from thunderstore.core.utils import replace_cdn from thunderstore.repository.models import APIV1ChunkedPackageCache @@ -28,6 +29,8 @@ def get(self, request: Request, community_identifier: str): cache = APIV1ChunkedPackageCache.get_latest_for_community(community) if cache: - return redirect(request.build_absolute_uri(cache.index.data_url)) + url = request.build_absolute_uri(cache.index.data_url) + url = replace_cdn(url, request.query_params.get("cdn")) + return redirect(url) return Response({"error": "No cache available"}, status=503)