about summary refs log tree commit diff
path: root/tools/nixery/server
diff options
context:
space:
mode:
authorVincent Ambo <tazjin@google.com>2019-10-03T12·02+0100
committerVincent Ambo <github@tazj.in>2019-10-03T12·21+0100
commitfeba42e40933fe932b1ca330d2c919ae018a9a7f (patch)
tree6eba82f02f1a6ffdd1c21ef64050a35f0f9a836c /tools/nixery/server
parent43a642435b653d04d730de50735d310f1f1083eb (diff)
feat(server): Fetch popularity data on launch
The last missing puzzle piece for #50!
Diffstat (limited to 'tools/nixery/server')
-rw-r--r--tools/nixery/server/main.go37
1 files changed, 37 insertions, 0 deletions
diff --git a/tools/nixery/server/main.go b/tools/nixery/server/main.go
index aeb70163da6a..b87af650650a 100644
--- a/tools/nixery/server/main.go
+++ b/tools/nixery/server/main.go
@@ -29,6 +29,7 @@ import (
 	"context"
 	"encoding/json"
 	"fmt"
+	"io/ioutil"
 	"log"
 	"net/http"
 	"regexp"
@@ -37,6 +38,7 @@ import (
 	"cloud.google.com/go/storage"
 	"github.com/google/nixery/builder"
 	"github.com/google/nixery/config"
+	"github.com/google/nixery/layers"
 )
 
 // ManifestMediaType is the Content-Type used for the manifest itself. This
@@ -96,6 +98,32 @@ func prepareBucket(ctx context.Context, cfg *config.Config) *storage.BucketHandl
 	return bkt
 }
 
+// Downloads the popularity information for the package set from the
+// URL specified in Nixery's configuration.
+func downloadPopularity(url string) (layers.Popularity, error) {
+	resp, err := http.Get(url)
+	if err != nil {
+		return nil, err
+	}
+
+	if resp.StatusCode != 200 {
+		return nil, fmt.Errorf("popularity download from '%s' returned status: %s\n", url, resp.Status)
+	}
+
+	j, err := ioutil.ReadAll(resp.Body)
+	if err != nil {
+		return nil, err
+	}
+
+	var pop layers.Popularity
+	err = json.Unmarshal(j, &pop)
+	if err != nil {
+		return nil, err
+	}
+
+	return pop, nil
+}
+
 // Error format corresponding to the registry protocol V2 specification. This
 // allows feeding back errors to clients in a way that can be presented to
 // users.
@@ -200,10 +228,19 @@ func main() {
 		log.Fatalln("Failed to instantiate build cache", err)
 	}
 
+	var pop layers.Popularity
+	if cfg.PopUrl != "" {
+		pop, err = downloadPopularity(cfg.PopUrl)
+		if err != nil {
+			log.Fatalln("Failed to fetch popularity information", err)
+		}
+	}
+
 	state := builder.State{
 		Bucket: bucket,
 		Cache:  &cache,
 		Cfg:    cfg,
+		Pop:    pop,
 	}
 
 	log.Printf("Starting Nixery on port %s\n", cfg.Port)