Struct Static

Source
pub struct Static { /* private fields */ }
Expand description

Serve static files.

§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

Source

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;
}
Source

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;
}
Source

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
}
Source

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§

Source§

impl Debug for Static

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Handler for Static

Source§

fn call<'a>( &'a self, req: Request<Body>, ) -> Pin<Box<dyn Future<Output = Reply> + Send + 'a>>

Call the handler.

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.