Remove SpaRouter (#1784)

This commit is contained in:
David Pedersen 2023-02-25 11:05:23 +01:00 committed by GitHub
parent f726f16b6d
commit 27f05ad32e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 26 additions and 231 deletions

View file

@ -7,11 +7,33 @@ and this project adheres to [Semantic Versioning].
# Unreleased
- **breaking:** `SpaRouter::handle_error` has been removed ([#1783])
- **breaking:** `SpaRouter` has been removed. Use `ServeDir` and `ServeFile`
from `tower-http` instead:
```rust
// before
Router::new().merge(SpaRouter::new("/assets", "dist"));
// with ServeDir
Router::new().nest_service("/assets", ServeDir::new("dist"));
// before with `index_file`
Router::new().merge(SpaRouter::new("/assets", "dist").index_file("index.html"));
// with ServeDir + ServeFile
Router::new().nest_service(
"/assets",
ServeDir::new("dist").not_found_service(ServeFile::new("dist/index.html")),
);
```
See the [static-file-server-example] for more examples.
- **breaking:** Change casing of `ProtoBuf` to `Protobuf` ([#1595])
[#1783]: https://github.com/tokio-rs/axum/pull/1783
[#1595]: https://github.com/tokio-rs/axum/pull/1595
[static-file-server-example]: https://github.com/tokio-rs/axum/blob/main/examples/static-file-server/src/main.rs
# 0.5.0 (12. February, 2022)

View file

@ -12,9 +12,6 @@ use tower_service::Service;
mod resource;
#[cfg(feature = "spa")]
mod spa;
#[cfg(feature = "typed-routing")]
mod typed;
@ -28,9 +25,6 @@ pub use axum_macros::TypedPath;
#[cfg(feature = "typed-routing")]
pub use self::typed::{SecondElementIs, TypedPath};
#[cfg(feature = "spa")]
pub use self::spa::SpaRouter;
/// Extension trait that adds additional methods to [`Router`].
pub trait RouterExt<S, B>: sealed::Sealed {
/// Add a typed `GET` route to the router.

View file

@ -1,202 +0,0 @@
use axum::{
body::{Body, HttpBody},
Router,
};
use std::{
any::type_name,
fmt,
marker::PhantomData,
path::{Path, PathBuf},
sync::Arc,
};
use tower_http::services::{ServeDir, ServeFile};
/// Router for single page applications.
///
/// `SpaRouter` gives a routing setup commonly used for single page applications.
///
/// # Example
///
/// ```
/// use axum_extra::routing::SpaRouter;
/// use axum::{Router, routing::get};
///
/// let spa = SpaRouter::new("/assets", "dist");
///
/// let app = Router::new()
/// // `SpaRouter` implements `Into<Router>` so it works with `merge`
/// .merge(spa)
/// // we can still add other routes
/// .route("/api/foo", get(api_foo));
/// # let _: Router = app;
///
/// async fn api_foo() {}
/// ```
///
/// With this setup we get this behavior:
///
/// - `GET /` will serve `index.html`
/// - `GET /assets/app.js` will serve `dist/app.js` assuming that file exists
/// - `GET /assets/doesnt_exist` will respond with `404 Not Found` assuming no
/// such file exists
/// - `GET /some/other/path` will serve `index.html` since there isn't another
/// route for it
/// - `GET /api/foo` will serve the `api_foo` handler function
pub struct SpaRouter<S = (), B = Body> {
paths: Arc<Paths>,
_marker: PhantomData<fn() -> (S, B)>,
}
#[derive(Debug)]
struct Paths {
assets_path: String,
assets_dir: PathBuf,
index_file: PathBuf,
}
impl<S, B> SpaRouter<S, B> {
/// Create a new `SpaRouter`.
///
/// Assets will be served at `GET /{serve_assets_at}` from the directory at `assets_dir`.
///
/// The index file defaults to `assets_dir.join("index.html")`.
pub fn new<P>(serve_assets_at: &str, assets_dir: P) -> Self
where
P: AsRef<Path>,
{
let path = assets_dir.as_ref();
Self {
paths: Arc::new(Paths {
assets_path: serve_assets_at.to_owned(),
assets_dir: path.to_owned(),
index_file: path.join("index.html"),
}),
_marker: PhantomData,
}
}
}
impl<S, B> SpaRouter<S, B> {
/// Set the path to the index file.
///
/// `path` must be relative to `assets_dir` passed to [`SpaRouter::new`].
///
/// # Example
///
/// ```
/// use axum_extra::routing::SpaRouter;
/// use axum::Router;
///
/// let spa = SpaRouter::new("/assets", "dist")
/// .index_file("another_file.html");
///
/// let app = Router::new().merge(spa);
/// # let _: Router = app;
/// ```
pub fn index_file<P>(mut self, path: P) -> Self
where
P: AsRef<Path>,
{
self.paths = Arc::new(Paths {
assets_path: self.paths.assets_path.clone(),
assets_dir: self.paths.assets_dir.clone(),
index_file: self.paths.assets_dir.join(path),
});
self
}
}
impl<S, B> From<SpaRouter<S, B>> for Router<S, B>
where
B: HttpBody + Send + 'static,
S: Clone + Send + Sync + 'static,
{
fn from(spa: SpaRouter<S, B>) -> Router<S, B> {
let assets_service = ServeDir::new(&spa.paths.assets_dir);
Router::new()
.nest_service(&spa.paths.assets_path, assets_service)
.fallback_service(ServeFile::new(&spa.paths.index_file))
}
}
impl<B, T> fmt::Debug for SpaRouter<B, T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let Self { paths, _marker } = self;
f.debug_struct("SpaRouter")
.field("paths", &paths)
.field("request_body_type", &format_args!("{}", type_name::<B>()))
.field(
"extractor_input_type",
&format_args!("{}", type_name::<T>()),
)
.finish()
}
}
impl<B, T> Clone for SpaRouter<B, T> {
fn clone(&self) -> Self {
Self {
paths: self.paths.clone(),
_marker: self._marker,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::test_helpers::*;
use axum::routing::get;
use http::StatusCode;
#[tokio::test]
async fn basic() {
let app = Router::new()
.route("/foo", get(|| async { "GET /foo" }))
.merge(SpaRouter::new("/assets", "test_files"));
let client = TestClient::new(app);
let res = client.get("/").send().await;
assert_eq!(res.status(), StatusCode::OK);
assert_eq!(res.text().await, "<h1>Hello, World!</h1>\n");
let res = client.get("/some/random/path").send().await;
assert_eq!(res.status(), StatusCode::OK);
assert_eq!(res.text().await, "<h1>Hello, World!</h1>\n");
let res = client.get("/assets/script.js").send().await;
assert_eq!(res.status(), StatusCode::OK);
assert_eq!(res.text().await, "console.log('hi')\n");
let res = client.get("/foo").send().await;
assert_eq!(res.status(), StatusCode::OK);
assert_eq!(res.text().await, "GET /foo");
let res = client.get("/assets/doesnt_exist").send().await;
assert_eq!(res.status(), StatusCode::NOT_FOUND);
}
#[tokio::test]
async fn setting_index_file() {
let app =
Router::new().merge(SpaRouter::new("/assets", "test_files").index_file("index_2.html"));
let client = TestClient::new(app);
let res = client.get("/").send().await;
assert_eq!(res.status(), StatusCode::OK);
assert_eq!(res.text().await, "<strong>Hello, World!</strong>\n");
let res = client.get("/some/random/path").send().await;
assert_eq!(res.status(), StatusCode::OK);
assert_eq!(res.text().await, "<strong>Hello, World!</strong>\n");
}
#[allow(dead_code)]
fn works_with_router_with_state() {
let _: Router = Router::new()
.merge(SpaRouter::new("/assets", "test_files"))
.route("/", get(|_: axum::extract::State<String>| async {}))
.with_state(String::new());
}
}

View file

@ -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"));