Skip to main content

Assets

Struct Assets 

Source
pub struct Assets { /* private fields */ }
Expand description

A folder served under a mount path.

use web_net::{App, Assets};

let app = App::builder()
    .assets(Assets::new("/static", "./public").cache_control("public, max-age=31536000, immutable"))
    .build()?;

Implementations§

Source§

impl Assets

Source

pub fn new(mount: &str, dir: impl Into<PathBuf>) -> Self

Serves dir under mount (/static). The folder must exist when the application is built.

use web_net::{App, Assets};

let dir = std::env::temp_dir().join("web-net-doc-assets-new");
std::fs::create_dir_all(&dir)?;
std::fs::write(dir.join("site.css"), "body { margin: 0 }")?;

let app = App::builder().assets(Assets::new("/static", &dir)).build()?;
let r = serve_net::testing::respond(&app, b"GET /static/site.css HTTP/1.1\r\nHost: localhost\r\n\r\n");
assert_eq!(r.header("content-type"), Some(&b"text/css; charset=utf-8"[..]));
assert_eq!(r.text(), "body { margin: 0 }");
Source

pub fn cache_control(self, value: &str) -> Self

The Cache-Control value (default no-cache: the browser keeps the file and revalidates it, which a matching ETag answers with 304). For files whose names change with their content: public, max-age=31536000, immutable.

use web_net::{App, Assets};

let dir = std::env::temp_dir().join("web-net-doc-assets-cache");
std::fs::create_dir_all(&dir)?;
std::fs::write(dir.join("app.3f9a.js"), "let x = 1;")?;

let app = App::builder()
    .assets(Assets::new("/static", &dir).cache_control("public, max-age=31536000, immutable"))
    .build()?;
let r = serve_net::testing::respond(&app, b"GET /static/app.3f9a.js HTTP/1.1\r\nHost: localhost\r\n\r\n");
assert_eq!(r.header("cache-control"), Some(&b"public, max-age=31536000, immutable"[..]));
Source

pub fn index(self, name: &str) -> Self

The file served for a folder (index.html). Without one, a folder is a 404: there are never listings.

use web_net::{App, Assets};

let dir = std::env::temp_dir().join("web-net-doc-assets-index");
std::fs::create_dir_all(dir.join("guide"))?;
std::fs::write(dir.join("guide").join("index.html"), "<h1>Guide</h1>")?;

let app = App::builder().assets(Assets::new("/docs", &dir).index("index.html")).build()?;
let r = serve_net::testing::respond(&app, b"GET /docs/guide HTTP/1.1\r\nHost: localhost\r\n\r\n");
assert_eq!(r.text(), "<h1>Guide</h1>");
Source

pub fn precompressed(self, on: bool) -> Self

Whether file.br and file.gz next to a file are served to clients that accept them (default on).

use web_net::{App, Assets};

let dir = std::env::temp_dir().join("web-net-doc-assets-precompressed");
std::fs::create_dir_all(&dir)?;
std::fs::write(dir.join("app.js"), "let x = 1;")?;
std::fs::write(dir.join("app.js.gz"), b"not used")?;

let app = App::builder().assets(Assets::new("/static", &dir).precompressed(false)).build()?;
let r = serve_net::testing::respond(&app, b"GET /static/app.js HTTP/1.1\r\nHost: localhost\r\nAccept-Encoding: gzip\r\n\r\n");
assert_eq!((r.text(), r.header("content-encoding")), ("let x = 1;", None));
Source

pub fn hidden_files(self, on: bool) -> Self

Serve names that start with a dot (default off: .env, .git, .htaccess are 404).

use web_net::{App, Assets};

let dir = std::env::temp_dir().join("web-net-doc-assets-hidden");
std::fs::create_dir_all(&dir)?;
std::fs::write(dir.join(".about.txt"), "hidden")?;

let shown = App::builder().assets(Assets::new("/static", &dir).hidden_files(true)).build()?;
let kept = App::builder().assets(Assets::new("/static", &dir)).build()?;
let req = b"GET /static/.about.txt HTTP/1.1\r\nHost: localhost\r\n\r\n";
assert_eq!(serve_net::testing::respond(&shown, req).status, 200);
assert_eq!(serve_net::testing::respond(&kept, req).status, 404);

Trait Implementations§

Source§

impl Clone for Assets

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Assets

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Forget for Assets

§

impl Freeze for Assets

§

impl Move for Assets

§

impl RefUnwindSafe for Assets

§

impl Send for Assets

§

impl Sync for Assets

§

impl Unpin for Assets

§

impl UnsafeUnpin for Assets

§

impl UnwindSafe for Assets

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.