liveBook · Manning

Static site

As explained in Rust Servers, Services, and Apps

A static web page is a web document served as stored content rather than generated dynamically for each request.

A static web page is served as stored content and presents the same page to each visitor. A bank homepage is a typical example; personalized account balances are dynamic, though they may share static elements such as logos and styling. section 7.1

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
}

An Actix server can mount a directory at `/static` and serve files from it. Place an HTML page in that directory, then request it at a matching URL. section 7.2

A template engine turns a static template into dynamic HTML by inserting data supplied by the web application. Static pages can also be served by a custom Rust HTTP server built on the standard library. section 7.1 section 2.4

How Rust Servers, Services, and Apps covers it

How deep it goes
Builds a static-file server with Actix and summarizes a custom HTTP implementation for serving pages and JSON.
Examples
Uses a bank homepage, an HTML file, and a `/static` route that serves files from a directory.
What you can do after
Apply Actix Web and actix-files to serve an HTML file from a static directory.
How it connects
Connects static pages to HTTP servers, TCP, and dynamic pages generated with template engines.

Also explained in

Compare how 2 books explain static site →

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.