From c18ff1a270d3e5ce6c63110fb2cef78a79ad43ef Mon Sep 17 00:00:00 2001 From: Florian Klink Date: Tue, 3 Oct 2023 15:01:47 +0300 Subject: refactor(tvix/nar-bridge): deduplicate NAR HEAD and GET Use a genNarHandler() function accepting a boolean to construct the HTTP handler. Change-Id: I17c054826d91a9dbed8b1f53945a51f27fa60ace Reviewed-on: https://cl.tvl.fyi/c/depot/+/9537 Tested-by: BuildkiteCI Reviewed-by: Connor Brewster Autosubmit: flokli --- tvix/nar-bridge/pkg/http/nar_get.go | 75 +++++++++++++------------------------ 1 file changed, 26 insertions(+), 49 deletions(-) (limited to 'tvix') diff --git a/tvix/nar-bridge/pkg/http/nar_get.go b/tvix/nar-bridge/pkg/http/nar_get.go index be7eb82606..0c2b299e78 100644 --- a/tvix/nar-bridge/pkg/http/nar_get.go +++ b/tvix/nar-bridge/pkg/http/nar_get.go @@ -155,65 +155,42 @@ func renderNar( } func registerNarGet(s *Server) { - // TODO: properly compose this - s.handler.Head(narUrl, func(w http.ResponseWriter, r *http.Request) { - defer r.Body.Close() + // produce a handler for rendering NAR files. + genNarHandler := func(isHead bool) func(w http.ResponseWriter, r *http.Request) { + return func(w http.ResponseWriter, r *http.Request) { + defer r.Body.Close() - ctx := r.Context() + ctx := r.Context() - // parse the narhash sent in the request URL - narHash, err := parseNarHashFromUrl(chi.URLParamFromCtx(ctx, "narhash")) - if err != nil { - log.WithError(err).WithField("url", r.URL).Error("unable to decode nar hash from url") - w.WriteHeader(http.StatusBadRequest) - _, err := w.Write([]byte("unable to decode nar hash from url")) + // parse the narhash sent in the request URL + narHash, err := parseNarHashFromUrl(chi.URLParamFromCtx(ctx, "narhash")) if err != nil { - log.WithError(err).Errorf("unable to write error message to client") - } - - return - } - - log := log.WithField("narhash_url", narHash.SRIString()) + log.WithError(err).WithField("url", r.URL).Error("unable to decode nar hash from url") + w.WriteHeader(http.StatusBadRequest) + _, err := w.Write([]byte("unable to decode nar hash from url")) + if err != nil { + log.WithError(err).Errorf("unable to write error message to client") + } - err = renderNar(ctx, log, s.directoryServiceClient, s.blobServiceClient, &s.narHashToPathInfoMu, s.narHashToPathInfo, w, narHash, true) - if err != nil { - if errors.Is(err, fs.ErrNotExist) { - w.WriteHeader(http.StatusNotFound) - } else { - log.WithError(err).Warn("unable to render nar") - w.WriteHeader(http.StatusInternalServerError) + return } - } - }) - s.handler.Get(narUrl, func(w http.ResponseWriter, r *http.Request) { - defer r.Body.Close() + log := log.WithField("narhash_url", narHash.SRIString()) - ctx := r.Context() - - // parse the narhash sent in the request URL - narHash, err := parseNarHashFromUrl(chi.URLParamFromCtx(ctx, "narhash")) - if err != nil { - log.WithError(err).WithField("url", r.URL).Error("unable to decode nar hash from url") - w.WriteHeader(http.StatusBadRequest) - _, err := w.Write([]byte("unable to decode nar hash from url")) + // TODO: inline more of that function here? + err = renderNar(ctx, log, s.directoryServiceClient, s.blobServiceClient, &s.narHashToPathInfoMu, s.narHashToPathInfo, w, narHash, isHead) if err != nil { - log.WithError(err).Errorf("unable to write error message to client") + if errors.Is(err, fs.ErrNotExist) { + w.WriteHeader(http.StatusNotFound) + } else { + log.WithError(err).Warn("unable to render nar") + w.WriteHeader(http.StatusInternalServerError) + } } - return } + } - log := log.WithField("narhash_url", narHash.SRIString()) - - err = renderNar(ctx, log, s.directoryServiceClient, s.blobServiceClient, &s.narHashToPathInfoMu, s.narHashToPathInfo, w, narHash, false) - if err != nil { - if errors.Is(err, fs.ErrNotExist) { - w.WriteHeader(http.StatusNotFound) - } else { - w.WriteHeader(http.StatusInternalServerError) - } - } - }) + s.handler.Head(narUrl, genNarHandler(true)) + s.handler.Get(narUrl, genNarHandler(false)) } -- cgit 1.4.1