You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
So my goal is to handle specific routes and leave other routes to be served by public folder. If the file does not exist in public, public/index.html will be served. Here the issue is I added /login router with only POST method.
My expectation is that GET /login route will call fallback since I only added POST /login.
Instead, I get 405 Method Not Allowed. If I delete the line route("/login", post(controllers::user::sign_in)), GET /login renders public/index.html.
The text was updated successfully, but these errors were encountered:
There is some magic that makes the method router work a little bit as if it was part of the path router, but they are really separate things. This really seems like it's working as intended to me. I think this would be the best solution:
let static_files =
get_service(ServeDir::new("public").fallback(ServeFile::new("public/index.html"))).handle_error(|_e: std::io::Error| asyncmove{(
axum::http::StatusCode::INTERNAL_SERVER_ERROR,"You know what happened.",)});let app = Router::new().route("/api/validateSession",get(controllers::api::validate_session),).route("/login",post(controllers::user::sign_in).merge(static_files.clone())).fallback(static_files);
That's intentional. The design we chose for axum means that a route for /login exists it just doesn't support POST, thus 405 is appropriate.
It's a trade off and making that go to the fallback has other downsides (see issues for rocket never returning 405)
If you want to handle 405 in a custom way you have to write a middleware to catch 405s and return a different response. Using axum::middleware::from_fn is the easiest way to do that.
Bug Report
Version
axum v0.5.4
Platform
Arch Linux
5.17.5-arch1-1
Description
Sorry for that complex title. Here is my code,
So my goal is to handle specific routes and leave other routes to be served by
public
folder. If the file does not exist inpublic
,public/index.html
will be served. Here the issue is I added/login
router with onlyPOST
method.My expectation is that
GET /login
route will call fallback since I only addedPOST /login
.Instead, I get
405 Method Not Allowed
. If I delete the lineroute("/login", post(controllers::user::sign_in))
,GET /login
renderspublic/index.html
.The text was updated successfully, but these errors were encountered: