(provider RoutesProvider)
| 279 | } |
| 280 | |
| 281 | func (h *routerHandler) Build(provider RoutesProvider) error { |
| 282 | h.trees = h.trees[0:0] // reset, inneed when rebuilding. |
| 283 | h.errorTrees = h.errorTrees[0:0] |
| 284 | |
| 285 | // set the default error code handler, will be fired on error codes |
| 286 | // that are not handled by a specific handler (On(Any)ErrorCode). |
| 287 | h.errorDefaultHandlers = append(provider.GetDefaultErrorMiddleware(), defaultErrorHandler) |
| 288 | |
| 289 | rp := errgroup.New("Routes Builder") |
| 290 | registeredRoutes := provider.GetRoutes() |
| 291 | |
| 292 | // before sort. |
| 293 | for _, r := range registeredRoutes { |
| 294 | if r.topLink != nil { |
| 295 | bindMultiParamTypesHandler(r) |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | // sort, subdomains go first. |
| 300 | sort.Slice(registeredRoutes, func(i, j int) bool { |
| 301 | first, second := registeredRoutes[i], registeredRoutes[j] |
| 302 | lsub1 := len(first.Subdomain) |
| 303 | lsub2 := len(second.Subdomain) |
| 304 | |
| 305 | firstSlashLen := strings.Count(first.Path, "/") |
| 306 | secondSlashLen := strings.Count(second.Path, "/") |
| 307 | |
| 308 | if lsub1 == lsub2 && first.Method == second.Method { |
| 309 | if secondSlashLen < firstSlashLen { |
| 310 | // fixes order when wildcard root is registered before other wildcard paths |
| 311 | return true |
| 312 | } |
| 313 | |
| 314 | if secondSlashLen == firstSlashLen { |
| 315 | // fixes order when static path with the same prefix with a wildcard path |
| 316 | // is registered after the wildcard path, although this is managed |
| 317 | // by the low-level node but it couldn't work if we registered a root level wildcard, this fixes it. |
| 318 | if len(first.tmpl.Params) == 0 { |
| 319 | return false |
| 320 | } |
| 321 | if len(second.tmpl.Params) == 0 { |
| 322 | return true |
| 323 | } |
| 324 | |
| 325 | // No don't fix the order by framework's suggestion, |
| 326 | // let it as it is today; {string} and {path} should be registered before {id} {uint} and e.t.c. |
| 327 | // see `bindMultiParamTypesHandler` for the reason. Order of registration matters. |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | // the rest are handled inside the node |
| 332 | return lsub1 > lsub2 |
| 333 | }) |
| 334 | |
| 335 | noLogCount := 0 |
| 336 | |
| 337 | for _, r := range registeredRoutes { |
| 338 | if r.NoLog { |
nothing calls this directly
no test coverage detected