2014-11-11 02:57:38 +00:00
|
|
|
package registry
|
|
|
|
|
|
|
|
import (
|
2014-12-11 06:33:36 +00:00
|
|
|
"fmt"
|
2014-11-11 02:57:38 +00:00
|
|
|
"net/http"
|
|
|
|
|
2014-11-21 03:57:01 +00:00
|
|
|
"github.com/docker/docker-registry/storagedriver"
|
|
|
|
"github.com/docker/docker-registry/storagedriver/factory"
|
|
|
|
|
2014-11-11 02:57:38 +00:00
|
|
|
"github.com/docker/docker-registry/configuration"
|
2014-11-21 03:57:01 +00:00
|
|
|
"github.com/docker/docker-registry/storage"
|
2014-11-11 02:57:38 +00:00
|
|
|
|
|
|
|
log "github.com/Sirupsen/logrus"
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
)
|
|
|
|
|
|
|
|
// App is a global registry application object. Shared resources can be placed
|
|
|
|
// on this object that will be accessible from all requests. Any writable
|
|
|
|
// fields should be protected.
|
|
|
|
type App struct {
|
|
|
|
Config configuration.Configuration
|
|
|
|
|
|
|
|
router *mux.Router
|
2014-11-21 03:57:01 +00:00
|
|
|
|
|
|
|
// driver maintains the app global storage driver instance.
|
|
|
|
driver storagedriver.StorageDriver
|
|
|
|
|
|
|
|
// services contains the main services instance for the application.
|
|
|
|
services *storage.Services
|
2014-11-11 02:57:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewApp takes a configuration and returns a configured app, ready to serve
|
|
|
|
// requests. The app only implements ServeHTTP and can be wrapped in other
|
|
|
|
// handlers accordingly.
|
|
|
|
func NewApp(configuration configuration.Configuration) *App {
|
|
|
|
app := &App{
|
|
|
|
Config: configuration,
|
|
|
|
router: v2APIRouter(),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register the handler dispatchers.
|
2014-12-11 06:33:36 +00:00
|
|
|
app.register(routeNameBase, func(ctx *Context, r *http.Request) http.Handler {
|
|
|
|
return http.HandlerFunc(apiBase)
|
|
|
|
})
|
2014-11-11 02:57:38 +00:00
|
|
|
app.register(routeNameImageManifest, imageManifestDispatcher)
|
|
|
|
app.register(routeNameTags, tagsDispatcher)
|
2014-11-21 03:57:01 +00:00
|
|
|
app.register(routeNameBlob, layerDispatcher)
|
2014-11-19 03:38:14 +00:00
|
|
|
app.register(routeNameBlobUpload, layerUploadDispatcher)
|
|
|
|
app.register(routeNameBlobUploadResume, layerUploadDispatcher)
|
2014-11-11 02:57:38 +00:00
|
|
|
|
2014-11-21 03:57:01 +00:00
|
|
|
driver, err := factory.Create(configuration.Storage.Type(), configuration.Storage.Parameters())
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
// TODO(stevvooe): Move the creation of a service into a protected
|
|
|
|
// method, where this is created lazily. Its status can be queried via
|
|
|
|
// a health check.
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
app.driver = driver
|
|
|
|
app.services = storage.NewServices(app.driver)
|
|
|
|
|
2014-11-11 02:57:38 +00:00
|
|
|
return app
|
|
|
|
}
|
|
|
|
|
|
|
|
func (app *App) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
app.router.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
// register a handler with the application, by route name. The handler will be
|
|
|
|
// passed through the application filters and context will be constructed at
|
|
|
|
// request time.
|
|
|
|
func (app *App) register(routeName string, dispatch dispatchFunc) {
|
|
|
|
|
|
|
|
// TODO(stevvooe): This odd dispatcher/route registration is by-product of
|
|
|
|
// some limitations in the gorilla/mux router. We are using it to keep
|
|
|
|
// routing consistent between the client and server, but we may want to
|
|
|
|
// replace it with manual routing and structure-based dispatch for better
|
|
|
|
// control over the request execution.
|
|
|
|
|
|
|
|
app.router.GetRoute(routeName).Handler(app.dispatcher(dispatch))
|
|
|
|
}
|
|
|
|
|
|
|
|
// dispatchFunc takes a context and request and returns a constructed handler
|
|
|
|
// for the route. The dispatcher will use this to dynamically create request
|
|
|
|
// specific handlers for each endpoint without creating a new router for each
|
|
|
|
// request.
|
|
|
|
type dispatchFunc func(ctx *Context, r *http.Request) http.Handler
|
|
|
|
|
|
|
|
// TODO(stevvooe): dispatchers should probably have some validation error
|
|
|
|
// chain with proper error reporting.
|
|
|
|
|
2014-11-21 03:57:01 +00:00
|
|
|
// singleStatusResponseWriter only allows the first status to be written to be
|
|
|
|
// the valid request status. The current use case of this class should be
|
|
|
|
// factored out.
|
|
|
|
type singleStatusResponseWriter struct {
|
|
|
|
http.ResponseWriter
|
|
|
|
status int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ssrw *singleStatusResponseWriter) WriteHeader(status int) {
|
|
|
|
if ssrw.status != 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ssrw.status = status
|
|
|
|
ssrw.ResponseWriter.WriteHeader(status)
|
|
|
|
}
|
|
|
|
|
2014-11-11 02:57:38 +00:00
|
|
|
// dispatcher returns a handler that constructs a request specific context and
|
|
|
|
// handler, using the dispatch factory function.
|
|
|
|
func (app *App) dispatcher(dispatch dispatchFunc) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
context := &Context{
|
Initial implementation of Manifest HTTP API
Push, pull and delete of manifest files in the registry have been implemented
on top of the storage services. Basic workflows, including reporting of missing
manifests are tested, including various proposed response codes. Common testing
functionality has been collected into shared methods. A test suite may be
emerging but it might better to capture more edge cases (such as resumable
upload, range requests, etc.) before we commit to a full approach.
To support clearer test cases and simpler handler methods, an application aware
urlBuilder has been added. We may want to export the functionality for use in
the client, which could allow us to abstract away from gorilla/mux.
A few error codes have been added to fill in error conditions missing from the
proposal. Some use cases have identified some problems with the approach to
error reporting that requires more work to reconcile. To resolve this, the
mapping of Go errors into error types needs to pulled out of the handlers and
into the application. We also need to move to type-based errors, with rich
information, rather than value-based errors. ErrorHandlers will probably
replace the http.Handlers to make this work correctly.
Unrelated to the above, the "length" parameter has been migrated to "size" for
completing layer uploads. This change should have gone out before but these
diffs ending up being coupled with the parameter name change due to updates to
the layer unit tests.
2014-11-26 20:16:58 +00:00
|
|
|
App: app,
|
|
|
|
Name: vars["name"],
|
|
|
|
urlBuilder: newURLBuilderFromRequest(r),
|
2014-11-11 02:57:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Store vars for underlying handlers.
|
|
|
|
context.vars = vars
|
|
|
|
|
|
|
|
context.log = log.WithField("name", context.Name)
|
|
|
|
handler := dispatch(context, r)
|
|
|
|
|
2014-11-21 03:57:01 +00:00
|
|
|
ssrw := &singleStatusResponseWriter{ResponseWriter: w}
|
2014-11-11 02:57:38 +00:00
|
|
|
context.log.Infoln("handler", resolveHandlerName(r.Method, handler))
|
2014-11-21 03:57:01 +00:00
|
|
|
handler.ServeHTTP(ssrw, r)
|
2014-11-11 02:57:38 +00:00
|
|
|
|
|
|
|
// Automated error response handling here. Handlers may return their
|
|
|
|
// own errors if they need different behavior (such as range errors
|
|
|
|
// for layer upload).
|
2014-11-21 03:57:01 +00:00
|
|
|
if context.Errors.Len() > 0 {
|
|
|
|
if ssrw.status == 0 {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
}
|
2014-11-11 02:57:38 +00:00
|
|
|
serveJSON(w, context.Errors)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2014-12-11 06:33:36 +00:00
|
|
|
|
|
|
|
// apiBase implements a simple yes-man for doing overall checks against the
|
|
|
|
// api. This can support auth roundtrips to support docker login.
|
|
|
|
func apiBase(w http.ResponseWriter, r *http.Request) {
|
|
|
|
const emptyJSON = "{}"
|
|
|
|
// Provide a simple /v2/ 200 OK response with empty json response.
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.Header().Set("Content-Length", fmt.Sprint(len(emptyJSON)))
|
|
|
|
|
|
|
|
fmt.Fprint(w, emptyJSON)
|
|
|
|
}
|