/// Static assets, served from disk under `/static/` (std/file, ADR 0049). import std/file import std/http.{type Response} import std/string /// The file at `.` + `path` (`/static/x.txt` is `./static/x.txt`), with /// the content type of its extension, or 404. A path containing `..` is /// rejected: a request's path must not leave the directory. pub fn static_file :: path: String -> Response / { if string.contains path ".." { http.not_found () } else { match file.read_bytes ("." <> path) { Ok(content) -> http.response 200 (content_type path) content , Error(_) -> http.not_found () } } } /// The content type of a path's extension. fn content_type :: path: String -> String { if string.ends_with path ".css" { "text/css; charset=utf-8" } else if string.ends_with path ".js" { "text/javascript; charset=utf-8" } else if string.ends_with path ".png" { "image/png" } else if string.ends_with path ".jpg" || string.ends_with path ".jpeg" { "image/jpeg" } else if string.ends_with path ".svg" { "image/svg+xml" } else if string.ends_with path ".woff2" { "font/woff2" } else if string.ends_with path ".html" { "text/html; charset=utf-8" } else if string.ends_with path ".txt" { "text/plain; charset=utf-8" } else { "application/octet-stream" } }