// ThenFlat provides a binary operation for handling routing using flatchains,it inspects the requests where // if it matches the given criteria passes off to the supplied Chain else passes it down its own chain scope func ThenFlat(methods, pattern string, pass FlatChains, log *log.Logger) FlatChains { var rmethods = GetMethods(methods) var rxc = reggy.CreateClassic(pattern) return NewFlatChain(func(c *Context, next NextHandler) { req := c.Req res := c.Res method := req.Method url := req.URL.Path //ok we have no method restrictions or we have the method,so we can continue else ignore if len(rmethods) == 0 || HasMethod(rmethods, method) { ok, param := rxc.Validate(url) // not good, so ignore if ok { //ok we got a hit, copy over the parameters pass.Handle(res, req, Collector(param)) } else { c.Copy(param) next(c) } } else { next(c) } }, log) }
// BareRule defines a matching rule for a specified pattern func (r *ChainRouter) BareRule(mo, pattern string, fx RHandler) { methods := GetMethods(mo) patt := reggy.CreateClassic(pattern) cr := &ChainRouta{ClassicMatchMux: patt, Handler: BuildMatchesMethod(methods, fx)} r.wg.Lock() defer r.wg.Unlock() r.paths = append(r.paths, cr) }
// Rule defines a matching rule which returns a flatchain func (r *ChainRouter) Rule(mo, pattern string, fx FlatHandler) FlatChains { if fx == nil { fx = IdentityCall } methods := GetMethods(mo) patt := reggy.CreateClassic(pattern) fr := FlatRouteBuild(methods, patt, fx, r.Log) cr := &ChainRouta{ClassicMatchMux: patt, Handler: fr.Handle} r.wg.Lock() defer r.wg.Unlock() r.paths = append(r.paths, cr) return fr }
// Add adds a route into the sets of routes, method can be "" to allow all methods to be handled or a stringed range eg "get head put" to allow this range of methods(get,head,put) only for the handler func (r *Routes) Add(mo, pattern string, h RHandler) { r.ro.Lock() defer r.ro.Unlock() var methods []string if mo != "" { cln := multispaces.ReplaceAllString(mo, " ") methods = multispaces.Split(cln, -1) } if router, ok := r.routes[pattern]; ok { for _, ro := range methods { if _, ok := router.method[ro]; !ok { router.method[ro] = h } } return } var fatt string if r.namespace != "" { fatt = reggy.CleanPath(fmt.Sprintf("/%s/%s", r.namespace, pattern)) } else { fatt = pattern } var mod = make(map[string]RHandler) for _, ro := range methods { mod[ro] = h } r.routes[pattern] = &Route{ ClassicMatchMux: reggy.CreateClassic(fatt), handler: h, method: mod, nomethod: len(mod) == 0, } }
// FlatRoute provides a new routing system based on the middleware stack and if a request matches // then its passed down the chain else ignored func FlatRoute(methods, pattern string, fx FlatHandler, lg *log.Logger) FlatChains { return FlatRouteBuild(GetMethods(methods), reggy.CreateClassic(pattern), fx, lg) }