Struct ructe::StaticFiles [−][src]
Handler for static files.
Apart from handling templates for dynamic content, ructe also helps with constants for static content.
Most sites that need HTML templates also needs some static resources. Maybe one or several CSS files, some javascript, and / or pictures. A good way to reduce network round-trips is to use a far expires header to tell the browser it can cache those files and don’t need to check if they have changed. But what if the files do change? Then pretty much the only way to make sure the browser gets the updated file is to change the URL to the file as well.
Ructe can create content-dependent file names for static files.
If you have an image.png
, ructe may call it image-SomeHash.png
where SomeHash
is 8 url-safe base64 characters encoding 48 bits
of a md5 sum of the file.
Each static file will be available as a
StaticFile
struct instance in
your templates::statics
module.
Also, the const STATICS
array in the same module will contain a
reference to each of those instances.
Actually serving the file is a job for a web framework like iron, nickel or rocket, but ructe helps by packing the file contents into a constant struct that you can access from rust code.
Overview
This section describes how to set up your project to serve static content using ructe.
To do this, the first step is to add a line in build.rs
telling
ructe to find and transpile your static files:
let mut ructe = Ructe::from_env()?; ructe.statics()?.add_files("static")?;
Then you need to link to the encoded file.
For an image, you probably want to link it from an <img>
tag in
a template. That can be done like this:
@use super::statics::image_png;
@()
<img alt="Something" src="/static/@image_png.name">
So, what has happened here?
First, assuming the static
directory in your
$CARGO_MANIFEST_DIR
contained a file name image.png
, your
templates::statics
module (which is reachable as super::statics
from inside a template) will contain a
pub static image_png: StaticFile
which can be imported and used
in both templates and rust code.
A StaticFile
has a field named name
which is a &'static str
containing the name with the generated hash, image-SomeHash.png
.
The next step is that a browser actually sends a request for
/static/image-SomeHash.png
and your server needs to deliver it.
Here, things depend on your web framework, so we start with some
pseudo code.
Full examples for warp, gotham, nickel, and iron is
available in the ructe repository.
/// A hypothetical web framework calls this each /static/... request, /// with the name component of the URL as the name argument. fn serve_static(name: &str) -> Response { if let Some(data) = StaticFile::get(name) { Response::Ok(data.content) } else { Response::NotFound } }
The StaticFile::get
function returns the &'static StaticFile
for a given file name if the file exists.
This is a reference to the same struct that we used by the name
image_png
in the template.
Besides the name
field (which will be equal to the argument, or
get
would not have returned this StaticFile
), there is a
content: &'static [u8]
field which contains the actual file
data.
Content-types
How to get the content type of static files.
Ructe has support for making the content-type of each static
file availiable using the
mime crate.
Since mime version 0.3.0 was a breaking change of how the
mime::Mime
type was implemented, and both Nickel and Iron
currently require the old version (0.2.x), ructe provides
support for both mime 0.2.x and mime 0.3.x with separate
feature flags.
Mime 0.2.x
To use the mime 0.2.x support, enable the mime02
feature and
add mime 0.2.x as a dependency:
[build-dependencies]
ructe = { version = "^0.3.2", features = ["mime02"] }
[dependencies]
mime = "~0.2"
A Mime
as implemented in mime
version 0.2.x cannot be
created statically, so instead a StaticFile
provides
pub fn mime(&self) -> Mime
.
use templates::statics::image_png; assert_eq!(format!("Type is {}", image_png.mime()), "Type is image/png");
Mime 0.3.x
To use the mime 0.3.x support, enable the mime3
feature and
add mime 0.3.x as a dependency:
[build-dependencies]
ructe = { version = "^0.3.2", features = ["mime03"] }
[dependencies]
mime = "~0.3"
From version 0.3, the mime
crates supports creating const
static Mime
objects, so with this feature, a StaticFile
simply has a pub mime: &'static Mime
field.
use templates::statics::image_png; assert_eq!(format!("Type is {}", image_png.mime), "Type is image/png");
Implementations
impl StaticFiles
[src]
pub fn add_files(&mut self, indir: impl AsRef<Path>) -> Result<()>
[src]
Add all files from a specific directory, indir
, as static files.
pub fn add_files_as(&mut self, indir: impl AsRef<Path>, to: &str) -> Result<()>
[src]
Add all files from a specific directory, indir
, as static files.
The to
string is used as a directory path of the resulting
urls, the file names are taken as is, without adding any hash.
This is usefull for resources used by preexisting javascript
packages, where it might be hard to change the used urls.
Note that some way of changing the url when the content
changes is still needed if you serve the files with far
expire, and using this method makes that your responsibility
rathr than ructes.
Either the file may have hashed names as is, or you may use
the version number of a 3:rd party package as part of the to
parameter.
The to
parameter may be an empty string.
In that case, no extra slash is added.
pub fn add_file(&mut self, path: impl AsRef<Path>) -> Result<()>
[src]
Add one specific file as a static file.
Create a name to use in the url like name-hash.ext
where
name and ext are the name and extension from path
and has is
a few url-friendly bytes from a hash of the file content.
pub fn add_file_as(
&mut self,
path: impl AsRef<Path>,
url_name: &str
) -> Result<()>
[src]
&mut self,
path: impl AsRef<Path>,
url_name: &str
) -> Result<()>
Add one specific file as a static file.
Use url_name
in the url without adding any hash characters.
pub fn add_file_data<P>(&mut self, path: P, data: &[u8]) -> Result<()> where
P: AsRef<Path>,
[src]
P: AsRef<Path>,
Add a resource by its name and content, without reading an actual file.
The path
parameter is used only to create a file name, the actual
content of the static file will be the data
parameter.
A hash will be added to the file name, just as for
file-sourced statics.
Examples
With the folloing code in build.rs
:
let mut statics = ructe.statics()?; statics.add_file_data("black.css", b"body{color:black}\n");
A StaticFile
named black_css
will be defined in the
templates::statics
module of your crate:
assert_eq!(statics::black_css.name, "black-r3rltVhW.css");
pub fn get_names(&self) -> &BTreeMap<String, String>
[src]
Get a mapping of names, from without hash to with.
let mut statics = ructe.statics()?; statics.add_file_data("black.css", b"body{color:black}\n"); statics.add_file_data("blue.css", b"body{color:blue}\n"); assert_eq!( statics.get_names().iter() .map(|(a, b)| format!("{} -> {}", a, b)) .collect::<Vec<_>>(), vec!["black_css -> black-r3rltVhW.css".to_string(), "blue_css -> blue-GZGxfXag.css".to_string()], );
Trait Implementations
impl Drop for StaticFiles
[src]
Auto Trait Implementations
impl RefUnwindSafe for StaticFiles
impl Send for StaticFiles
impl Sync for StaticFiles
impl Unpin for StaticFiles
impl UnwindSafe for StaticFiles
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> Conv for T
impl<T> Conv for T
impl<T> FmtForward for T
pub fn fmt_binary(self) -> FmtBinary<Self> where
Self: Binary,
Self: Binary,
pub fn fmt_display(self) -> FmtDisplay<Self> where
Self: Display,
Self: Display,
pub fn fmt_lower_exp(self) -> FmtLowerExp<Self> where
Self: LowerExp,
Self: LowerExp,
pub fn fmt_lower_hex(self) -> FmtLowerHex<Self> where
Self: LowerHex,
Self: LowerHex,
pub fn fmt_octal(self) -> FmtOctal<Self> where
Self: Octal,
Self: Octal,
pub fn fmt_pointer(self) -> FmtPointer<Self> where
Self: Pointer,
Self: Pointer,
pub fn fmt_upper_exp(self) -> FmtUpperExp<Self> where
Self: UpperExp,
Self: UpperExp,
pub fn fmt_upper_hex(self) -> FmtUpperHex<Self> where
Self: UpperHex,
Self: UpperHex,
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> Pipe for T where
T: ?Sized,
T: ?Sized,
pub fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> R
pub fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> R where
R: 'a,
R: 'a,
pub fn pipe_ref_mut<'a, R>(
&'a mut self,
func: impl FnOnce(&'a mut Self) -> R
) -> R where
R: 'a,
&'a mut self,
func: impl FnOnce(&'a mut Self) -> R
) -> R where
R: 'a,
pub fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R where
Self: Borrow<B>,
R: 'a,
B: 'a + ?Sized,
Self: Borrow<B>,
R: 'a,
B: 'a + ?Sized,
pub fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R
) -> R where
Self: BorrowMut<B>,
R: 'a,
B: 'a + ?Sized,
&'a mut self,
func: impl FnOnce(&'a mut B) -> R
) -> R where
Self: BorrowMut<B>,
R: 'a,
B: 'a + ?Sized,
pub fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R where
Self: AsRef<U>,
R: 'a,
U: 'a + ?Sized,
Self: AsRef<U>,
R: 'a,
U: 'a + ?Sized,
pub fn pipe_as_mut<'a, U, R>(
&'a mut self,
func: impl FnOnce(&'a mut U) -> R
) -> R where
Self: AsMut<U>,
R: 'a,
U: 'a + ?Sized,
&'a mut self,
func: impl FnOnce(&'a mut U) -> R
) -> R where
Self: AsMut<U>,
R: 'a,
U: 'a + ?Sized,
pub fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R where
Self: Deref<Target = T>,
T: 'a + ?Sized,
R: 'a,
Self: Deref<Target = T>,
T: 'a + ?Sized,
R: 'a,
pub fn pipe_deref_mut<'a, T, R>(
&'a mut self,
func: impl FnOnce(&'a mut T) -> R
) -> R where
Self: DerefMut<Target = T> + Deref,
T: 'a + ?Sized,
R: 'a,
&'a mut self,
func: impl FnOnce(&'a mut T) -> R
) -> R where
Self: DerefMut<Target = T> + Deref,
T: 'a + ?Sized,
R: 'a,
impl<T> Pipe for T
impl<T> PipeAsRef for T
pub fn pipe_as_ref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R where
Self: AsRef<T>,
T: 'a,
R: 'a,
Self: AsRef<T>,
T: 'a,
R: 'a,
pub fn pipe_as_mut<'a, T, R>(
&'a mut self,
func: impl FnOnce(&'a mut T) -> R
) -> R where
Self: AsMut<T>,
T: 'a,
R: 'a,
&'a mut self,
func: impl FnOnce(&'a mut T) -> R
) -> R where
Self: AsMut<T>,
T: 'a,
R: 'a,
impl<T> PipeBorrow for T
pub fn pipe_borrow<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R where
Self: Borrow<T>,
T: 'a,
R: 'a,
Self: Borrow<T>,
T: 'a,
R: 'a,
pub fn pipe_borrow_mut<'a, T, R>(
&'a mut self,
func: impl FnOnce(&'a mut T) -> R
) -> R where
Self: BorrowMut<T>,
T: 'a,
R: 'a,
&'a mut self,
func: impl FnOnce(&'a mut T) -> R
) -> R where
Self: BorrowMut<T>,
T: 'a,
R: 'a,
impl<T> PipeDeref for T
pub fn pipe_deref<'a, R>(
&'a self,
func: impl FnOnce(&'a Self::Target) -> R
) -> R where
Self: Deref,
R: 'a,
&'a self,
func: impl FnOnce(&'a Self::Target) -> R
) -> R where
Self: Deref,
R: 'a,
pub fn pipe_deref_mut<'a, R>(
&'a mut self,
func: impl FnOnce(&'a mut Self::Target) -> R
) -> R where
Self: DerefMut,
R: 'a,
&'a mut self,
func: impl FnOnce(&'a mut Self::Target) -> R
) -> R where
Self: DerefMut,
R: 'a,
impl<T> PipeRef for T
pub fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> R where
R: 'a,
R: 'a,
pub fn pipe_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> R where
R: 'a,
R: 'a,
impl<T> Tap for T
pub fn tap(self, func: impl FnOnce(&Self)) -> Self
pub fn tap_mut(self, func: impl FnOnce(&mut Self)) -> Self
pub fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self where
Self: Borrow<B>,
B: ?Sized,
Self: Borrow<B>,
B: ?Sized,
pub fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self where
Self: BorrowMut<B>,
B: ?Sized,
Self: BorrowMut<B>,
B: ?Sized,
pub fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self where
Self: AsRef<R>,
R: ?Sized,
Self: AsRef<R>,
R: ?Sized,
pub fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self where
Self: AsMut<R>,
R: ?Sized,
Self: AsMut<R>,
R: ?Sized,
pub fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self where
Self: Deref<Target = T>,
T: ?Sized,
Self: Deref<Target = T>,
T: ?Sized,
pub fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self where
Self: DerefMut<Target = T> + Deref,
T: ?Sized,
Self: DerefMut<Target = T> + Deref,
T: ?Sized,
pub fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
pub fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
pub fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self where
Self: Borrow<B>,
B: ?Sized,
Self: Borrow<B>,
B: ?Sized,
pub fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self where
Self: BorrowMut<B>,
B: ?Sized,
Self: BorrowMut<B>,
B: ?Sized,
pub fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self where
Self: AsRef<R>,
R: ?Sized,
Self: AsRef<R>,
R: ?Sized,
pub fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self where
Self: AsMut<R>,
R: ?Sized,
Self: AsMut<R>,
R: ?Sized,
pub fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self where
Self: Deref<Target = T>,
T: ?Sized,
Self: Deref<Target = T>,
T: ?Sized,
pub fn tap_deref_mut_dbg<T>(self, func: impl FnOnce(&mut T)) -> Self where
Self: DerefMut<Target = T> + Deref,
T: ?Sized,
Self: DerefMut<Target = T> + Deref,
T: ?Sized,
impl<T> Tap for T
pub fn tap<F, R>(self, func: F) -> Self where
F: FnOnce(&Self) -> R,
F: FnOnce(&Self) -> R,
pub fn tap_dbg<F, R>(self, func: F) -> Self where
F: FnOnce(&Self) -> R,
F: FnOnce(&Self) -> R,
pub fn tap_mut<F, R>(self, func: F) -> Self where
F: FnOnce(&mut Self) -> R,
F: FnOnce(&mut Self) -> R,
pub fn tap_mut_dbg<F, R>(self, func: F) -> Self where
F: FnOnce(&mut Self) -> R,
F: FnOnce(&mut Self) -> R,
impl<T, U> TapAsRef<U> for T where
U: ?Sized,
U: ?Sized,
pub fn tap_ref<F, R>(self, func: F) -> Self where
Self: AsRef<T>,
F: FnOnce(&T) -> R,
Self: AsRef<T>,
F: FnOnce(&T) -> R,
pub fn tap_ref_dbg<F, R>(self, func: F) -> Self where
Self: AsRef<T>,
F: FnOnce(&T) -> R,
Self: AsRef<T>,
F: FnOnce(&T) -> R,
pub fn tap_ref_mut<F, R>(self, func: F) -> Self where
Self: AsMut<T>,
F: FnOnce(&mut T) -> R,
Self: AsMut<T>,
F: FnOnce(&mut T) -> R,
pub fn tap_ref_mut_dbg<F, R>(self, func: F) -> Self where
Self: AsMut<T>,
F: FnOnce(&mut T) -> R,
Self: AsMut<T>,
F: FnOnce(&mut T) -> R,
impl<T, U> TapBorrow<U> for T where
U: ?Sized,
U: ?Sized,
pub fn tap_borrow<F, R>(self, func: F) -> Self where
Self: Borrow<T>,
F: FnOnce(&T) -> R,
Self: Borrow<T>,
F: FnOnce(&T) -> R,
pub fn tap_borrow_dbg<F, R>(self, func: F) -> Self where
Self: Borrow<T>,
F: FnOnce(&T) -> R,
Self: Borrow<T>,
F: FnOnce(&T) -> R,
pub fn tap_borrow_mut<F, R>(self, func: F) -> Self where
Self: BorrowMut<T>,
F: FnOnce(&mut T) -> R,
Self: BorrowMut<T>,
F: FnOnce(&mut T) -> R,
pub fn tap_borrow_mut_dbg<F, R>(self, func: F) -> Self where
Self: BorrowMut<T>,
F: FnOnce(&mut T) -> R,
Self: BorrowMut<T>,
F: FnOnce(&mut T) -> R,
impl<T> TapDeref for T
pub fn tap_deref<F, R>(self, func: F) -> Self where
Self: Deref,
F: FnOnce(&Self::Target) -> R,
Self: Deref,
F: FnOnce(&Self::Target) -> R,
pub fn tap_deref_dbg<F, R>(self, func: F) -> Self where
Self: Deref,
F: FnOnce(&Self::Target) -> R,
Self: Deref,
F: FnOnce(&Self::Target) -> R,
pub fn tap_deref_mut<F, R>(self, func: F) -> Self where
Self: DerefMut,
F: FnOnce(&mut Self::Target) -> R,
Self: DerefMut,
F: FnOnce(&mut Self::Target) -> R,
pub fn tap_deref_mut_dbg<F, R>(self, func: F) -> Self where
Self: DerefMut,
F: FnOnce(&mut Self::Target) -> R,
Self: DerefMut,
F: FnOnce(&mut Self::Target) -> R,
impl<T> TryConv for T
impl<T> TryConv for T
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,