Search⌘ K
AI Features

Other Data Servers

Explore how to build Rust servers that serve files or directories using warp and create dynamic HTML responses with handlebars templates. This lesson guides you through setting up file servers and rendering templates efficiently in Rust.

We'll cover the following...

File or directory server

We occasionally need a server for special cases, not just to serve web pages. For example, we might need to show other kinds of files, or let the user download them. For this use case, warp comes to our rescue again with a simple solution.

Rust 1.40.0
use warp::Filter;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
server_setup!(3); // Needed for this playground to work
let data = warp::get()
.and(warp::path::end())
.and(warp::fs::file("/usercode/data.json"));
println!("Server started on: http://127.0.0.1:3030");
warp::serve(data).run(([127, 0, 0, 1], 3030)).await;
Ok(())
}

In the code above, we pass the file /usercode/data.json to the response with warp::fs::file() in line 9.

The warp functionality can also serve a whole directory by mapping each ...