Пример #1
0
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
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
}