Skip to content

Improved web routing for Relic #2489

Closed
Closed

Description

@vlidholt

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.

Activity

  1. added
    enhancement
    on Jul 18, 2024
  2. SandPod commented on Jul 22, 2024

    @SandPod
    Contributor

    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.

    1. If that is correct, what happens when the value does not completely match or fails to match?

    2. 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.

  3. vlidholt commented on Jul 22, 2024

    @vlidholt
    CollaboratorAuthor

    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:

    1. If it fails to match, the router moves on to the next route.
    2. Not sure exactly what you are getting at here. But it should be easy to create a catch-all route that you add last to your router. Something like matchPath: '/<catchAll|.*>'. This method can also be used to catch other types of custom routes.
  4. added
    area: web server
    on Aug 29, 2024
  5. moved this to Later 🥳 in Serverpod Frameworkson Mar 5, 2025
  6. moved this to Later 🥳 in Serverpod Frameworkson Jun 16, 2025
  7. moved this from Later 🥳 to Now 🔥 in Serverpod Frameworkson Jun 16, 2025
  8. nielsenko commented on Aug 4, 2025

    @nielsenko
    Collaborator

    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.

  9. moved this from In Progress 🔥 to Done 🚀 in Serverpod Frameworkson Aug 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area: web serverenhancement

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions