about summary refs log tree commit diff
path: root/tvix/nar-bridge/pkg/http/nar_get.go
diff options
context:
space:
mode:
authorFlorian Klink <flokli@flokli.de>2023-10-03T12·01+0300
committerflokli <flokli@flokli.de>2023-10-05T06·17+0000
commitc18ff1a270d3e5ce6c63110fb2cef78a79ad43ef (patch)
tree2fae3026cacf094786507f6ff8d12589eb5ffee1 /tvix/nar-bridge/pkg/http/nar_get.go
parent45511004dfd1c902707e83c690065b49aad64264 (diff)
refactor(tvix/nar-bridge): deduplicate NAR HEAD and GET r/6702
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 <cbrewster@hey.com>
Autosubmit: flokli <flokli@flokli.de>
Diffstat (limited to '')
-rw-r--r--tvix/nar-bridge/pkg/http/nar_get.go75
1 files changed, 26 insertions, 49 deletions
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))
 }