Crate tower_serve_static

Crate tower_serve_static 

Source
Expand description

Tower file services embedding assets into the binary.

§Serve Static File

use tower_serve_static::{ServeFile, include_file};

// File is located relative to `CARGO_MANIFEST_DIR` (the directory containing the manifest of your package).
// This will embed and serve the `README.md` file.
let service = ServeFile::new(include_file!("/README.md"));

// Run our service using `axum`
let app = axum::Router::new().nest_service("/", service);

// run our app with axum, listening locally on port 3000
let listener = tokio::net::TcpListener::bind("127.0.0.1:3000").await?;
axum::serve(listener, app).await?;

§Serve Static Directory

use tower_serve_static::{ServeDir};
use include_dir::{Dir, include_dir};

// Use `$CARGO_MANIFEST_DIR` to make path relative to your package.
// This will embed and serve files in the `src` directory and its subdirectories.
static ASSETS_DIR: Dir<'static> = include_dir!("$CARGO_MANIFEST_DIR/src");
let service = ServeDir::new(&ASSETS_DIR);

// Run our service using `axum`
let app = axum::Router::new().nest_service("/", service);

// run our app with axum, listening locally on port 3000
let listener = tokio::net::TcpListener::bind("127.0.0.1:3000").await?;
axum::serve(listener, app).await?;

§Features

This library exposes the following features that can be enabled:

  • metadata - enables ServeDir to include the Last-Modified header in the response headers. Additionally, it enables responding with a suitable reply for If-Modified-Since conditional requests.

Macros§

include_file
Create a new File.
include_file_with_mime
Create a new File with a specific mime type.

Structs§

AsyncReadBody
Adapter that turns an impl AsyncRead to an impl Body.
File
A file.
ServeDir
Service that serves files from a given directory and all its sub directories.
ServeDirResponseBody
Response body for ServeDir.
ServeDirResponseFuture
Response future of ServeDir.
ServeFile
Service that serves a file.
ServeFileResponseBody
Response body for ServeFile.
ServeFileResponseFuture
Response future of ServeFile.