liveBook · Manning

Static file

As explained in Rust Servers, Services, and Apps

A static asset is a file served to clients without per-request content generation, such as a stylesheet or image.

A static file is an asset served directly to clients without generating per-request content. In an Actix application, files such as HTML, CSS, and images can be mapped to a filesystem directory. section 7.2 section 8.2

A typical Actix setup serves static files by:

  • Adding the actix-files dependency alongside actix-web. section 7.2
  • Creating a static directory containing files such as static-web-page.html, stylesheets, or background images. section 7.2 section 8.2
  • Registering fs::Files::new("/static", "./static") so GET requests beginning with /static resolve to files on disk. section 7.2
  • Referencing an asset from HTML with a URL such as /static/tutor-styles.css or /static/background.jpg. section 8.2 section 7.5

Serving a static page

Place static-web-page.html under ./static, register the /static route, start the server with cargo run --bin static, and open http://localhost:8080/static/static-web-page.html. section 7.2

use actix_files as fs;                                   #1
use actix_web::{error, web, App, Error, HttpResponse, HttpServer, Result};
 
#[actix_web::main]
async fn main() -> std::io::Result<()> {                 #2
    let addr = env::var("SERVER_ADDR").unwrap_or_else(|_|
    "127.0.0.1:8080".to_string());
    println!("Listening on: {}, open browser and visit have a try!",addr);
    HttpServer::new(|| {
        App::new().service(fs::Files::new(
        "/static", "./static").show_files_listing())   #3
    })
    .bind(addr)?                                         #4
    .run()                                               #5
    .await                                               #6
}

Shows the Actix configuration that maps the /static URL prefix to the ./static directory and starts the server. section 7.2

How Rust Servers, Services, and Apps covers it

How deep it goes
Explains directory mapping, URL references, and server startup, but does not descend into static-file caching or deployment internals.
Examples
Uses a static HTML page, tutor registration stylesheet, background image, and tutor-list styling as concrete assets.
What it assumes
Assumes familiarity with Rust projects and Actix handlers while demonstrating the static directory and route configuration.
What you can do after
Operate Actix Web with actix-files to expose a filesystem directory under a URL prefix.
How it connects
Connects static files with Actix Web, templates, CSS, HTML, images, and server-side rendering.

Also explained in

Compare how 6 books explain static file →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Rust Servers, Services, and Apps. The section links point to the parts of the book it draws on.