-
-
Notifications
You must be signed in to change notification settings - Fork 480
Closed
Description
I noticed a strange behaviour when playing around with middlewares, when I add my middleware to the app as a template argument it works as expected, however, when I add the same middleware as a constructor argument, neither before_handle
and after_handle
are executed.
I added a log to the after_handle
function:
void after_handle(crow::request& req, crow::response& res, context& ctx) {
CROW_LOG_ERROR << "wtf?!";
// ...
}
Compiling and running the following:
crow::App<MyMiddleware> app;
MyMiddleware& middleware = app.get_middleware<MyMiddleware>();
// configure middleware
// define routes
app.bindaddr("127.0.0.1").port(8080).run();
Sending a single request prints "wtf?!" as expected:
However, if I compile and run the following:
MyMiddleware middleware;
// configure middleware
crow::App<> app(middleware);
// define routes
app.bindaddr("127.0.0.1").port(8080).run();
And send a single request, I don't get the "wtf?!" message:
The reference docs Say about the constructor "Construct Crow with a subset of middleware.", what does that mean exactly? Can this be mentioned in the Middleware guide?
Metadata
Metadata
Assignees
Labels
No labels
Type
Projects
Milestone
Relationships
Development
Select code repository
Activity
[-]Middleware `after_handle` not executed when adding middleware through the constructor[/-][+]Middleware doesn't work when added through the constructor[/+]Guiorgy commentedon Aug 5, 2024
Also an FYI, in middleware.h, in the
is_after_handle_arity_3_impl
structvalue
is defined as aconstexpr
, while inis_before_handle_arity_3_impl
as aconst
.Guiorgy commentedon Aug 5, 2024
From looking at the source, I think the middlewares passed to the constructor replace those defined in the template arguments as an alternative initialization to the
app.get_middleware()
approach:If this is correct, it would sure be useful to have a
static_assert
that prevents passing middleware types missing in the App template arguments.I'll try to submit a PR when I get time.
gittiver commentedon Apr 15, 2025
unluckily PR is stuck in draft state.
Guiorgy commentedon Apr 15, 2025
Yeah, life stuff. Feel free to close them. I'll reopen them one day if I manage to get it over the line.