예제 #1
0
func NewResource(m ResourceController) *ResourceRouter {
	resource := new(ResourceRouter)

	descriptor := createDescriptor(m, resource)

	resource.varName = descriptor.varName

	restfuncs := descriptor.GetRestMap()
	memberfuncs := descriptor.GetGenericMapList("Member")
	collectionfuncs := descriptor.GetGenericMapList("Collection")

	resource.Member = router.New()
	resource.Member.Routing = memberSignaler{varName: descriptor.varName, indexer: m}
	memberactions := splitter{}
	memberactions.get = firstNonNilMiddleware([]rack.Middleware{restfuncs["show"], memberfuncs.get[""], memberfuncs.all[""]})
	memberactions.post = firstNonNilMiddleware([]rack.Middleware{memberfuncs.post[""], memberfuncs.all[""]})
	memberactions.put = firstNonNilMiddleware([]rack.Middleware{restfuncs["update"], memberfuncs.put[""], memberfuncs.all[""]})
	memberactions.delete = firstNonNilMiddleware([]rack.Middleware{restfuncs["destroy"], memberfuncs.delete[""], memberfuncs.all[""]})
	resource.Member.Action = memberactions
	resource.Member.Name = memberNamer{varName: descriptor.varName}

	if restfuncs["edit"] != nil {
		memberfuncs.get["edit"] = restfuncs["edit"]
	}
	addMapListRoutes(resource.Member, memberfuncs)

	resource.Collection = router.New()
	resource.Collection.Routing = collectionSignaler{name: descriptor.routeName}
	collectionactions := splitter{}
	collectionactions.get = firstNonNilMiddleware([]rack.Middleware{restfuncs["index"], collectionfuncs.get[""], collectionfuncs.all[""]})
	collectionactions.post = firstNonNilMiddleware([]rack.Middleware{restfuncs["create"], collectionfuncs.post[""], collectionfuncs.all[""]})
	collectionactions.put = firstNonNilMiddleware([]rack.Middleware{collectionfuncs.put[""], collectionfuncs.all[""]})
	collectionactions.delete = firstNonNilMiddleware([]rack.Middleware{collectionfuncs.delete[""], collectionfuncs.all[""]})
	resource.Collection.Action = collectionactions
	resource.Collection.Name = router.NameString(descriptor.routeName)

	if restfuncs["new"] != nil {
		collectionfuncs.get["new"] = restfuncs["new"]
	}
	addMapListRoutes(resource.Collection, collectionfuncs)

	resource.Collection.AddRoute(resource.Member)

	return resource
}
예제 #2
0
//use this to create a route namespace (i.e. "admin").
// name is the string used for the namespace.
// intermediate will be ran if the namespace is used (http://example.com/admin/posts).
// final will be ran if the namespace is the destination (http://example.com/admin)
func NewNamespace(name string, intermediate router.Signaler, final rack.Middleware) *router.Router {
	r := router.New()
	r.Routing = router.SignalFunc(func(vars map[string]interface{}) bool {
		section := router.V(vars).CurrentSection()

		if !router.V(vars).IsCaseSensitive() {
			name = strings.ToLower(name)
		}

		if section != name {
			return false
		}

		if intermediate != nil {
			return intermediate.Run(vars)
		}

		return true
	})

	r.Action = final
	return r
}