Note: This site is currently "Under construction". I'm migrating to a new version of my site building software. Lots of things are in a state of disrepair as a result (for example, footnote links aren't working). It's all part of the process of building in public. Most things should still be readable though.

Serve A Local Directory With axum

Code

[package]
name = "axum_serve_directory"
version = "0.1.0"
edition = "2021"

[dependencies]
axum = "0.6.20"
tokio = { version = "1.32.0", features = ["rt-multi-thread", "macros"] }
tower-http = { version = "0.4.4", features = ["fs"] }

Code

use axum::Router;
use std::net::SocketAddr;
use std::path::Path;
use tower_http::services::ServeDir;

#[tokio::main]
async fn main() {
    let addr = SocketAddr::from(([127, 0, 0, 1], 3313));
    let app = Router::new().nest_service("/", ServeDir::new(Path::new("html")));
    axum::Server::bind(&addr)
        .serve(app.into_make_service())
        .await
        .unwrap();
}