Struct rocket_contrib::serve::StaticFiles [−][src]
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. If a requested file is not found, the
routes forward the incoming request. The default rank of the generated
routes is 10
. To customize route ranking, use the StaticFiles::rank()
method.
Options
The handler’s functionality can be customized by passing an Options
to
StaticFiles::new()
.
Example
To serve files from the /static
directory on the local file system 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; #[launch] fn rocket() -> rocket::Rocket { rocket::ignite().mount("/public", StaticFiles::from("/static")) }
With this, 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
.
Relative Paths
In the example above, /static
is an absolute path. If your static files
are stored relative to your crate and your project is managed by Rocket, use
the crate_relative!
macro to obtain a path that is relative to your
crate’s root. For example, to serve files in the static
subdirectory of
your crate at /
, you might write:
use rocket_contrib::serve::{StaticFiles, crate_relative}; #[launch] fn rocket() -> rocket::Rocket { rocket::ignite().mount("/", StaticFiles::from(crate_relative!("static"))) }
Implementations
impl StaticFiles
[src]
pub fn from<P: AsRef<Path>>(path: P) -> Self
[src]
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()
.
Panics
Panics if path
does not exist or is not a directory.
Example
Serve the static files in the /www/public
local directory on path
/static
.
use rocket_contrib::serve::StaticFiles; #[launch] fn rocket() -> rocket::Rocket { rocket::ignite().mount("/static", StaticFiles::from("/www/public")) }
Exactly as before, but set the rank for generated routes to 30
.
use rocket_contrib::serve::StaticFiles; #[launch] fn rocket() -> rocket::Rocket { rocket::ignite().mount("/static", StaticFiles::from("/www/public").rank(30)) }
pub fn new<P: AsRef<Path>>(path: P, options: Options) -> Self
[src]
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()
.
Panics
Panics if path
does not exist or is not a directory.
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}; #[launch] fn rocket() -> rocket::Rocket { let options = Options::Index | Options::DotFiles; rocket::ignite() .mount("/static", StaticFiles::from("/www/public")) .mount("/pub", StaticFiles::new("/www/public", options).rank(-1)) }
pub fn rank(self, rank: isize) -> Self
[src]
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
impl Clone for StaticFiles
[src]
fn clone(&self) -> StaticFiles
[src]
pub fn clone_from(&mut self, source: &Self)
1.0.0[src]
impl Handler for StaticFiles
[src]
fn handle<'r, 's: 'r, 'life0, 'async_trait>(
&'s self,
req: &'r Request<'life0>,
data: Data
) -> Pin<Box<dyn Future<Output = Outcome<'r>> + Send + 'async_trait>> where
'r: 'async_trait,
's: 'async_trait,
'life0: 'async_trait,
Self: 'async_trait,
[src]
&'s self,
req: &'r Request<'life0>,
data: Data
) -> Pin<Box<dyn Future<Output = Outcome<'r>> + Send + 'async_trait>> where
'r: 'async_trait,
's: 'async_trait,
'life0: 'async_trait,
Self: 'async_trait,
impl Into<Vec<Route, Global>> 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> Cloneable for T where
T: Handler + Clone,
[src]
T: Handler + Clone,
pub fn clone_handler(&self) -> Box<dyn Handler + 'static, Global>
[src]
impl<T> From<T> for T
[src]
impl<T> Instrument for T
[src]
pub fn instrument(self, span: Span) -> Instrumented<Self>
[src]
pub fn in_current_span(self) -> Instrumented<Self>
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> IntoCollection<T> for T
pub fn into_collection<A>(self) -> SmallVec<A> where
A: Array<Item = T>,
A: Array<Item = T>,
pub fn mapped<U, F, A>(self, f: F) -> SmallVec<A> where
F: FnMut(T) -> U,
A: Array<Item = U>,
F: FnMut(T) -> U,
A: Array<Item = U>,
impl<T> IntoSql for T
pub fn into_sql<T>(self) -> Self::Expression where
Self: AsExpression<T>,
Self: AsExpression<T>,
pub fn as_sql<'a, T>(&'a self) -> <&'a Self as AsExpression<T>>::Expression where
&'a Self: AsExpression<T>,
&'a Self: AsExpression<T>,
impl<T> Same<T> for T
type Output = T
Should always be Self
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
pub fn to_owned(&self) -> T
[src]
pub fn clone_into(&self, target: &mut T)
[src]
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>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
pub fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
V: MultiLane<T>,