Struct tower_http::services::ServeDir
source · [−]pub struct ServeDir<F = DefaultServeDirFallback> { /* private fields */ }
Expand description
Service that serves files from a given directory and all its sub directories.
The Content-Type
will be guessed from the file extension.
An empty response with status 404 Not Found
will be returned if:
- The file doesn’t exist
- Any segment of the path contains
..
- Any segment of the path contains a backslash
- We don’t have necessary permissions to read the file
Example
use tower_http::services::ServeDir;
// This will serve files in the "assets" directory and
// its subdirectories
let service = ServeDir::new("assets");
// Run our service using `hyper`
let addr = std::net::SocketAddr::from(([127, 0, 0, 1], 3000));
hyper::Server::bind(&addr)
.serve(tower::make::Shared::new(service))
.await
.expect("server error");
Implementations
sourceimpl<F> ServeDir<F>
impl<F> ServeDir<F>
sourcepub fn append_index_html_on_directories(self, append: bool) -> Self
Available on crate feature fs
only.
pub fn append_index_html_on_directories(self, append: bool) -> Self
fs
only.If the requested path is a directory append index.html
.
This is useful for static sites.
Defaults to true
.
sourcepub fn with_buf_chunk_size(self, chunk_size: usize) -> Self
Available on crate feature fs
only.
pub fn with_buf_chunk_size(self, chunk_size: usize) -> Self
fs
only.Set a specific read buffer chunk size.
The default capacity is 64kb.
sourcepub fn precompressed_gzip(self) -> Self
Available on crate feature fs
only.
pub fn precompressed_gzip(self) -> Self
fs
only.Informs the service that it should also look for a precompressed gzip version of any file in the directory.
Assuming the dir
directory is being served and dir/foo.txt
is requested,
a client with an Accept-Encoding
header that allows the gzip encoding
will receive the file dir/foo.txt.gz
instead of dir/foo.txt
.
If the precompressed file is not available, or the client doesn’t support it,
the uncompressed version will be served instead.
Both the precompressed version and the uncompressed version are expected
to be present in the directory. Different precompressed variants can be combined.
sourcepub fn precompressed_br(self) -> Self
Available on crate feature fs
only.
pub fn precompressed_br(self) -> Self
fs
only.Informs the service that it should also look for a precompressed brotli version of any file in the directory.
Assuming the dir
directory is being served and dir/foo.txt
is requested,
a client with an Accept-Encoding
header that allows the brotli encoding
will receive the file dir/foo.txt.br
instead of dir/foo.txt
.
If the precompressed file is not available, or the client doesn’t support it,
the uncompressed version will be served instead.
Both the precompressed version and the uncompressed version are expected
to be present in the directory. Different precompressed variants can be combined.
sourcepub fn precompressed_deflate(self) -> Self
Available on crate feature fs
only.
pub fn precompressed_deflate(self) -> Self
fs
only.Informs the service that it should also look for a precompressed deflate version of any file in the directory.
Assuming the dir
directory is being served and dir/foo.txt
is requested,
a client with an Accept-Encoding
header that allows the deflate encoding
will receive the file dir/foo.txt.zz
instead of dir/foo.txt
.
If the precompressed file is not available, or the client doesn’t support it,
the uncompressed version will be served instead.
Both the precompressed version and the uncompressed version are expected
to be present in the directory. Different precompressed variants can be combined.
sourcepub fn fallback<F2>(self, new_fallback: F2) -> ServeDir<F2>
Available on crate feature fs
only.
pub fn fallback<F2>(self, new_fallback: F2) -> ServeDir<F2>
fs
only.Set the fallback service.
This service will be called if there is no file at the path of the request.
The status code returned by the fallback will not be altered. Use
ServeDir::not_found_service
to set a fallback and always respond with 404 Not Found
.
Example
This can be used to respond with a different file:
use tower_http::services::{ServeDir, ServeFile};
let service = ServeDir::new("assets")
// respond with `not_found.html` for missing files
.fallback(ServeFile::new("assets/not_found.html"));
// Run our service using `hyper`
let addr = std::net::SocketAddr::from(([127, 0, 0, 1], 3000));
hyper::Server::bind(&addr)
.serve(tower::make::Shared::new(service))
.await
.expect("server error");
sourcepub fn not_found_service<F2>(self, new_fallback: F2) -> ServeDir<SetStatus<F2>>
Available on crate feature fs
only.
pub fn not_found_service<F2>(self, new_fallback: F2) -> ServeDir<SetStatus<F2>>
fs
only.Set the fallback service and override the fallback’s status code to 404 Not Found
.
This service will be called if there is no file at the path of the request.
Example
This can be used to respond with a different file:
use tower_http::services::{ServeDir, ServeFile};
let service = ServeDir::new("assets")
// respond with `404 Not Found` and the contents of `not_found.html` for missing files
.not_found_service(ServeFile::new("assets/not_found.html"));
// Run our service using `hyper`
let addr = std::net::SocketAddr::from(([127, 0, 0, 1], 3000));
hyper::Server::bind(&addr)
.serve(tower::make::Shared::new(service))
.await
.expect("server error");
Setups like this are often found in single page applications.
sourcepub fn call_fallback_on_method_not_allowed(self, call_fallback: bool) -> Self
Available on crate feature fs
only.
pub fn call_fallback_on_method_not_allowed(self, call_fallback: bool) -> Self
fs
only.Customize whether or not to call the fallback for requests that aren’t GET
or HEAD
.
Defaults to not calling the fallback and instead returning 405 Method Not Allowed
.
Trait Implementations
sourceimpl<ReqBody, F, FResBody> Service<Request<ReqBody>> for ServeDir<F> where
F: Service<Request<ReqBody>, Response = Response<FResBody>> + Clone,
F::Error: Into<Error>,
F::Future: Send + 'static,
FResBody: Body<Data = Bytes> + Send + 'static,
FResBody::Error: Into<Box<dyn Error + Send + Sync>>,
impl<ReqBody, F, FResBody> Service<Request<ReqBody>> for ServeDir<F> where
F: Service<Request<ReqBody>, Response = Response<FResBody>> + Clone,
F::Error: Into<Error>,
F::Future: Send + 'static,
FResBody: Body<Data = Bytes> + Send + 'static,
FResBody::Error: Into<Box<dyn Error + Send + Sync>>,
type Response = Response<ResponseBody>
type Response = Response<ResponseBody>
Responses given by the service.
type Future = ResponseFuture<ReqBody, F>
type Future = ResponseFuture<ReqBody, F>
The future response value.
Auto Trait Implementations
impl<F> RefUnwindSafe for ServeDir<F> where
F: RefUnwindSafe,
impl<F> Send for ServeDir<F> where
F: Send,
impl<F> Sync for ServeDir<F> where
F: Sync,
impl<F> Unpin for ServeDir<F> where
F: Unpin,
impl<F> UnwindSafe for ServeDir<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>
sourceimpl<T> PolicyExt for T where
T: ?Sized,
impl<T> PolicyExt for T where
T: ?Sized,
sourceimpl<T, Request> ServiceExt<Request> for T where
T: Service<Request> + ?Sized,
impl<T, Request> ServiceExt<Request> for T where
T: Service<Request> + ?Sized,
sourcefn ready(&mut self) -> Ready<'_, Self, Request>
fn ready(&mut self) -> Ready<'_, Self, Request>
Yields a mutable reference to the service when it is ready to accept a request.
sourcefn ready_and(&mut self) -> Ready<'_, Self, Request>
fn ready_and(&mut self) -> Ready<'_, Self, Request>
please use the ServiceExt::ready
method instead
Yields a mutable reference to the service when it is ready to accept a request.
sourcefn ready_oneshot(self) -> ReadyOneshot<Self, Request>
fn ready_oneshot(self) -> ReadyOneshot<Self, Request>
Yields the service when it is ready to accept a request.
sourcefn oneshot(self, req: Request) -> Oneshot<Self, Request>
fn oneshot(self, req: Request) -> Oneshot<Self, Request>
Consume this Service
, calling with the providing request once it is ready.
sourcefn call_all<S>(self, reqs: S) -> CallAll<Self, S> where
S: Stream<Item = Request>,
Self::Error: Into<Box<dyn Error + Send + Sync + 'static, Global>>,
fn call_all<S>(self, reqs: S) -> CallAll<Self, S> where
S: Stream<Item = Request>,
Self::Error: Into<Box<dyn Error + Send + Sync + 'static, Global>>,
sourcefn and_then<F>(self, f: F) -> AndThen<Self, F> where
F: Clone,
fn and_then<F>(self, f: F) -> AndThen<Self, F> where
F: Clone,
Executes a new future after this service’s future resolves. This does
not alter the behaviour of the poll_ready
method. Read more
sourcefn map_response<F, Response>(self, f: F) -> MapResponse<Self, F> where
F: FnOnce(Self::Response) -> Response + Clone,
fn map_response<F, Response>(self, f: F) -> MapResponse<Self, F> where
F: FnOnce(Self::Response) -> Response + Clone,
Maps this service’s response value to a different value. This does not
alter the behaviour of the poll_ready
method. Read more
sourcefn map_err<F, Error>(self, f: F) -> MapErr<Self, F> where
F: FnOnce(Self::Error) -> Error + Clone,
fn map_err<F, Error>(self, f: F) -> MapErr<Self, F> where
F: FnOnce(Self::Error) -> Error + Clone,
Maps this service’s error value to a different value. This does not
alter the behaviour of the poll_ready
method. Read more
sourcefn map_result<F, Response, Error>(self, f: F) -> MapResult<Self, F> where
Error: From<Self::Error>,
F: FnOnce(Result<Self::Response, Self::Error>) -> Result<Response, Error> + Clone,
fn map_result<F, Response, Error>(self, f: F) -> MapResult<Self, F> where
Error: From<Self::Error>,
F: FnOnce(Result<Self::Response, Self::Error>) -> Result<Response, Error> + Clone,
Maps this service’s result type (Result<Self::Response, Self::Error>
)
to a different value, regardless of whether the future succeeds or
fails. Read more
sourcefn map_request<F, NewRequest>(self, f: F) -> MapRequest<Self, F> where
F: FnMut(NewRequest) -> Request,
fn map_request<F, NewRequest>(self, f: F) -> MapRequest<Self, F> where
F: FnMut(NewRequest) -> Request,
Composes a function in front of the service. Read more
sourcefn then<F, Response, Error, Fut>(self, f: F) -> Then<Self, F> where
Error: From<Self::Error>,
F: FnOnce(Result<Self::Response, Self::Error>) -> Fut + Clone,
Fut: Future<Output = Result<Response, Error>>,
fn then<F, Response, Error, Fut>(self, f: F) -> Then<Self, F> where
Error: From<Self::Error>,
F: FnOnce(Result<Self::Response, Self::Error>) -> Fut + Clone,
Fut: Future<Output = Result<Response, Error>>,
Composes an asynchronous function after this service. Read more
sourcefn map_future<F, Fut, Response, Error>(self, f: F) -> MapFuture<Self, F> where
F: FnMut(Self::Future) -> Fut,
Error: From<Self::Error>,
Fut: Future<Output = Result<Response, Error>>,
fn map_future<F, Fut, Response, Error>(self, f: F) -> MapFuture<Self, F> where
F: FnMut(Self::Future) -> Fut,
Error: From<Self::Error>,
Fut: Future<Output = Result<Response, Error>>,
Composes a function that transforms futures produced by the service. Read more
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more
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