diff options
Diffstat (limited to 'tvix/nar-bridge/pkg/http')
-rw-r--r-- | tvix/nar-bridge/pkg/http/nar_get.go | 197 | ||||
-rw-r--r-- | tvix/nar-bridge/pkg/http/nar_put.go | 141 | ||||
-rw-r--r-- | tvix/nar-bridge/pkg/http/narinfo.go | 51 | ||||
-rw-r--r-- | tvix/nar-bridge/pkg/http/narinfo_get.go | 137 | ||||
-rw-r--r-- | tvix/nar-bridge/pkg/http/narinfo_put.go | 103 | ||||
-rw-r--r-- | tvix/nar-bridge/pkg/http/server.go | 119 | ||||
-rw-r--r-- | tvix/nar-bridge/pkg/http/util.go | 24 |
7 files changed, 0 insertions, 772 deletions
diff --git a/tvix/nar-bridge/pkg/http/nar_get.go b/tvix/nar-bridge/pkg/http/nar_get.go deleted file mode 100644 index 75797f8da90e..000000000000 --- a/tvix/nar-bridge/pkg/http/nar_get.go +++ /dev/null @@ -1,197 +0,0 @@ -package http - -import ( - "bytes" - "context" - "encoding/base64" - "encoding/hex" - "errors" - "fmt" - "io" - "io/fs" - "net/http" - "sync" - - castorev1pb "code.tvl.fyi/tvix/castore-go" - storev1pb "code.tvl.fyi/tvix/store-go" - "github.com/go-chi/chi/v5" - nixhash "github.com/nix-community/go-nix/pkg/hash" - "github.com/nix-community/go-nix/pkg/nixbase32" - log "github.com/sirupsen/logrus" -) - -const ( - narUrl = "/nar/{narhash:^([" + nixbase32.Alphabet + "]{52})$}.nar" -) - -func renderNar( - ctx context.Context, - log *log.Entry, - directoryServiceClient castorev1pb.DirectoryServiceClient, - blobServiceClient castorev1pb.BlobServiceClient, - narHashDbMu *sync.Mutex, - narHashDb map[string]*narData, - w io.Writer, - narHash *nixhash.Hash, - headOnly bool, -) error { - // look in the lookup table - narHashDbMu.Lock() - narData, found := narHashDb[narHash.SRIString()] - narHashDbMu.Unlock() - - rootNode := narData.rootNode - - // if we didn't find anything, return 404. - if !found { - return fmt.Errorf("narHash not found: %w", fs.ErrNotExist) - } - - // if this was only a head request, we're done. - if headOnly { - return nil - } - - directories := make(map[string]*castorev1pb.Directory) - - // If the root node is a directory, ask the directory service for all directories - if pathInfoDirectory := rootNode.GetDirectory(); pathInfoDirectory != nil { - rootDirectoryDigest := pathInfoDirectory.GetDigest() - log = log.WithField("root_directory", base64.StdEncoding.EncodeToString(rootDirectoryDigest)) - - directoryStream, err := directoryServiceClient.Get(ctx, &castorev1pb.GetDirectoryRequest{ - ByWhat: &castorev1pb.GetDirectoryRequest_Digest{ - Digest: rootDirectoryDigest, - }, - Recursive: true, - }) - if err != nil { - return fmt.Errorf("unable to query directory stream: %w", err) - } - - // For now, we just stream all of these locally and put them into a hashmap, - // which is used in the lookup function below. - for { - directory, err := directoryStream.Recv() - if err != nil { - if err == io.EOF { - break - } - return fmt.Errorf("unable to receive from directory stream: %w", err) - } - - // calculate directory digest - // TODO: do we need to do any more validation? - directoryDgst, err := directory.Digest() - if err != nil { - return fmt.Errorf("unable to calculate directory digest: %w", err) - } - - log.WithField("directory", base64.StdEncoding.EncodeToString(directoryDgst)).Debug("received directory node") - - directories[hex.EncodeToString(directoryDgst)] = directory - } - - } - - // render the NAR file - err := storev1pb.Export( - w, - rootNode, - func(directoryDigest []byte) (*castorev1pb.Directory, error) { - log.WithField("directory", base64.StdEncoding.EncodeToString(directoryDigest)).Debug("Get directory") - directoryRefStr := hex.EncodeToString(directoryDigest) - directory, found := directories[directoryRefStr] - if !found { - return nil, fmt.Errorf( - "directory with hash %v does not exist: %w", - directoryDigest, - fs.ErrNotExist, - ) - } - - return directory, nil - }, - func(blobDigest []byte) (io.ReadCloser, error) { - log.WithField("blob", base64.StdEncoding.EncodeToString(blobDigest)).Debug("Get blob") - resp, err := blobServiceClient.Read(ctx, &castorev1pb.ReadBlobRequest{ - Digest: blobDigest, - }) - if err != nil { - return nil, fmt.Errorf("unable to get blob: %w", err) - } - - // set up a pipe, let a goroutine write, return the reader. - pR, pW := io.Pipe() - - go func() { - for { - chunk, err := resp.Recv() - if errors.Is(err, io.EOF) { - break - } - if err != nil { - pW.CloseWithError(fmt.Errorf("receiving chunk: %w", err)) - return - } - - // write the received chunk to the writer part of the pipe - if _, err := io.Copy(pW, bytes.NewReader(chunk.GetData())); err != nil { - log.WithError(err).Error("writing chunk to pipe") - pW.CloseWithError(fmt.Errorf("writing chunk to pipe: %w", err)) - return - } - } - pW.Close() - - }() - - return io.NopCloser(pR), nil - }, - ) - if err != nil { - return fmt.Errorf("unable to export nar: %w", err) - } - return nil -} - -func registerNarGet(s *Server) { - // 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() - - // 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 { - log.WithError(err).Errorf("unable to write error message to client") - } - - return - } - - log := log.WithField("narhash_url", narHash.SRIString()) - - // TODO: inline more of that function here? - err = renderNar(ctx, log, s.directoryServiceClient, s.blobServiceClient, &s.narDbMu, s.narDb, w, narHash, isHead) - 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.Head(narUrl, genNarHandler(true)) - s.handler.Get(narUrl, genNarHandler(false)) -} diff --git a/tvix/nar-bridge/pkg/http/nar_put.go b/tvix/nar-bridge/pkg/http/nar_put.go deleted file mode 100644 index fdfa20f9c396..000000000000 --- a/tvix/nar-bridge/pkg/http/nar_put.go +++ /dev/null @@ -1,141 +0,0 @@ -package http - -import ( - "bufio" - "bytes" - "fmt" - "net/http" - - castorev1pb "code.tvl.fyi/tvix/castore-go" - "code.tvl.fyi/tvix/nar-bridge/pkg/importer" - "github.com/go-chi/chi/v5" - mh "github.com/multiformats/go-multihash/core" - nixhash "github.com/nix-community/go-nix/pkg/hash" - "github.com/sirupsen/logrus" - log "github.com/sirupsen/logrus" -) - -func registerNarPut(s *Server) { - s.handler.Put(narUrl, func(w http.ResponseWriter, r *http.Request) { - defer r.Body.Close() - - ctx := r.Context() - - // parse the narhash sent in the request URL - narHashFromUrl, 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 { - log.WithError(err).Error("unable to write error message to client") - } - - return - } - - log := log.WithField("narhash_url", narHashFromUrl.SRIString()) - - directoriesUploader := importer.NewDirectoriesUploader(ctx, s.directoryServiceClient) - defer directoriesUploader.Done() //nolint:errcheck - - rootNode, narSize, narSha256, err := importer.Import( - ctx, - // buffer the body by 10MiB - bufio.NewReaderSize(r.Body, 10*1024*1024), - importer.GenBlobUploaderCb(ctx, s.blobServiceClient), - func(directory *castorev1pb.Directory) ([]byte, error) { - return directoriesUploader.Put(directory) - }, - ) - - if err != nil { - log.Errorf("error during NAR import: %v", err) - w.WriteHeader(http.StatusInternalServerError) - _, err := w.Write([]byte(fmt.Sprintf("error during NAR import: %v", err))) - if err != nil { - log.WithError(err).Errorf("unable to write error message to client") - } - - return - } - - log.Debug("closing the stream") - - // Close the directories uploader - directoriesPutResponse, err := directoriesUploader.Done() - if err != nil { - log.WithError(err).Error("error during directory upload") - w.WriteHeader(http.StatusBadRequest) - _, err := w.Write([]byte("error during directory upload")) - if err != nil { - log.WithError(err).Errorf("unable to write error message to client") - } - - return - } - // If we uploaded directories (so directoriesPutResponse doesn't return null), - // the RootDigest field in directoriesPutResponse should match the digest - // returned in the PathInfo struct returned by the `Import` call. - // This check ensures the server-side came up with the same root hash. - - if directoriesPutResponse != nil { - rootDigestPathInfo := rootNode.GetDirectory().GetDigest() - rootDigestDirectoriesPutResponse := directoriesPutResponse.GetRootDigest() - - log := log.WithFields(logrus.Fields{ - "root_digest_pathinfo": rootDigestPathInfo, - "root_digest_directories_put_resp": rootDigestDirectoriesPutResponse, - }) - if !bytes.Equal(rootDigestPathInfo, rootDigestDirectoriesPutResponse) { - log.Errorf("returned root digest doesn't match what's calculated") - - w.WriteHeader(http.StatusBadRequest) - _, err := w.Write([]byte("error in root digest calculation")) - if err != nil { - log.WithError(err).Error("unable to write error message to client") - } - - return - } - } - - // Compare the nar hash specified in the URL with the one that has been - // calculated while processing the NAR file. - narHash, err := nixhash.FromHashTypeAndDigest(mh.SHA2_256, narSha256) - if err != nil { - panic("must parse nixbase32") - } - - if !bytes.Equal(narHashFromUrl.Digest(), narHash.Digest()) { - log := log.WithFields(logrus.Fields{ - "narhash_received_sha256": narHash.SRIString(), - "narsize": narSize, - }) - log.Error("received bytes don't match narhash from URL") - - w.WriteHeader(http.StatusBadRequest) - _, err := w.Write([]byte("received bytes don't match narHash specified in URL")) - if err != nil { - log.WithError(err).Errorf("unable to write error message to client") - } - - return - } - - // Insert the partial pathinfo structs into our lookup map, - // so requesting the NAR file will be possible. - // The same might exist already, but it'll have the same contents (so - // replacing will be a no-op), except maybe the root node Name field value, which - // is safe to ignore (as not part of the NAR). - s.narDbMu.Lock() - s.narDb[narHash.SRIString()] = &narData{ - rootNode: rootNode, - narSize: narSize, - } - s.narDbMu.Unlock() - - // Done! - }) - -} diff --git a/tvix/nar-bridge/pkg/http/narinfo.go b/tvix/nar-bridge/pkg/http/narinfo.go deleted file mode 100644 index e5b99a9505f1..000000000000 --- a/tvix/nar-bridge/pkg/http/narinfo.go +++ /dev/null @@ -1,51 +0,0 @@ -package http - -import ( - "fmt" - - storev1pb "code.tvl.fyi/tvix/store-go" - mh "github.com/multiformats/go-multihash/core" - nixhash "github.com/nix-community/go-nix/pkg/hash" - - "github.com/nix-community/go-nix/pkg/narinfo" - "github.com/nix-community/go-nix/pkg/narinfo/signature" - "github.com/nix-community/go-nix/pkg/nixbase32" -) - -// ToNixNarInfo converts the PathInfo to a narinfo.NarInfo. -func ToNixNarInfo(p *storev1pb.PathInfo) (*narinfo.NarInfo, error) { - // ensure the PathInfo is valid, and extract the StorePath from the node in - // there. - storePath, err := p.Validate() - if err != nil { - return nil, fmt.Errorf("failed to validate PathInfo: %w", err) - } - - // convert the signatures from storev1pb signatures to narinfo signatures - narinfoSignatures := make([]signature.Signature, len(p.GetNarinfo().GetSignatures())) - for i, pathInfoSignature := range p.GetNarinfo().GetSignatures() { - narinfoSignatures[i] = signature.Signature{ - Name: pathInfoSignature.GetName(), - Data: pathInfoSignature.GetData(), - } - } - - // produce nixhash for the narsha256. - narHash, err := nixhash.FromHashTypeAndDigest( - mh.SHA2_256, - p.GetNarinfo().GetNarSha256(), - ) - if err != nil { - return nil, fmt.Errorf("invalid narsha256: %w", err) - } - - return &narinfo.NarInfo{ - StorePath: storePath.Absolute(), - URL: "nar/" + nixbase32.EncodeToString(narHash.Digest()) + ".nar", - Compression: "none", - NarHash: narHash, - NarSize: uint64(p.GetNarinfo().GetNarSize()), - References: p.GetNarinfo().GetReferenceNames(), - Signatures: narinfoSignatures, - }, nil -} diff --git a/tvix/nar-bridge/pkg/http/narinfo_get.go b/tvix/nar-bridge/pkg/http/narinfo_get.go deleted file mode 100644 index d43cb58078da..000000000000 --- a/tvix/nar-bridge/pkg/http/narinfo_get.go +++ /dev/null @@ -1,137 +0,0 @@ -package http - -import ( - "context" - "encoding/base64" - "errors" - "fmt" - "io" - "io/fs" - "net/http" - "strings" - "sync" - - storev1pb "code.tvl.fyi/tvix/store-go" - "github.com/go-chi/chi/v5" - nixhash "github.com/nix-community/go-nix/pkg/hash" - "github.com/nix-community/go-nix/pkg/nixbase32" - log "github.com/sirupsen/logrus" - "google.golang.org/grpc/codes" - "google.golang.org/grpc/status" -) - -// renderNarinfo writes narinfo contents to a passed io.Writer, or a returns a -// (wrapped) io.ErrNoExist error if something doesn't exist. -// if headOnly is set to true, only the existence is checked, but no content is -// actually written. -func renderNarinfo( - ctx context.Context, - log *log.Entry, - pathInfoServiceClient storev1pb.PathInfoServiceClient, - narHashToPathInfoMu *sync.Mutex, - narHashToPathInfo map[string]*narData, - outputHash []byte, - w io.Writer, - headOnly bool, -) error { - pathInfo, err := pathInfoServiceClient.Get(ctx, &storev1pb.GetPathInfoRequest{ - ByWhat: &storev1pb.GetPathInfoRequest_ByOutputHash{ - ByOutputHash: outputHash, - }, - }) - if err != nil { - st, ok := status.FromError(err) - if ok { - if st.Code() == codes.NotFound { - return fmt.Errorf("output hash %v not found: %w", base64.StdEncoding.EncodeToString(outputHash), fs.ErrNotExist) - } - return fmt.Errorf("unable to get pathinfo, code %v: %w", st.Code(), err) - } - - return fmt.Errorf("unable to get pathinfo: %w", err) - } - - log = log.WithField("pathInfo", pathInfo) - - if _, err := pathInfo.Validate(); err != nil { - log.WithError(err).Error("unable to validate PathInfo") - - return fmt.Errorf("unable to validate PathInfo: %w", err) - } - - if pathInfo.GetNarinfo() == nil { - log.Error("PathInfo doesn't contain Narinfo field") - - return fmt.Errorf("PathInfo doesn't contain Narinfo field") - } - - // extract the NARHash. This must succeed, as Validate() did succeed. - narHash, err := nixhash.FromHashTypeAndDigest(0x12, pathInfo.GetNarinfo().GetNarSha256()) - if err != nil { - panic("must parse NarHash") - } - - // add things to the lookup table, in case the same process didn't handle the NAR hash yet. - narHashToPathInfoMu.Lock() - narHashToPathInfo[narHash.SRIString()] = &narData{ - rootNode: pathInfo.GetNode(), - narSize: pathInfo.GetNarinfo().GetNarSize(), - } - narHashToPathInfoMu.Unlock() - - if headOnly { - return nil - } - - // convert the PathInfo to NARInfo. - narInfo, err := ToNixNarInfo(pathInfo) - - // Write it out to the client. - _, err = io.Copy(w, strings.NewReader(narInfo.String())) - if err != nil { - return fmt.Errorf("unable to write narinfo to client: %w", err) - } - - return nil -} - -func registerNarinfoGet(s *Server) { - // GET/HEAD $outHash.narinfo looks up the PathInfo from the tvix-store, - // and, if it's a GET request, render a .narinfo file to the client. - // In both cases it will keep the PathInfo in the lookup map, - // so a subsequent GET/HEAD /nar/ $narhash.nar request can find it. - genNarinfoHandler := 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() - log := log.WithField("outputhash", chi.URLParamFromCtx(ctx, "outputhash")) - - // parse the output hash sent in the request URL - outputHash, err := nixbase32.DecodeString(chi.URLParamFromCtx(ctx, "outputhash")) - if err != nil { - log.WithError(err).Error("unable to decode output hash from url") - w.WriteHeader(http.StatusBadRequest) - _, err := w.Write([]byte("unable to decode output hash from url")) - if err != nil { - log.WithError(err).Errorf("unable to write error message to client") - } - - return - } - - err = renderNarinfo(ctx, log, s.pathInfoServiceClient, &s.narDbMu, s.narDb, outputHash, w, isHead) - if err != nil { - if errors.Is(err, fs.ErrNotExist) { - w.WriteHeader(http.StatusNotFound) - } else { - log.WithError(err).Warn("unable to render narinfo") - w.WriteHeader(http.StatusInternalServerError) - } - } - } - } - - s.handler.Get("/{outputhash:^["+nixbase32.Alphabet+"]{32}}.narinfo", genNarinfoHandler(false)) - s.handler.Head("/{outputhash:^["+nixbase32.Alphabet+"]{32}}.narinfo", genNarinfoHandler(true)) -} diff --git a/tvix/nar-bridge/pkg/http/narinfo_put.go b/tvix/nar-bridge/pkg/http/narinfo_put.go deleted file mode 100644 index fd588bec8644..000000000000 --- a/tvix/nar-bridge/pkg/http/narinfo_put.go +++ /dev/null @@ -1,103 +0,0 @@ -package http - -import ( - "net/http" - - "code.tvl.fyi/tvix/nar-bridge/pkg/importer" - "github.com/go-chi/chi/v5" - "github.com/nix-community/go-nix/pkg/narinfo" - "github.com/nix-community/go-nix/pkg/nixbase32" - "github.com/sirupsen/logrus" - log "github.com/sirupsen/logrus" -) - -func registerNarinfoPut(s *Server) { - s.handler.Put("/{outputhash:^["+nixbase32.Alphabet+"]{32}}.narinfo", func(w http.ResponseWriter, r *http.Request) { - defer r.Body.Close() - - ctx := r.Context() - log := log.WithField("outputhash", chi.URLParamFromCtx(ctx, "outputhash")) - - // TODO: decide on merging behaviour. - // Maybe it's fine to add if contents are the same, but more sigs can be added? - // Right now, just replace a .narinfo for a path that already exists. - - // read and parse the .narinfo file - narInfo, err := narinfo.Parse(r.Body) - if err != nil { - log.WithError(err).Error("unable to parse narinfo") - w.WriteHeader(http.StatusBadRequest) - _, err := w.Write([]byte("unable to parse narinfo")) - if err != nil { - log.WithError(err).Errorf("unable to write error message to client") - } - - return - } - - log = log.WithFields(logrus.Fields{ - "narhash": narInfo.NarHash.SRIString(), - "output_path": narInfo.StorePath, - }) - - // look up the narHash in our temporary map - s.narDbMu.Lock() - narData, found := s.narDb[narInfo.NarHash.SRIString()] - s.narDbMu.Unlock() - if !found { - log.Error("unable to find referred NAR") - w.WriteHeader(http.StatusBadRequest) - _, err := w.Write([]byte("unable to find referred NAR")) - if err != nil { - log.WithError(err).Errorf("unable to write error message to client") - } - - return - } - - rootNode := narData.rootNode - - // compare fields with what we computed while receiving the NAR file - - // NarSize needs to match - if narData.narSize != narInfo.NarSize { - log.Error("narsize mismatch") - w.WriteHeader(http.StatusBadRequest) - _, err := w.Write([]byte("unable to parse narinfo")) - if err != nil { - log.WithError(err).Errorf("unable to write error message to client") - } - - return - } - - pathInfo, err := importer.GenPathInfo(rootNode, narInfo) - if err != nil { - log.WithError(err).Error("unable to generate PathInfo") - - w.WriteHeader(http.StatusInternalServerError) - _, err := w.Write([]byte("unable to generate PathInfo")) - if err != nil { - log.WithError(err).Errorf("unable to write error message to client") - } - - return - } - - log.WithField("pathInfo", pathInfo).Debug("inserted new pathInfo") - - receivedPathInfo, err := s.pathInfoServiceClient.Put(ctx, pathInfo) - if err != nil { - log.WithError(err).Error("unable to upload pathinfo to service") - w.WriteHeader(http.StatusInternalServerError) - _, err := w.Write([]byte("unable to upload pathinfo to server")) - if err != nil { - log.WithError(err).Errorf("unable to write error message to client") - } - - return - } - - log.WithField("pathInfo", receivedPathInfo).Debug("got back PathInfo") - }) -} diff --git a/tvix/nar-bridge/pkg/http/server.go b/tvix/nar-bridge/pkg/http/server.go deleted file mode 100644 index fbcb20be18b7..000000000000 --- a/tvix/nar-bridge/pkg/http/server.go +++ /dev/null @@ -1,119 +0,0 @@ -package http - -import ( - "context" - "fmt" - "net" - "net/http" - "strings" - "sync" - "time" - - castorev1pb "code.tvl.fyi/tvix/castore-go" - storev1pb "code.tvl.fyi/tvix/store-go" - "github.com/go-chi/chi/middleware" - "github.com/go-chi/chi/v5" - log "github.com/sirupsen/logrus" - "go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp" -) - -type Server struct { - srv *http.Server - handler chi.Router - - directoryServiceClient castorev1pb.DirectoryServiceClient - blobServiceClient castorev1pb.BlobServiceClient - pathInfoServiceClient storev1pb.PathInfoServiceClient - - // When uploading NAR files to a HTTP binary cache, the .nar - // files are uploaded before the .narinfo files. - // We need *both* to be able to fully construct a PathInfo object. - // Keep a in-memory map of narhash(es) (in SRI) to (unnamed) root node and nar - // size. - // This is necessary until we can ask a PathInfoService for a node with a given - // narSha256. - narDbMu sync.Mutex - narDb map[string]*narData -} - -type narData struct { - rootNode *castorev1pb.Node - narSize uint64 -} - -func New( - directoryServiceClient castorev1pb.DirectoryServiceClient, - blobServiceClient castorev1pb.BlobServiceClient, - pathInfoServiceClient storev1pb.PathInfoServiceClient, - enableAccessLog bool, - priority int, -) *Server { - r := chi.NewRouter() - r.Use(func(h http.Handler) http.Handler { - return otelhttp.NewHandler(h, "http.request") - }) - - if enableAccessLog { - r.Use(middleware.Logger) - } - - r.Get("/", func(w http.ResponseWriter, r *http.Request) { - _, err := w.Write([]byte("nar-bridge")) - if err != nil { - log.Errorf("Unable to write response: %v", err) - } - }) - - r.Get("/nix-cache-info", func(w http.ResponseWriter, r *http.Request) { - _, err := w.Write([]byte(fmt.Sprintf("StoreDir: /nix/store\nWantMassQuery: 1\nPriority: %d\n", priority))) - if err != nil { - log.Errorf("Unable to write response: %v", err) - } - }) - - s := &Server{ - handler: r, - directoryServiceClient: directoryServiceClient, - blobServiceClient: blobServiceClient, - pathInfoServiceClient: pathInfoServiceClient, - narDb: make(map[string]*narData), - } - - registerNarPut(s) - registerNarinfoPut(s) - - registerNarinfoGet(s) - registerNarGet(s) - - return s -} - -func (s *Server) Shutdown(ctx context.Context) error { - return s.srv.Shutdown(ctx) -} - -// ListenAndServer starts the webserver, and waits for it being closed or -// shutdown, after which it'll return ErrServerClosed. -func (s *Server) ListenAndServe(addr string) error { - s.srv = &http.Server{ - Handler: s.handler, - ReadTimeout: 500 * time.Second, - WriteTimeout: 500 * time.Second, - IdleTimeout: 500 * time.Second, - } - - var listener net.Listener - var err error - - // check addr. If it contains slashes, assume it's a unix domain socket. - if strings.Contains(addr, "/") { - listener, err = net.Listen("unix", addr) - } else { - listener, err = net.Listen("tcp", addr) - } - if err != nil { - return fmt.Errorf("unable to listen on %v: %w", addr, err) - } - - return s.srv.Serve(listener) -} diff --git a/tvix/nar-bridge/pkg/http/util.go b/tvix/nar-bridge/pkg/http/util.go deleted file mode 100644 index 60febea1f430..000000000000 --- a/tvix/nar-bridge/pkg/http/util.go +++ /dev/null @@ -1,24 +0,0 @@ -package http - -import ( - "fmt" - nixhash "github.com/nix-community/go-nix/pkg/hash" -) - -// parseNarHashFromUrl parses a nixbase32 string representing a sha256 NarHash -// and returns a nixhash.Hash when it was able to parse, or an error. -func parseNarHashFromUrl(narHashFromUrl string) (*nixhash.Hash, error) { - // peek at the length. If it's 52 characters, assume sha256, - // if it's something else, this is an error. - l := len(narHashFromUrl) - if l != 52 { - return nil, fmt.Errorf("invalid length of narHash: %v", l) - } - - nixHash, err := nixhash.ParseNixBase32("sha256:" + narHashFromUrl) - if err != nil { - return nil, fmt.Errorf("unable to parse nixbase32 hash: %w", err) - } - - return nixHash, nil -} |