Repository navigation
Description
Serverpod's routing abilities can be greatly improved as it currently cannot do pattern matching on URL paths. We should also move the path matcher down into the Route itself. Doing this would make the routes easier to share between packages and include in modules.
The interface for adding a route currently looks like this:
pod.webServer.addRoute(RouteRoot(), '/');
pod.webServer.addRoute(RouteRoot(), '/index.html');Instead, a route could just be added without the path:
pod.webServer.addRoute(RouteRoot());The Route class would need to add a matchPath property. The allowed HTTP methods of the route should be a Set, and we should include all methods supported by the HTTP protocol. Regular expressions are supported using the <paramName|REGEXP> syntax, where REGEXP is a regular expression (leaving out ^ and $). For example:
class MyRoute extends Route {
MyRoute() : super(
matchPath: '/user/<userName>/<msgId|\d+>',
methods: [RouteMethod.post],
);
Future<Response> handleRequest(Session session, Request request) async {
var userName = pathParameter['userName'];
var msgId = int.parse(pathParameter['msgId']);
...
}
}(The handleCall method of the Route should be renamed to handleRequest.)
We should also introduce a Router class, which handles the routing. The WebServer can be inheriting from the Router class, giving it the ability to add routes. In addition, the Router should add support for adding sub-routers. The sub path of the router may not include any parameters:
var myRouter = Router();
myRouter.addRoute(MyRoute);
pod.webServer.addRouter('/sub/path', myRouter);Routes are matched in the order in which they are added. So, if multiple matches exist, only the first one will be called.
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone 🚀
Activity
SandPod commentedon Jul 22, 2024
The parameter parsing and the REGEXP support is a bit unclear to me. I have two questions:
If I understand your example correctly,
<>in the path indicated that what ever is in that section of the route will be parsed as a parameter. If the<>does not contain a|then anything will match and be stored in the param name. Otherwise, you will get REGEXP validation on the value supplied.If that is correct, what happens when the value does not completely match or fails to match?
How is the REGEXP supported for normal routes that should not be parsed as a parameter? Say, for example, I want to create a catch-all route for different campaigns that might have ended that I want to identify based on the route.
vlidholt commentedon Jul 22, 2024
These are all good questions. 😅 My thinking is to use the (not too well documented) behavior from
shelf_router, then figure out how it works and document it.My assumption is:
matchPath: '/<catchAll|.*>'. This method can also be used to catch other types of custom routes.nielsenko commentedon Aug 4, 2025
With #3625 relic includes and efficient router build on a trie data structure, that supports parameters (
../:id/..), wildcards (../*/..) and tails (../**).Note: Interface is different than suggested here.