Skip to content

Conversation

@mathieucarbou
Copy link
Member

@mathieucarbou mathieucarbou commented Feb 2, 2025

  • Allow to skip global server middleware on any handler
  • Allow to access the catch-all handler to add middlewares on it

Solves: #49

I added an example showing how to use that: https://github.com/ESP32Async/ESPAsyncWebServer/blob/feat/skip-server-middleware/examples/SkipServerMiddleware/SkipServerMiddleware.ino

  // we apply auth middleware to the server globally
  server.addMiddleware(&basicAuth);

  // protected endpoint: requires basic authentication
  // curl -v -u admin:admin  http://192.168.4.1/
  server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) {
    request->send(200, "text/plain", "Hello, world!");
  });

  // we skip all global middleware from the catchall handler
  server.catchAllHandler().skipServerMiddlewares();
  // we apply a specific middleware to the catchall handler only to log requests without a handler defined
  server.catchAllHandler().addMiddleware(&logging);

  // standard 404 handler: will display the request in the console i na curl-like style
  // curl -v -H "Foo: Bar"  http://192.168.4.1/foo
  server.onNotFound([](AsyncWebServerRequest *request) {
    request->send(404, "text/plain", "Not found");
  });
  • you can skip the global middleware for any handler you want, including the catch-all
  • you can add middleware to any handler, including catch-all.

@mathieucarbou mathieucarbou self-assigned this Feb 2, 2025
@mathieucarbou mathieucarbou changed the base branch from feat/continuation to main February 2, 2025 16:29
@mathieucarbou mathieucarbou changed the base branch from main to feat/continuation February 2, 2025 16:30
@mathieucarbou mathieucarbou requested a review from a team February 2, 2025 16:30
@mathieucarbou mathieucarbou changed the base branch from feat/continuation to main February 2, 2025 16:31
@mathieucarbou mathieucarbou force-pushed the feat/skip-server-middleware branch from 51cde4c to d42bb46 Compare February 2, 2025 16:49
@mathieucarbou mathieucarbou force-pushed the feat/skip-server-middleware branch 2 times, most recently from 7eb04c6 to c50eb70 Compare February 3, 2025 13:18
- Allow to skip global server middleware on any handler
- Allow to access the catch-all handler to add middlewares on it
@mathieucarbou mathieucarbou force-pushed the feat/skip-server-middleware branch from c50eb70 to 0b8ad86 Compare February 3, 2025 23:41
@mathieucarbou mathieucarbou merged commit 29de033 into main Feb 4, 2025
21 checks passed
@mathieucarbou mathieucarbou deleted the feat/skip-server-middleware branch February 4, 2025 07:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants