drogonframework / drogon

Drogon: A C++14/17/20 based HTTP web application framework running on Linux/macOS/Unix/Windows
MIT License
11.44k stars 1.1k forks source link

Setting global filters as in the form of middlewares #1908

Closed ericel closed 8 months ago

ericel commented 8 months ago

I think it would make sense to have the ability to set global filters in the form of middleware. Drogon App has the registerFilter() method, but truth be told, it is of no use. At least I haven't found it to be useful because filters registered with that method are never called.

Web development requires middleware that sometimes would make sense to apply site-wide. Setting such filters on each route definition doesn't make much sense.

for example:

auto csrfFilter = std::make_shared<CsrfFilter>();
drogon::app().registerFilter(csrfFilter);

But this filter is never called when declared like it is except on individual routes.

an-tao commented 8 months ago

@ericel The registerFilter method is suitable for filters that cannot be created automatically, for example if their constructor has some parameters. The method register the filter to the reflection system in drogon, make it can be used as normal filters. If you need global filters, please use the GlobalFilters plugin.

ericel commented 8 months ago

@an-tao Thanks a lot.