mirror of
https://github.com/tokio-rs/axum.git
synced 2026-08-22 00:00:17 +02:00
Remove SpaRouter (#1784)
This commit is contained in:
@@ -11,7 +11,6 @@ use axum::{
|
||||
routing::get,
|
||||
Router,
|
||||
};
|
||||
use axum_extra::routing::SpaRouter;
|
||||
use std::net::SocketAddr;
|
||||
use tower::ServiceExt;
|
||||
use tower_http::{
|
||||
@@ -31,7 +30,6 @@ async fn main() {
|
||||
.init();
|
||||
|
||||
tokio::join!(
|
||||
serve(using_spa_router(), 3000),
|
||||
serve(using_serve_dir(), 3001),
|
||||
serve(using_serve_dir_with_assets_fallback(), 3002),
|
||||
serve(using_serve_dir_only_from_root_via_fallback(), 3003),
|
||||
@@ -41,30 +39,13 @@ async fn main() {
|
||||
);
|
||||
}
|
||||
|
||||
fn using_spa_router() -> Router {
|
||||
// `SpaRouter` is the easiest way to serve assets at a nested route like `/assets`
|
||||
//
|
||||
// Requests starting with `/assets` will be served from files in the current directory.
|
||||
// Requests to unknown routes will get `index.html`.
|
||||
Router::new()
|
||||
.route("/foo", get(|| async { "Hi from /foo" }))
|
||||
.merge(SpaRouter::new("/assets", "assets").index_file("index.html"))
|
||||
}
|
||||
|
||||
fn using_serve_dir() -> Router {
|
||||
// `SpaRouter` is just a convenient wrapper around `ServeDir`
|
||||
//
|
||||
// You can use `ServeDir` directly to further customize your setup
|
||||
let serve_dir = ServeDir::new("assets");
|
||||
|
||||
Router::new()
|
||||
.route("/foo", get(|| async { "Hi from /foo" }))
|
||||
.nest_service("/assets", serve_dir.clone())
|
||||
.fallback_service(serve_dir)
|
||||
// serve the file in the "assets" directory under `/assets`
|
||||
Router::new().nest_service("/assets", ServeDir::new("assets"))
|
||||
}
|
||||
|
||||
fn using_serve_dir_with_assets_fallback() -> Router {
|
||||
// for example `ServeDir` allows setting a fallback if an asset is not found
|
||||
// `ServeDir` allows setting a fallback if an asset is not found
|
||||
// so with this `GET /assets/doesnt-exist.jpg` will return `index.html`
|
||||
// rather than a 404
|
||||
let serve_dir = ServeDir::new("assets").not_found_service(ServeFile::new("assets/index.html"));
|
||||
|
||||
Reference in New Issue
Block a user