Exemple #1
0
func (r *Router) Url(name string, parameter ...string) string {

	var route *route = nil

	if r.get[name] != nil {
		route = r.get[name]
	} else if r.post[name] != nil {
		route = r.post[name]
	} else if r.put[name] != nil {
		route = r.put[name]
	} else if r.delete[name] != nil {
		route = r.delete[name]
	}

	if route == nil {
		// Not Found
		return "#"
	}

	if len(parameter) != 0 && len(parameter) != len(route.keys) {
		// No Match
		return "#"
	}

	//----------------------------------------
	// Match
	//----------------------------------------
	if len(parameter) == 0 {
		// No paramter
		return route.pattern
	}

	url := route.pattern
	keys := route.keys
	for i := 0; i < len(parameter); i++ {
		url = string551.Replace(url, ":"+keys[i]+":", parameter[i])

	}

	return url
}
Exemple #2
0
func (r *Router) Add(packageName string, method routerMethod, name, pattern string, action ActionFunc) {

	route := &route{
		name:        name,
		pattern:     pattern,
		regex:       nil,
		packageName: packageName,
		action:      action,
		keys:        []string{},
	}

	if method != COMMAND {
		route.keys = r.getKeys(pattern)
		regexPattern := pattern
		for i := 0; i < len(route.keys); i++ {
			regexPattern = string551.Replace(regexPattern, ":"+route.keys[i]+":", ".*")
		}
		route.regex = regexp.MustCompile("^" + regexPattern + "$")
	}

	if method&GET == GET {
		r.addGet(name, route)
	}
	if method&POST == POST {
		r.addPost(name, route)
	}
	if method&PUT == PUT {
		r.addPut(name, route)
	}
	if method&DELETE == DELETE {
		r.addDelete(name, route)
	}
	if method&COMMAND == COMMAND {
		r.addCommand(name, route)
	}
}