axum icon indicating copy to clipboard operation
axum copied to clipboard

Add `FixNestedRedirect` middleware

Open davidpdrsn opened this issue 2 years ago • 5 comments

This adds FixNestedRedirectLayer which uses NestedPath to change redirects from nested services to include the path they're nested at.

Fixes #1731

davidpdrsn avatar Sep 17 '23 10:09 davidpdrsn

Wouldn't it be better to fix this in tower-http? The current implementation in tower-http doesn't allow scenarios to use ServeDir with a prefix (e.g. /files/*). If such a mechanism would exist, a thin wrapper around ServeDir on axum side would fix this automatically.

georgmu avatar Sep 17 '23 14:09 georgmu

Wouldn't it be better to fix this in tower-http?

That would require moving axum's NestedPath to tower-http or adding something similar there. I'm not a big fan of that.

FixNestedRedirectLayer is also useful when not using ServeDir.

davidpdrsn avatar Sep 17 '23 15:09 davidpdrsn

Converting to draft because I just remember that this (and NestedPath) doesn't support path params in the nested path. axum supports .nest("/:foo", ...) which this should probably support.

davidpdrsn avatar Sep 17 '23 15:09 davidpdrsn

Wouldn't it be better to fix this in tower-http?

That would require moving axum's NestedPath to tower-http or adding something similar there. I'm not a big fan of that.

I wouldn't add NestedPath to tower-http, but give ServeDir the option to specify a "mount point" in the URL tree (like "files", so that GET /files/foo.txt would search for foo.txt, not files/foo.txt).

If ServeDir could do this, a thin wrapper around ServeDir in axum (let's call it NestedServeDir) could use the NestedPath mechanism to apply to instanciate ServeDir with the correct settings.

georgmu avatar Sep 18 '23 07:09 georgmu

I'm a bit surprised this is in axum as opposed to axum-extra.

Yeah I considered putting it in axum-extra. That probably is the better path. I'll move it.

I wouldn't add NestedPath to tower-http, but give ServeDir the option to specify a "mount point" in the URL tree (like "files", so that GET /files/foo.txt would search for foo.txt, not files/foo.txt).

I think I'll move forward with the path in this PR and then see what happens as people use it and give feedback.

davidpdrsn avatar Sep 18 '23 08:09 davidpdrsn