routes.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package v2
  2. import "github.com/gorilla/mux"
  3. // The following are definitions of the name under which all V2 routes are
  4. // registered. These symbols can be used to look up a route based on the name.
  5. const (
  6. RouteNameBase = "base"
  7. RouteNameManifest = "manifest"
  8. RouteNameTags = "tags"
  9. RouteNameBlob = "blob"
  10. RouteNameBlobUpload = "blob-upload"
  11. RouteNameBlobUploadChunk = "blob-upload-chunk"
  12. RouteNameCatalog = "catalog"
  13. )
  14. var allEndpoints = []string{
  15. RouteNameManifest,
  16. RouteNameCatalog,
  17. RouteNameTags,
  18. RouteNameBlob,
  19. RouteNameBlobUpload,
  20. RouteNameBlobUploadChunk,
  21. }
  22. // Router builds a gorilla router with named routes for the various API
  23. // methods. This can be used directly by both server implementations and
  24. // clients.
  25. func Router() *mux.Router {
  26. return RouterWithPrefix("")
  27. }
  28. // RouterWithPrefix builds a gorilla router with a configured prefix
  29. // on all routes.
  30. func RouterWithPrefix(prefix string) *mux.Router {
  31. rootRouter := mux.NewRouter()
  32. router := rootRouter
  33. if prefix != "" {
  34. router = router.PathPrefix(prefix).Subrouter()
  35. }
  36. router.StrictSlash(true)
  37. for _, descriptor := range routeDescriptors {
  38. router.Path(descriptor.Path).Name(descriptor.Name)
  39. }
  40. return rootRouter
  41. }