about summary refs log tree commit diff
path: root/fun/quinistry/main.go
blob: 50b47418d1a8615f6b8639608904d3fa9a825bc5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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)
}