| 137 | } |
| 138 | |
| 139 | func (n *node) InsertRoute(method methodTyp, pattern string, handler http.Handler) *node { |
| 140 | var parent *node |
| 141 | search := pattern |
| 142 | |
| 143 | for { |
| 144 | // Handle key exhaustion |
| 145 | if len(search) == 0 { |
| 146 | // Insert or update the node's leaf handler |
| 147 | n.setEndpoint(method, handler, pattern) |
| 148 | return n |
| 149 | } |
| 150 | |
| 151 | // We're going to be searching for a wild node next, |
| 152 | // in this case, we need to get the tail |
| 153 | var label = search[0] |
| 154 | var segTail byte |
| 155 | var segEndIdx int |
| 156 | var segTyp nodeTyp |
| 157 | var segRexpat string |
| 158 | if label == '{' || label == '*' { |
| 159 | segTyp, _, segRexpat, segTail, _, segEndIdx = patNextSegment(search) |
| 160 | } |
| 161 | |
| 162 | var prefix string |
| 163 | if segTyp == ntRegexp { |
| 164 | prefix = segRexpat |
| 165 | } |
| 166 | |
| 167 | // Look for the edge to attach to |
| 168 | parent = n |
| 169 | n = n.getEdge(segTyp, label, segTail, prefix) |
| 170 | |
| 171 | // No edge, create one |
| 172 | if n == nil { |
| 173 | child := &node{label: label, tail: segTail, prefix: search} |
| 174 | hn := parent.addChild(child, search) |
| 175 | hn.setEndpoint(method, handler, pattern) |
| 176 | |
| 177 | return hn |
| 178 | } |
| 179 | |
| 180 | // Found an edge to match the pattern |
| 181 | |
| 182 | if n.typ > ntStatic { |
| 183 | // We found a param node, trim the param from the search path and continue. |
| 184 | // This param/wild pattern segment would already be on the tree from a previous |
| 185 | // call to addChild when creating a new node. |
| 186 | search = search[segEndIdx:] |
| 187 | continue |
| 188 | } |
| 189 | |
| 190 | // Static nodes fall below here. |
| 191 | // Determine longest prefix of the search key on match. |
| 192 | commonPrefix := longestPrefix(search, n.prefix) |
| 193 | if commonPrefix == len(n.prefix) { |
| 194 | // the common prefix is as long as the current node's prefix we're attempting to insert. |
| 195 | // keep the search going. |
| 196 | search = search[commonPrefix:] |