pub struct Static { /* private fields */ }
Expand description
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§
Source§impl Static
impl Static
Sourcepub fn dir(path: impl AsRef<Path>) -> Self
pub fn dir(path: impl AsRef<Path>) -> Self
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;
}
Sourcepub fn file(path: impl AsRef<Path>) -> Self
pub fn file(path: impl AsRef<Path>) -> Self
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;
}
Sourcepub async fn send_file(
req: &Request<Body>,
path: impl AsRef<Path>,
) -> Result<Response<Body>, Error>
pub async fn send_file( 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
}
Sourcepub fn index_file(self, file: Option<&str>) -> Self
pub fn index_file(self, file: Option<&str>) -> Self
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 Freeze for Static
impl RefUnwindSafe for Static
impl Send for Static
impl Sync for Static
impl Unpin for Static
impl UnwindSafe for Static
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more