As of my last update in April 2023, in Axum, to define a catch-all route, you can use a route that matches any path using a wildcard pattern. Here's an example to demonstrate how you might set this up:
use axum::{
routing::get,
Router,
http::StatusCode,
response::{IntoResponse, Response},
};
use std::net::SocketAddr;
async fn general_route() -> &'static str {
"This matches specific paths"
}
async fn catch_all_route() -> impl IntoResponse {
(StatusCode::NOT_FOUND, "This is a catch-all route, possibly a 404")
}
#[tokio::main]
async fn main() {
// Define your app routes
let app = Router::new()
.route("/specific_path", get(general_route))
// The catch-all route; it uses a wildcard pattern to match any path
.route("/*catch_all", get(catch_all_route));
// Define the address to run the server on
let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
println!("Listening on http://{}", addr);
// Run the server
axum::Server::bind(&addr)
.serve(app.into_make_service())
.await
.unwrap();
}