Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Grouped/named routes feature request #497

Closed
BartWillems opened this issue Dec 7, 2017 · 3 comments
Closed

Grouped/named routes feature request #497

BartWillems opened this issue Dec 7, 2017 · 3 comments
Labels
request Request for new functionality

Comments

@BartWillems
Copy link

BartWillems commented Dec 7, 2017

I have a feature request for grouped/named routes.
This could be used to ask rocket for the routes(their paths, contenttype, request type, ...) with some identifier.
Or maybe it could be useful to set some guard on an entire group?

In my head it could look like this:

rocket::ignite()
    .mount("/api",
                routes![login],
                "api_routes"
               );
Json(rocket::routes("api_routes").unwrap());
// [
//    {
//        "path" : "/api/login",
//        "type" : "POST"
//    }
// ]
@BartWillems BartWillems changed the title Grouped/named routes Grouped/named routes feature request Dec 7, 2017
@SergioBenitez SergioBenitez added the request Request for new functionality label Dec 14, 2017
@karuna
Copy link
Contributor

karuna commented Jan 21, 2018

Also 👍 if it's possible to set the same RequestGuard to group. Assigning same RequestGuard to dozens of function getting old pretty quick.

@ourhut
Copy link

ourhut commented Apr 19, 2019

I'm facing the same need, I'd like to add the same guard to an entire set of routes rather than to every route starting with a common path. I'm curious if anyone has an elegant workaround at this point.

@SergioBenitez
Copy link
Member

If you simply want to ask Rocket for information on a set of routes, you can group them into a variable yourself:

let these_routes = routes![a, b, c, d];

for route in these_routes {
    // get info on routes
}

If you want to ask questions later, you can put a HashMap in managed state:

struct RouteMap(HashMap<&'static str, Vec<Route>>);

fn main() {
    let mut map = HashMap::new();
    let these_routes = routes![a, b, c, d];
    map.insert("these_routes", these_routes.clone());

    rocket.mount("/", these_routes).manage(RouteMap(map)).launch();
}

#[get("/")]
fn map(routes: State<RouteMap>) { .. }

You can, of course, wrap this into a more elegant API if you'll be using it often.

@ourhut If we implement #749, such a thing would be possible elegantly.

Because the initial request is possible outside of Rocket and the follow-up should be possible by solving another issue, I'm closing this out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
request Request for new functionality
Projects
None yet
Development

No branches or pull requests

4 participants