Skip to content

Commit

Permalink
Collapse routing of root and non-root routes into one
Browse files Browse the repository at this point in the history
  • Loading branch information
markuswustenberg committed Oct 9, 2024
1 parent fc82969 commit 5f13637
Showing 1 changed file with 4 additions and 16 deletions.
20 changes: 4 additions & 16 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,12 @@ func NewRouter() *Router {

// Run satisfies [Runner].
func (r *Router) Run(ctx Context) error {
if len(ctx.Args) == 0 {
runner, ok := r.runners[""]
if !ok {
return ErrorRouteNotFound
}

// Apply middlewares in reverse order, so that middlewares are applied in the order they were added.
for i := len(r.middlewares) - 1; i >= 0; i-- {
runner = r.middlewares[i](runner)
}

return runner.Run(ctx)
}

for _, pattern := range r.patterns {
if ctx.Args[0] == pattern {
if (len(ctx.Args) == 0 && pattern == "") || (ctx.Args[0] == pattern) {
runner := r.runners[pattern]
ctx.Args = ctx.Args[1:]
if len(ctx.Args) > 0 {
ctx.Args = ctx.Args[1:]
}

for i := len(r.middlewares) - 1; i >= 0; i-- {
runner = r.middlewares[i](runner)
Expand Down

0 comments on commit 5f13637

Please sign in to comment.