kilork.github.io

actix-web-static-files

The actix-web side of static-files.

A browser requesting app.js from an actix-web app, answered from embedded bytes

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

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.

Where to look

source · crate · docs