add adds an abitary route to the router
(method, path string, handler RouteHandler, middlewares ...Middleware)
| 168 | |
| 169 | // add adds an abitary route to the router |
| 170 | func (r *Router) add(method, path string, handler RouteHandler, middlewares ...Middleware) *route { |
| 171 | for _, m := range middlewares { |
| 172 | handler = m(handler) |
| 173 | } |
| 174 | |
| 175 | exact := true |
| 176 | if strings.HasSuffix(path, "*") { |
| 177 | exact = false |
| 178 | path = strings.TrimSuffix(path, "*") |
| 179 | } |
| 180 | |
| 181 | newRoute := &route{ |
| 182 | method: method, |
| 183 | path: r.config.PathPrefix + path, |
| 184 | handler: handler, |
| 185 | exact: exact, |
| 186 | } |
| 187 | |
| 188 | r.routes = append(r.routes, newRoute) |
| 189 | |
| 190 | // Sort routes by exact flag, exact routes go first in the |
| 191 | // same order they were added |
| 192 | slices.SortStableFunc(r.routes, func(a, b *route) int { |
| 193 | switch { |
| 194 | case a.exact == b.exact: |
| 195 | return 0 |
| 196 | case a.exact: |
| 197 | return -1 |
| 198 | default: |
| 199 | return 1 |
| 200 | } |
| 201 | }) |
| 202 | |
| 203 | return newRoute |
| 204 | } |
| 205 | |
| 206 | // getRequestID tries to read request id from headers or from lambda |
| 207 | // context or generates a new one if nothing found. |