actix-web-static-files
Once the frontend is inside the binary, something has to answer requests for it. This is that something: one service that serves the bundled resources, with the content type and cache behaviour a browser expects.
What changes for you
- One line in your app.
ResourceFiles::new("/", generate())and the whole site is served. - Answers come from memory. No
open(), no path traversal question, no missing-file case to handle. - Single-page apps work. A path that is not a file falls back to
index.html, so an Angular-style router deep link opens instead of 404ing. - The rest of actix-web is untouched. Your API routes keep their own scope; the static service only takes what is left.
- Custom npm toolchains. The underlying crate drives npm, yarn
or pnpm, so the frontend build is part of
cargo build.
Getting it
[dependencies]
actix-web-static-files = { version = "4.1", features = ["static-files-03"] }
static-files = "0.3"use static_files::resource_dir;
fn main() -> std::io::Result<()> {
resource_dir("./static").build()
}use actix_web::{App, HttpServer};
use actix_web_static_files::ResourceFiles;
include!(concat!(env!("OUT_DIR"), "/generated.rs"));
#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| {
let generated = generate();
App::new().service(ResourceFiles::new("/", generated))
})
.bind("127.0.0.1:8081")?
.run()
.await
}There are examples for a plain folder, a webpack build and an Angular router, and a benchmark for the serving overhead.