Dynamically define what filter I want to use
Is your feature request related to a problem? Please describe. I am using drogon in a base docker image that I use for various projects, and I often need to create a controller which sole purpose is to serve specific data, and ideally its routes are filtered (using a drogon filter). I have created a base controller that I can reuse in different projects, but I am forced to specify statically (with a string) the name of the filter I want to use on its routes, and I am not aware of any way to dynamically determine the wanted filter when creating said controller. The controller is not auto-generated because I need to pass arguments to it.
Describe the solution you'd like A great solution would be to be able to specify the filter I want to use (either in string format or in templating) dynamically. I didn't find much ways to pass a filter to a route besides specifying a string containing the name of the filter class when adding a route, perhaps there is another way to do it which I am unaware of? Also, being able to register a filter with a chosen string id would be nice
Describe alternatives you've considered For now, I am either making sure the filter I use is always named the same, or copying the base controller with another filter in the route adding
From a similar standpoint: Is it possible to have 2 instances of filters that are of the same class, and registered in the same app? Since the registering process takes the class name as key to refer to the instanciated filter
A use case I can think of is, for example, building an IP filter which takes an array of ip ranges and filter on those. It would be useful to be able to have 2 (or more) instances of such filter, with each having different ranges to filter on for different routes. Currently, it seems that the way to do this would be to create a class for each filter even though they share logic, so that each can have their ip ranges and can be referred to individually
Is your feature request related to a problem? Please describe. I am using drogon in a base docker image that I use for various projects, and I often need to create a controller which sole purpose is to serve specific data, and ideally its routes are filtered (using a drogon filter). I have created a base controller that I can reuse in different projects, but I am forced to specify statically (with a string) the name of the filter I want to use on its routes, and I am not aware of any way to dynamically determine the wanted filter when creating said controller. The controller is not auto-generated because I need to pass arguments to it.
Describe the solution you'd like A great solution would be to be able to specify the filter I want to use (either in string format or in templating) dynamically. I didn't find much ways to pass a filter to a route besides specifying a string containing the name of the filter class when adding a route, perhaps there is another way to do it which I am unaware of? Also, being able to register a filter with a chosen string id would be nice
Describe alternatives you've considered For now, I am either making sure the filter I use is always named the same, or copying the base controller with another filter in the route adding
@danielbaud Sorry for the late reply. you could use the 'app().registerHandler' method to register your handlers in runtime. call it before calling the app().run() method.
From a similar standpoint: Is it possible to have 2 instances of filters that are of the same class, and registered in the same app? Since the registering process takes the class name as key to refer to the instanciated filter
A use case I can think of is, for example, building an IP filter which takes an array of ip ranges and filter on those. It would be useful to be able to have 2 (or more) instances of such filter, with each having different ranges to filter on for different routes. Currently, it seems that the way to do this would be to create a class for each filter even though they share logic, so that each can have their ip ranges and can be referred to individually
Currently, every filter used in drogon is in singleton mode, so you cannot create multiple filter instances of a specific filter class. A workaround is to define filter classes with the same function but different parameters through macros.
I found another workaround by having a base Filter class and deriving it into a "dummy" class with any wanted name anytime I need "another instance".
Have you ever though of eventually letting the caller define the key for controllers/filters when they are not auto generated, but rather instanciated and registered? Perhaps it would clash with the singleton implementation I guess?
The app().registerHandler method could serve me as well, for routes that do not require a whole controller
Currently, every filter used in drogon is in singleton mode, so you cannot create multiple filter instances of a specific filter class. A workaround is to define filter classes with the same function but different parameters through macros.
Macros are what I also used in my workaround, to define the filter name statically from oustide the controller
Anyway, thank you for your answer, I got something working the way I wanted 😄