From 39270b917aec8e79ee0faa9fe47580dd8435c19f Mon Sep 17 00:00:00 2001 From: Egari Date: Fri, 13 Dec 2024 20:50:47 +0100 Subject: [PATCH] If the module does not exist with fallback-proxy enabled early return with a 404, return 'empty' 200 result if fallback-proxy is not enabled --- internal/v3/api/release.go | 55 +++++++++++++++++++++++++++----------- 1 file changed, 39 insertions(+), 16 deletions(-) diff --git a/internal/v3/api/release.go b/internal/v3/api/release.go index 09a8c75..4dc123e 100644 --- a/internal/v3/api/release.go +++ b/internal/v3/api/release.go @@ -5,7 +5,6 @@ import ( "encoding/base64" "errors" "fmt" - "github.com/dadav/gorge/internal/log" "net/http" "net/url" "os" @@ -14,6 +13,7 @@ import ( "strings" "github.com/dadav/gorge/internal/config" + "github.com/dadav/gorge/internal/log" "github.com/dadav/gorge/internal/v3/backend" "github.com/dadav/gorge/internal/v3/utils" gen "github.com/dadav/gorge/pkg/gen/v3/openapi" @@ -264,10 +264,27 @@ func (s *ReleaseOperationsApi) GetReleases(ctx context.Context, limit int32, off results := []gen.Release{} filtered := []*gen.Release{} allReleases, _ := backend.ConfiguredBackend.GetAllReleases() + + base, _ := url.Parse("/v3/releases") params := url.Values{} + + filterSet := false + + if module != "" { + filterSet = true + params.Add("module", module) + } + + if owner != "" { + filterSet = true + params.Add("owner", owner) + } + params.Add("offset", strconv.Itoa(int(offset))) params.Add("limit", strconv.Itoa(int(limit))) - filterSet := false + + base.RawQuery = params.Encode() + currentInf := interface{}(base.String()) // We know there's no releases and a fallback proxy, so we should return a 404 to let the proxy handle it if config.FallbackProxyUrl != "" && len(allReleases) == 0 { @@ -280,26 +297,33 @@ func (s *ReleaseOperationsApi) GetReleases(ctx context.Context, limit int32, off } if module != "" { - filterSet = true - params.Add("module", module) - - // Perform an early query to see if the module even exists in the backend + // Perform an early query to see if the module even exists in the backend, optimization for instances with _many_ modules _, err := backend.ConfiguredBackend.GetModuleBySlug(module) if err != nil { log.Log.Debugf("Could not find module with slug '%s' in backend, returning 404 so we can proxy if desired\n", module) - return gen.Response(http.StatusNotFound, GetRelease404Response{ - Message: "No releases found", - Errors: []string{"No module(s) found for given query."}, - }), nil + if config.FallbackProxyUrl != "" { + return gen.Response(http.StatusNotFound, GetRelease404Response{ + Message: "No releases found", + Errors: []string{"No module(s) found for given query."}, + }), nil + } else { + return gen.Response(http.StatusOK, gen.GetReleases200Response{ + Pagination: gen.GetReleases200ResponsePagination{ + Limit: limit, + Offset: offset, + First: ¤tInf, + Previous: nil, + Current: ¤tInf, + Next: nil, + Total: 0, + }, + Results: []gen.Release{}, + }), nil + } } } - if owner != "" { - filterSet = true - params.Add("owner", owner) - } - prefiltered := []*gen.Release{} if len(allReleases) > int(offset) { prefiltered = allReleases[offset:] @@ -346,7 +370,6 @@ func (s *ReleaseOperationsApi) GetReleases(ctx context.Context, limit int32, off }), nil } - base, _ := url.Parse("/v3/releases") base.RawQuery = params.Encode() currentInf := interface{}(base.String()) params.Set("offset", "0")