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:
parent
45511004df
commit
c18ff1a270
1 changed files with 27 additions and 50 deletions
|
@ -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")
|
||||
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())
|
||||
|
||||
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"))
|
||||
// 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))
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue