예제 #1
0
파일: routes.go 프로젝트: snowsnail/zerver
func (r Routes) Apply(rt zerver.Router) error {
	var err error
	for i := 0; err == nil && i < len(r); i++ {
		var route = r[i]
		if route.Handler != nil {
			err = rt.Handle(route.Pattern, route.Handler)
		} else {
			err = rt.HandleFunc(route.Pattern,
				route.Method,
				zerver.Intercept(route.HandleFunc,
					route.Interceptors...))
		}
	}

	return err
}
예제 #2
0
파일: routes.go 프로젝트: jacobzheng/zerver
func (r Routes) HandleFunc(method, pattern string, fn zerver.HandleFunc, interceptors ...interface{}) Routes {
	rt, has := r[pattern]
	if has {
		if rt.Handler != nil {
			panic(fmt.Errorf("handler for patten %s already exists", pattern))
		}
	} else {
		rt = Route{
			MapHandler: make(handler.MapHandler),
		}
	}

	rt.MapHandler.Use(method, zerver.Intercept(fn, r.convertFilters(interceptors)...))
	r[pattern] = rt
	return r
}