tvl-depot/fun/quinistry/main.go

58 lines
1.3 KiB
Go
Raw Permalink Normal View History

2017-03-15 23:45:13 +01:00
package main
import (
"fmt"
"log"
"net/http"
)
func main() {
2017-03-16 11:43:17 +01:00
log.Println("Starting quinistry")
image := GetImageOfCurrentExecutable()
2017-03-15 23:45:13 +01:00
layerUri := fmt.Sprintf("/v2/quinistry/blobs/%s", image.LayerDigest)
configUri := fmt.Sprintf("/v2/quinistry/blobs/%s", image.ConfigDigest)
2017-03-15 23:45:13 +01:00
log.Fatal(http.ListenAndServe(":8080", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Acknowledge that we speak V2
if r.RequestURI == "/v2/" {
2017-03-16 11:43:17 +01:00
logRequest("Acknowleding V2 API", r)
2017-03-15 23:45:13 +01:00
fmt.Fprintln(w)
return
}
// Serve manifest
if r.RequestURI == "/v2/quinistry/manifests/latest" {
2017-03-16 11:43:17 +01:00
logRequest("Serving manifest", r)
w.Header().Set(ContentType, ManifestMediaType)
w.Header().Add(DigestHeader, image.ManifestDigest)
w.Write(image.Manifest)
2017-03-15 23:45:13 +01:00
return
}
// Serve actual image layer
2017-03-16 11:43:17 +01:00
if r.RequestURI == layerUri {
logRequest("Serving image layer blob", r)
w.Header().Add(DigestHeader, image.LayerDigest)
w.Write(image.Layer)
2017-03-16 11:43:17 +01:00
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)
2017-03-15 23:45:13 +01:00
return
}
2017-03-16 11:43:17 +01:00
log.Printf("Unhandled request: %v\n", *r)
2017-03-15 23:45:13 +01:00
})))
}
2017-03-16 11:43:17 +01:00
func logRequest(msg string, r *http.Request) {
log.Printf("%s: %s %s\n", msg, r.Method, r.RequestURI)
}