Example #1
0
func autoRouteFunc(this ControllerInterface, httpMethod, path string) {
	comps := toolkit.SplitString(path, "/")
	if len(comps) < 2 {
		this.HandleRequestPathNotFound()
		return
	}
	var methodName string
	for _, v := range comps[1:] {
		subComps := strings.Split(v, "-")
		for _, vv := range subComps {
			methodName += strings.Title(vv)
		}
	}
	if len(methodName) == 0 {
		this.HandleRequestPathNotFound()
		return
	}
	methodName = strings.Title(strings.ToLower(httpMethod)) + methodName
	value := reflect.ValueOf(this)
	method := value.MethodByName(methodName)
	if !method.IsValid() {
		this.HandleRequestPathNotFound()
		return
	}
	method.Call(nil)
}
Example #2
0
func (this *Router) GetController(path string) ControllerInterface {
	var ci ControllerInterface = nil
	//router func should be first
	if this.routerFunc != nil {
		ci = this.routerFunc(path)
	}

	//normal router map is second
	if ci == nil {
		cit, _ := this.routerMap[path]
		if cit == nil {
			//auto router map is the last
			comps := toolkit.SplitString(path, "/")
			if len(comps) > 1 {
				cit, _ = this.autoRouterMap[comps[0]]
			}
		}

		if cit == nil {
			return nil
		} else {
			return reflect.New(cit).Interface().(ControllerInterface)
		}

	} else {
		return ci
	}
}