about summary refs log tree commit diff
path: root/fun/quinistry/main.go
diff options
context:
space:
mode:
authorVincent Ambo <tazjin@google.com>2019-12-21T00·49+0000
committerVincent Ambo <tazjin@google.com>2019-12-21T00·49+0000
commitcdf25193a4a2c3537b5fbcd8973df3b5fdba8322 (patch)
tree259670a1428bbdf419477c8427a2837f8c521648 /fun/quinistry/main.go
parent47fc60a032a939329521034fe1b2afdeace610ac (diff)
parent86ff65a36ca9e9cc9aaa6b845eecb9c6c7fa488a (diff)
merge(quinistry): Integrate at //fun/quinistry r/260
This is too historically interesting for me to lose it.
Diffstat (limited to 'fun/quinistry/main.go')
-rw-r--r--fun/quinistry/main.go57
1 files changed, 57 insertions, 0 deletions
diff --git a/fun/quinistry/main.go b/fun/quinistry/main.go
new file mode 100644
index 000000000000..50b47418d1a8
--- /dev/null
+++ b/fun/quinistry/main.go
@@ -0,0 +1,57 @@
+package main
+
+import (
+	"fmt"
+	"log"
+	"net/http"
+)
+
+func main() {
+	log.Println("Starting quinistry")
+
+	image := GetImageOfCurrentExecutable()
+
+	layerUri := fmt.Sprintf("/v2/quinistry/blobs/%s", image.LayerDigest)
+	configUri := fmt.Sprintf("/v2/quinistry/blobs/%s", image.ConfigDigest)
+
+	log.Fatal(http.ListenAndServe(":8080", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+		// Acknowledge that we speak V2
+		if r.RequestURI == "/v2/" {
+			logRequest("Acknowleding V2 API", r)
+			fmt.Fprintln(w)
+			return
+		}
+
+		// Serve manifest
+		if r.RequestURI == "/v2/quinistry/manifests/latest" {
+			logRequest("Serving manifest", r)
+			w.Header().Set(ContentType, ManifestMediaType)
+			w.Header().Add(DigestHeader, image.ManifestDigest)
+			w.Write(image.Manifest)
+			return
+		}
+
+		// Serve actual image layer
+		if r.RequestURI == layerUri {
+			logRequest("Serving image layer blob", r)
+			w.Header().Add(DigestHeader, image.LayerDigest)
+			w.Write(image.Layer)
+			return
+		}
+
+		// Serve image config
+		if r.RequestURI == configUri {
+			logRequest("Serving config", r)
+			w.Header().Set("Content-Type", ImageConfigMediaType)
+			w.Header().Set(DigestHeader, image.ConfigDigest)
+			w.Write(image.Config)
+			return
+		}
+
+		log.Printf("Unhandled request: %v\n", *r)
+	})))
+}
+
+func logRequest(msg string, r *http.Request) {
+	log.Printf("%s: %s %s\n", msg, r.Method, r.RequestURI)
+}