drogonframework / drogon

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

Allow for creating composite filters #1976

Open Mis1eader-dev opened 3 months ago

Mis1eader-dev commented 3 months ago

Is your feature request related to a problem? Please describe. Currently we can define singular filters and add them one by one to controllers, however, this gets frustrating when we have dozens of API endpoints that have a set of 3 or 4 filters repeated in each one. For example:

PATH_ADD(
    "/api/something",
    "LoggedIn",
    "Verified",
    "NotSuspicious",
    HttpMethod::Post
)

Having these 3 filters repeated in multiple endpoints is error-prone in the case something is to be changed, for example we may want to add another filter on top of the 3 that does some other checks on the session, now we must go through each file and add that filter for all of them, and we may forget to add it for one of the API endpoints.

Describe the solution you'd like Add Composite Filters, they are filters that have names just like normal filters, and they have a list of strings that contain the names of individual filters. These composite filters can be added onto controllers like normal filters, and the composite filter will add its children into the controller automatically.

Describe alternatives you've considered Macros, and they are quite ugly:

// A header file

#define API_LOGGED_IN_FILTERS \
    "LoggedIn", \
    "Verified", \
    "NotSuspicious"
// API endpoint file

PATH_ADD(
    "/api/something",
    API_LOGGED_IN_FILTERS,
    HttpMethod::Post
)