#Routes with axum

29 messages · Page 1 of 1 (latest)

coral kraken
#

I'm looking at the documentation and I cannot figure out how to get this to run

pub fn router() -> Router {
    Router::new()
    .route("/", get(list_rooms))
    .route("/", post(create_room))
    .route("/:slug", get(room))
    .route("/:id/keys", get(stream_keys))
}

it gives me Invalid route "/:id/keys": insertion failed due to conflict with previously registered route: /:slug

How do I solve it?

coral kraken
#

OK so apparently if I rename :slug to :id it works

#

But I don't understand why

spiral sentinel
#

It is not possible to create segments that only match some types like numbers or regular expression. You must handle that manually in your handlers.
from the docs

#

if that's what you were going for

coral kraken
#

Not really one of my routes takes a string through Path and the other takes i32

spiral sentinel
#

yeah so it is what you were going for

coral kraken
#

Well why does it work fine if they're both named the same, then? I didn't change that logic

spiral sentinel
#

because one has more segments

coral kraken
#

the first handler still takes a string, the second takes an i32

#

but somehow it can't understand that if they're not named the same

#

which makes no sense to me

spiral sentinel
#

probably the axum developers wanted paths to be consistent, and /foo/123 and /foo/what-ever/keys looks like one should be a "subpath" of the other, but they're doing completely different things

#

I would also be confused by such an api

coral kraken
#

Interesting. I mean at the end of the day it doesn't really matter if they're named the same, but I think it's strange

#

In fact, when I first encountered this I thought it was a bug

spiral sentinel
#

what I mean is that you shouldn't design your api like that

coral kraken
#

Then how should I design it? You can get a room by its slug, and then for subroutes you pass the room id

spiral sentinel
#

if /123 gets a room, then /123/keys should be an operation on that room

coral kraken
#

it is

spiral sentinel
#

uhh

coral kraken
#

it gets the keys of that room

coral kraken
spiral sentinel
#

ok, so do /room-id/:slug

#

or /room-id?slug

coral kraken
#

you mean a route called /room-id?

spiral sentinel
#

yes

coral kraken
#

I don't really like that