Struct rocket_contrib::serve::StaticFiles

source ·
pub struct StaticFiles { /* private fields */ }
Expand description

Custom handler for serving static files.

This handler makes it simple to serve static files from a directory on the local file system. To use it, construct a StaticFiles using either StaticFiles::from() or StaticFiles::new() then simply mount the handler at a desired path. When mounted, the handler will generate route(s) that serve the desired static files.

§Options

The handler’s functionality can be customized by passing an Options to StaticFiles::new(). Additionally, the rank of generate routes, which defaults to 10, can be set via the StaticFiles::rank() builder method.

§Example

To serve files from the /static directory at the /public path, allowing index.html files to be used to respond to requests for a directory (the default), you might write the following:

use rocket_contrib::serve::StaticFiles;

fn main() {
    rocket::ignite()
        .mount("/public", StaticFiles::from("/static"))
        .launch();
}

With this set-up, requests for files at /public/<path..> will be handled by returning the contents of /static/<path..>. Requests for directories at /public/<directory> will be handled by returning the contents of /static/<directory>/index.html.

If your static files are stored relative to your crate and your project is managed by Cargo, you should either use a relative path and ensure that your server is started in the crate’s root directory or use the CARGO_MANIFEST_DIR to create an absolute path relative to your crate root. For example, to serve files in the static subdirectory of your crate at /, you might write:

use rocket_contrib::serve::StaticFiles;

fn main() {
    rocket::ignite()
        .mount("/", StaticFiles::from(concat!(env!("CARGO_MANIFEST_DIR"), "/static")))
        .launch();
}

Implementations§

source§

impl StaticFiles

source

pub fn from<P: AsRef<Path>>(path: P) -> Self

Constructs a new StaticFiles that serves files from the file system path. By default, Options::Index is set, and the generated routes have a rank of 10. To serve static files with other options, use StaticFiles::new(). To choose a different rank for generated routes, use StaticFiles::rank().

§Example

Serve the static files in the /www/public local directory on path /static.

use rocket_contrib::serve::StaticFiles;

fn main() {
    rocket::ignite()
        .mount("/static", StaticFiles::from("/www/public"))
        .launch();
}

Exactly as before, but set the rank for generated routes to 30.

use rocket_contrib::serve::StaticFiles;

fn main() {
    rocket::ignite()
        .mount("/static", StaticFiles::from("/www/public").rank(30))
        .launch();
}
source

pub fn new<P: AsRef<Path>>(path: P, options: Options) -> Self

Constructs a new StaticFiles that serves files from the file system path with options enabled. By default, the handler’s routes have a rank of 10. To choose a different rank, use StaticFiles::rank().

§Example

Serve the static files in the /www/public local directory on path /static without serving index files or dot files. Additionally, serve the same files on /pub with a route rank of -1 while also serving index files and dot files.

use rocket_contrib::serve::{StaticFiles, Options};

fn main() {
    let options = Options::Index | Options::DotFiles;
    rocket::ignite()
        .mount("/static", StaticFiles::from("/www/public"))
        .mount("/pub", StaticFiles::new("/www/public", options).rank(-1))
        .launch();
}
source

pub fn rank(self, rank: isize) -> Self

Sets the rank for generated routes to rank.

§Example
use rocket_contrib::serve::{StaticFiles, Options};

// A `StaticFiles` created with `from()` with routes of rank `3`.
StaticFiles::from("/public").rank(3);

// A `StaticFiles` created with `new()` with routes of rank `-15`.
StaticFiles::new("/public", Options::Index).rank(-15);

Trait Implementations§

source§

impl Clone for StaticFiles

source§

fn clone(&self) -> StaticFiles

Returns a copy of the value. Read more
1.0.0 · source§

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

Performs copy-assignment from source. Read more
source§

impl Handler for StaticFiles

source§

fn handle<'r>(&self, req: &'r Request<'_>, data: Data) -> Outcome<'r>

Called by Rocket when a Request with its associated Data should be handled by this handler. Read more
source§

impl Into<Vec<Route>> for StaticFiles

source§

fn into(self) -> Vec<Route>

Converts this type into the (usually inferred) input type.

Auto Trait Implementations§

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, I> AsResult<T, I> for T
where I: Input,

source§

fn as_result(self) -> Result<T, ParseErr<I>>

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, dst: *mut T)

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

impl<T> Cloneable for T
where T: Handler + Clone,

source§

fn clone_handler(&self) -> Box<dyn Handler>

Clones self.
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.

§

impl<T> IntoCollection<T> for T

§

fn into_collection<A>(self) -> SmallVec<A>
where A: Array<Item = T>,

Converts self into a collection.
§

fn mapped<U, F, A>(self, f: F) -> SmallVec<A>
where F: FnMut(T) -> U, A: Array<Item = U>,

source§

impl<T> IntoSql for T

source§

fn into_sql<T>(self) -> Self::Expression
where Self: Sized + AsExpression<T>,

Convert self to an expression for Diesel’s query builder. Read more
source§

fn as_sql<'a, T>(&'a self) -> <&'a Self as AsExpression<T>>::Expression
where &'a Self: AsExpression<T>,

Convert &self to an expression for Diesel’s query builder. Read more
source§

impl<T> Same for T

source§

type Output = T

Should always be Self
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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.
source§

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

source§

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

source§

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

source§

impl<T> Typeable for T
where T: Any,

source§

fn get_type(&self) -> TypeId

Get the TypeId of this object.
source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

source§

fn vzip(self) -> V