perwendel / spark

A simple expressive web framework for java. Spark has a kotlin DSL https://github.com/perwendel/spark-kotlin
Apache License 2.0
9.63k stars 1.56k forks source link

Get all mapped routes #1006

Open buckelieg opened 6 years ago

buckelieg commented 6 years ago

Provide method to retrieve all mapped routes (in all route groups).

ErnestoSalazar commented 6 years ago

Something like a method that list all mapped routes like this? -============================= /hello -> before /hello -> post /hello -> after -=============================

ooh, did you mean to access the routes objects?

danieljamesscott commented 5 years ago

I would also like this. Exposing Service.pathDeque would be great, even better if we could get the HTTP method too. Would you accept a PR which did this?

buckelieg commented 5 years ago

The idea was not to acess routes as objects (but this is interesting idea). Just wanted to have a list of all routes mapped. The use case of mine is that I want to create a component that restrincts access to particular route (URL) with dynamic configuratrion. The thing is if I have added the routes they are added to the list right away.

danieljamesscott commented 5 years ago

OK, so it sounds like that feature might be better with a 'filter' which is applied as the routes are configured?

buckelieg commented 5 years ago

Whenever I want to configure restrictions through UI - then yes. I am thinking about ONE security filter which uses configuration from DB which is in turn is configured from UI outside. And if administrator changes something - the results are applied immidiately.

buckelieg commented 5 years ago

Any chances to get this implemented?

johnnybigoode-zz commented 5 years ago

Heya, this use case seems necessary for me and I would like to try adding this feat. If any senior devs could give me some idea of the best practice for this, it would be greatly appreciated.

johnnybigoode-zz commented 5 years ago

@buckelieg

https://github.com/perwendel/spark/pull/1097

johnnybigoode-zz commented 5 years ago

So we would like to retrieve a liste of all routes avaliable within an ignited service.

So it makes sense to have a 'findAllRoutes' method in the Service class and another one in the Spark class, since the service is not clearly exposed from the Spark class.

The Service class has a Routes field that seems to hold all Routes available in a service. And when we stop the server, it clears the Routes list, which makes sense.

Whithin the Routes class we have two interesting methods 'find' and 'findMultiple' and both of them return a simple data class that stores 4 values:

private Object target;
private String matchUri;
private String requestURI;
private String acceptType;

More interestingly, there are two other private methods that find things, 'findTargetsForRequestedRoute' and 'findTargetWithGivenAcceptType', both of them return 'RouteEntry' objects.

The main difference between RouteEntry and RouteMatch is that the first has the httpMethod information that we want, while the second has a 'matchUri' field which is resultant of the 'find' methods mentioned.

Since what we would like is just information, there's no reason to use RouteEntry as a return object, some of its methods has performance observations, which is also good reason not to touch it.

So to get a decent return, it's best to have the httpMethod information within RouteEntry. Or when finding all routes, return a dictionary with each httpMethod for key, and a List of RouteEntries for a value.