22c9f45598
This changeset defines the application structure to be used for the http side of the new registry. The main components are the App and Context structs. The App context is instance global and manages global configuration and resources. Context contains request-specific resources that may be created as a by-product of an in-flight request. To latently construct per-request handlers and leverage gorilla/mux, a dispatch structure has been propped up next to the main handler flow. Without this, a router and all handlers need to be constructed on every request. By constructing handlers on each request, we ensure thread isolation and can carefully control the security context of in-flight requests. There are unit tests covering this functionality.
34 lines
726 B
Go
34 lines
726 B
Go
package registry
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/gorilla/handlers"
|
|
)
|
|
|
|
// layerDispatcher uses the request context to build a layerHandler.
|
|
func layerDispatcher(ctx *Context, r *http.Request) http.Handler {
|
|
layerHandler := &layerHandler{
|
|
Context: ctx,
|
|
TarSum: ctx.vars["tarsum"],
|
|
}
|
|
|
|
layerHandler.log = layerHandler.log.WithField("tarsum", layerHandler.TarSum)
|
|
|
|
return handlers.MethodHandler{
|
|
"GET": http.HandlerFunc(layerHandler.GetLayer),
|
|
}
|
|
}
|
|
|
|
// layerHandler serves http layer requests.
|
|
type layerHandler struct {
|
|
*Context
|
|
|
|
TarSum string
|
|
}
|
|
|
|
// GetLayer fetches the binary data from backend storage returns it in the
|
|
// response.
|
|
func (lh *layerHandler) GetLayer(w http.ResponseWriter, r *http.Request) {
|
|
|
|
}
|