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 <cbrewster@hey.com>
Autosubmit: flokli <flokli@flokli.de>
This commit is contained in:
Florian Klink 2023-10-03 15:01:47 +03:00 committed by flokli
parent 45511004df
commit c18ff1a270

View file

@ -155,65 +155,42 @@ func renderNar(
} }
func registerNarGet(s *Server) { func registerNarGet(s *Server) {
// TODO: properly compose this // produce a handler for rendering NAR files.
s.handler.Head(narUrl, func(w http.ResponseWriter, r *http.Request) { genNarHandler := func(isHead bool) func(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close() 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 // parse the narhash sent in the request URL
narHash, err := parseNarHashFromUrl(chi.URLParamFromCtx(ctx, "narhash")) 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"))
if err != nil { if err != nil {
log.WithError(err).Errorf("unable to write error message to client") 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")
}
return
} }
return log := log.WithField("narhash_url", narHash.SRIString())
}
log := log.WithField("narhash_url", narHash.SRIString()) // TODO: inline more of that function here?
err = renderNar(ctx, log, s.directoryServiceClient, s.blobServiceClient, &s.narHashToPathInfoMu, s.narHashToPathInfo, w, narHash, isHead)
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)
}
}
})
s.handler.Get(narUrl, func(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
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"))
if err != nil { 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()) s.handler.Head(narUrl, genNarHandler(true))
s.handler.Get(narUrl, genNarHandler(false))
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)
}
}
})
} }