Struct hreq::server::Static[][src]

pub struct Static { /* fields omitted */ }

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

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]

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

impl Debug for Static[src]

impl Handler for Static[src]

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]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T> Instrument for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.