Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor routing to a pattern-based implementation #110

Closed
wants to merge 7 commits into from

Conversation

thekid
Copy link
Member

@thekid thekid commented Mar 30, 2024

This pull request refactors the web.Routing class to use regular expressions for routing. This reduces complexity (by removing more than 200 lines of code) and increases performance (by not delegating to a variety of method calls) while sacrificing a completely flexible way of routing requests by using matcher functions, while adding the possibility to route patterns.

Route patterns

...are essentially regular expressions, with the small difference that . matches its literal form and that the ^ and $ metacharacters cannot be used due to how the patterns are constructed. If you want to match a single character use \C instead. Here are a couple of examples:

  • /(favicon.ico|robots.txt) matches the files favicon.ico and robots.txt (but not favicon_ico, which a regular expression would match)
  • /(?i)test.html using the internal-optional syntax matches the file test.html in any case
  • /users/[a-z]+ matches /users/test but not /users/0815.
  • HEAD / and HEAD both match HEAD requests to the root path.

Route patterns are matched against /[PATH] or [METHOD] /[PATH] if they do not start with forward slash.

Compacting

With route patterns, the following typical routing definition can be rewritten in a more compact way:

// Each static resource needed to be listed individually
$static= new AssetsFrom($this->environment->path('src/main/webapp'));
return [
  '/static'      => $static,
  '/favicon.ico' => $static,
  '/robots.txt'  => $static,
  /* ... */
];

// Group static resources in one pattern
return [
  '/(static|favicon.ico|robots.txt)' => new AssetsFrom($this->environment->path('src/main/webapp')),
  /* ... */
];

BC breaks

  • Changes Routing::routes() to return [:web.Handler] instead of web.Route[]
  • The class CannotRoute now lives directly in the web package instead of web.routing.
  • Removes Routing::with()
  • Removes Routing::mapping()

See also

https://pkg.go.dev/net/http#ServeMux

@thekid
Copy link
Member Author

thekid commented Apr 6, 2024

An alternative could be to add a web.Routes class with the new functionality and deprecate web.Routing in favor of it.

@thekid
Copy link
Member Author

thekid commented Apr 6, 2024

Closed in favor of #111

@thekid thekid closed this Apr 6, 2024
@thekid thekid deleted the refactor/pattern-based-routing branch April 6, 2024 11:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant