//FromRoute returns a route based on a previous route func FromRoute(r *Route, path string) *Route { return RawRoute(path, flux.DoPushSocket(r.Valid, func(v interface{}, s flux.SocketInterface) { req, ok := v.(*Request) if !ok { return } nreq := FromRequest(req, nil) s.Emit(nreq) }), flux.PushSocket(r.DTO), r.DTO, r.fail) }
//InvertRoute returns a route based on a previous route rejection of a request //provides a divert like or not path logic (i.e if that path does not match the parent route) //then this gets validate that rejected route) //the fail action can be set as nil which then uses the previous fail action from //the previous route func InvertRoute(r *Route, path string, fail Failure) *Route { valids := flux.DoPushSocket(r.Invalid, func(v interface{}, s flux.SocketInterface) { req, ok := v.(*Request) if !ok { return } s.Emit(req) }) if fail == nil { fail = r.fail } return RawRoute(path, valids, flux.PushSocket(r.DTO), r.DTO, fail) }
//RawRoute returns a route struct for a specific route path // which allows us to do NewRoute('{id:[/d+/]}') //As far as operation is concerned any when a Route gets a path to validate //against it first splits into into a list '/app/2/status' => ['app',2,'status'] //where each route within this route chain checks each first index and creates a new //slice excluding that first index if the first value in the first index match its own //rule and then pass it to the next route in its range else drops that packet into //its Invalid socket, also all child routes that extend from a base will all recieve //the parents Invalid socket so you can deal with routes that dont match in one place func RawRoute(path string, base *flux.Push, invalid *flux.Push, ts int, fail Failure) *Route { if path == "" { panic("route path can not be an empty string") } m := reggy.GenerateClassicMatcher(path) r := &Route{ base, make(map[string]*Route), m.Original, m, nil, invalid, ts, new(sync.RWMutex), fail, } //add new socket for valid routes and optional can made into payloadable r.Valid = flux.DoPushSocket(r, func(v interface{}, s flux.SocketInterface) { req, ok := v.(*Request) if !ok { return } if r.fail != nil { _, ok = req.Payload.(*PayloadRack) if !ok { var to int if req.Timeout == 0 { to = r.DTO } else { to = req.Timeout } py := req.Payload pl := NewPayloadRack(to, fail) req.Payload = pl pl.Load(py) } } f := req.Paths[0] ok = r.Pattern.Validate(f) if !ok { r.Invalid.Emit(req) return } req.Param = f s.Emit(req) }) return r }