Пример #1
0
// Sets up all of the mux router routes for the items in the given resource
func (rm *ResourceManager) setItemRoutes(res Resource, itemOnly bool) {

	var (
		router    *mux.Router
		itemRoute *mux.Route
	)

	if itemOnly {
		// If this resource has no "list", just use ResourceManager
		// or parent item router
		parent := res.ResourceParent()
		router = rm.Router
		if parent != nil {
			// If this is a child resource, use it's parent item router as a sub router
			router = rm.GetRoute(parent, "item").Subrouter()
		}

		itemRoute = router.PathPrefix(fmt.Sprintf("/%s/", res.ResourceName()))

	} else {
		// Else this has a list, so use the list router
		router = rm.GetRoute(res, "list").Subrouter()

		// In case of a list, use the regex specified for the id
		idFormat := res.ResourceFullName()
		if matcher, ok := res.(ResourceIdMatcher); ok {
			// If there is a custom regex match for the ID on this resource,
			// add that to the pathprefix for the URL
			regex := matcher.ResourceIdMatcher()
			if regex != "" {
				idFormat = strings.Join([]string{idFormat, regex}, ":")
			}
		}

		itemRoute = router.PathPrefix(fmt.Sprintf("/{%s}/", idFormat))
	}

	itemRoute = itemRoute.Name(res.ResourceFullName() + "." + "item")

	rm.RegisterRoute(res, "item", itemRoute)

	rm.setMethodRoutes(res, "Item")
}