Struct axum_extra::routing::SpaRouter
source · [−]pub struct SpaRouter<B = Body, T = (), F = fn(_: Error) -> Ready<StatusCode>> { /* private fields */ }
Expand description
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));
async fn api_foo() {}
With this setup we get this behavior:
GET /
will serveindex.html
GET /assets/app.js
will servedist/app.js
assuming that file existsGET /assets/doesnt_exist
will respond with404 Not Found
assuming no such file existsGET /some/other/path
will serveindex.html
since there isn’t another route for itGET /api/foo
will serve theapi_foo
handler function
Implementations
sourceimpl<B, T, F> SpaRouter<B, T, F>
impl<B, T, F> SpaRouter<B, T, F>
sourcepub fn index_file<P>(self, path: P) -> Self where
P: AsRef<Path>,
Available on crate feature spa
only.
pub fn index_file<P>(self, path: P) -> Self where
P: AsRef<Path>,
spa
only.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);
sourcepub fn handle_error<T2, F2>(self, f: F2) -> SpaRouter<B, T2, F2>
Available on crate feature spa
only.
pub fn handle_error<T2, F2>(self, f: F2) -> SpaRouter<B, T2, F2>
spa
only.Change the function used to handle unknown IO errors.
SpaRouter
automatically maps missing files and permission denied to
404 Not Found
. The callback given here will be used for other IO errors.
See axum::error_handling::HandleErrorLayer
for more details.
Example
use std::io;
use axum_extra::routing::SpaRouter;
use axum::{Router, http::{Method, Uri}};
let spa = SpaRouter::new("/assets", "dist").handle_error(handle_error);
async fn handle_error(method: Method, uri: Uri, err: io::Error) -> String {
format!("{} {} failed with {}", method, uri, err)
}
let app = Router::new().merge(spa);
Trait Implementations
sourceimpl<B, F, T> From<SpaRouter<B, T, F>> for Router<B> where
F: Clone + Send + 'static,
HandleError<Route<B, Error>, F, T>: Service<Request<B>, Response = Response, Error = Infallible>,
<HandleError<Route<B, Error>, F, T> as Service<Request<B>>>::Future: Send,
B: HttpBody + Send + 'static,
T: 'static,
impl<B, F, T> From<SpaRouter<B, T, F>> for Router<B> where
F: Clone + Send + 'static,
HandleError<Route<B, Error>, F, T>: Service<Request<B>, Response = Response, Error = Infallible>,
<HandleError<Route<B, Error>, F, T> as Service<Request<B>>>::Future: Send,
B: HttpBody + Send + 'static,
T: 'static,
Auto Trait Implementations
impl<B, T, F> RefUnwindSafe for SpaRouter<B, T, F> where
F: RefUnwindSafe,
impl<B, T, F> Send for SpaRouter<B, T, F> where
F: Send,
impl<B, T, F> Sync for SpaRouter<B, T, F> where
F: Sync,
impl<B, T, F> Unpin for SpaRouter<B, T, F> where
F: Unpin,
impl<B, T, F> UnwindSafe for SpaRouter<B, T, F> where
F: UnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
fn vzip(self) -> V
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
sourcefn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more