Skip to content

Add middleware support to Relic #2499

Closed

Description

@vlidholt

Add support for middleware to Serverpod's webserver, Relic, which can be used for custom logging, authentication, data validation, error handling, or other logic. The middleware should be able to modify a request and take over a response. We may potentially want to build a bridge for shelf middleware in a separate package.

Serverpod's middleware can be an abstract class Middleware. It's primary method is handleRequest, which is analog to the handleRequest method in a Route. The difference is that the method in the middleware can return null, in which case control over the response is passed on to the next chained middleware or to the Route.

If the middleware needs to override the Request, it can do so by calling the modifyRequest method.

abstract class Middleware {
  Future<Response?> handleRequest(Session session, Request request);

  void modifyRequest(Request request) {
 ...
 }
}

The middlewares can be injected using a Router or WebServer (see #2489). Either directly to the Router, affecting all routes:

pod.webServer.addMiddleware(MyCustomLogger());

Or, when adding the routes:

pod.webServer.addRoute(
  MyRoute(),
  middleware: [MyMiddleware()],
);

Activity

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

    @SandPod
    Contributor

    Why do we need a separate method for modifyRequest? Is the request passed to the method immutable? If not, what would happen if it is modified in the middleware directly?

  3. vlidholt commented on Jul 22, 2024

    @vlidholt
    CollaboratorAuthor

    Yes, the thinking is that the Request is immutable, but has a change method that returns a modified Request. This is how shelf works.

  4. added
    area: web server
    on Aug 29, 2024
  5. moved this to Later 🥳 in Serverpod Frameworkson Mar 5, 2025
  6. baudee commented on Apr 19, 2025

    @baudee

    This feature will be only for the web server ? Could be nice to have it as well for the Endpoints

  7. vanlooverenkoen commented on Apr 22, 2025

    @vanlooverenkoen
    Contributor

    I agree with @baudee having it in the endpoints would simplify my endpoints code by A LOT!

  8. dkbast commented on May 2, 2025

    @dkbast
    Contributor

    This would be super good for setting the CORS headers :)

  9. moved this to Later 🥳 in Serverpod Frameworkson Jun 16, 2025
  10. moved this from Later 🥳 to Now 🔥 in Serverpod Frameworkson Jun 16, 2025
  11. moved this from In Progress 🔥 to Next Up 🎯 in Serverpod Frameworkson Aug 11, 2025
  12. self-assigned this
    on Oct 1, 2025
  13. moved this from Next Up 🎯 to Done 🚀 in Serverpod Frameworkson Oct 7, 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