Struct hreq::server::Static [−][src]
Serve static files.
- Supports
HEAD
requests. - Directory default “index.html” (on windows “index.htm”).
- Caching using
if-modified-since
andmust-revalidate
. - Maps file extension to
content-type
using mime-guess. - Guesses character encoding of
text/*
mime types using chardetng. - Supports range requests.
Example
A request for http://localhost:3000/static/foo.html
would attempt to read a file
from disk /www/static/foo.html
.
use hreq::prelude::*; use hreq::server::Static; async fn start_server() { let mut server = Server::new(); server.at("/static/*file").all(Static::dir("/www/static")); let (handle, addr) = server.listen(3000).await.unwrap(); handle.keep_alive().await; }
Implementations
impl Static
[src]
pub fn dir(path: impl AsRef<Path>) -> Self
[src]
Creates a handler that serves files from a directory.
- Must be used with a path parameter
/path/*name
. - Cannot serve files from parent paths. I.e.
/path/../foo.txt
. - Path is either absolute, or made absolute by using
current_dir
upon creation.
Example
use hreq::prelude::*; use hreq::server::Static; async fn start_server() { let mut server = Server::new(); server.at("/static/*file").all(Static::dir("/www/static")); let (handle, addr) = server.listen(3000).await.unwrap(); handle.keep_alive().await; }
pub fn file(path: impl AsRef<Path>) -> Self
[src]
Creates a handler that serves the same file for every request.
- Path is either absolute, or made absolute by using
current_dir
upon creation.
Example
use hreq::prelude::*; use hreq::server::Static; async fn start_server() { let mut server = Server::new(); server.at("/*any").all(Static::file("/www/single-page-app.html")); let (handle, addr) = server.listen(3000).await.unwrap(); handle.keep_alive().await; }
pub async fn send_file(
req: &Request<Body>,
path: impl AsRef<Path>
) -> Result<Response<Body>, Error>
[src]
req: &Request<Body>,
path: impl AsRef<Path>
) -> Result<Response<Body>, Error>
Send a file as part of a handler.
Inspired by express js res.sendFile
.
use hreq::prelude::*; use hreq::server::Static; async fn start_server() { let mut server = Server::new(); server.at("/do/something").get(do_something); let (handle, addr) = server.listen(3000).await.unwrap(); handle.keep_alive().await; } async fn do_something( req: http::Request<Body> ) -> Result<http::Response<Body>, hreq::Error> { // do stuff with req. Static::send_file(&req, "/www/my-file.txt").await }
pub fn index_file(self, file: Option<&str>) -> Self
[src]
Change directory index file.
This defaults to “index.html” and on windows “index.htm”.
Set None
to turn off index files (and respond with 404 instead).
Trait Implementations
Auto Trait Implementations
impl RefUnwindSafe for Static
impl Send for Static
impl Sync for Static
impl Unpin for Static
impl UnwindSafe for Static
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T> Instrument for T
[src]
pub fn instrument(self, span: Span) -> Instrumented<Self>
[src]
pub fn in_current_span(self) -> Instrumented<Self>
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,