Struct rocket::fs::FileServer

source ·
pub struct FileServer { /* 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 FileServer using either FileServer::from() or FileServer::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 FileServer::rank() method.

Options

The handler’s functionality can be customized by passing an Options to FileServer::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::fs::FileServer;

#[launch]
fn rocket() -> _ {
    rocket::build().mount("/public", FileServer::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 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::fs::{FileServer, relative};

#[launch]
fn rocket() -> _ {
    rocket::build().mount("/", FileServer::from(relative!("static")))
}

Implementations§

source§

impl FileServer

source

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

Constructs a new FileServer 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 FileServer::new(). To choose a different rank for generated routes, use FileServer::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::fs::FileServer;

#[launch]
fn rocket() -> _ {
    rocket::build().mount("/static", FileServer::from("/www/public"))
}

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

use rocket::fs::FileServer;

#[launch]
fn rocket() -> _ {
    rocket::build().mount("/static", FileServer::from("/www/public").rank(30))
}
source

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

Constructs a new FileServer 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 FileServer::rank().

Panics

If Options::Missing is not set, panics if path does not exist or is not a directory. Otherwise does not panic.

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::fs::{FileServer, Options};

#[launch]
fn rocket() -> _ {
    let options = Options::Index | Options::DotFiles;
    rocket::build()
        .mount("/static", FileServer::from("/www/public"))
        .mount("/pub", FileServer::new("/www/public", options).rank(-1))
}
source

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

Sets the rank for generated routes to rank.

Example
use rocket::fs::{FileServer, Options};

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

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

Trait Implementations§

source§

impl Clone for FileServer

source§

fn clone(&self) -> FileServer

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 Debug for FileServer

source§

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

Formats the value using the given formatter. Read more
source§

impl From<FileServer> for Vec<Route>

source§

fn from(server: FileServer) -> Self

Converts to this type from the input type.
source§

impl Handler for FileServer

source§

fn handle<'r, 'life0, 'life1, 'async_trait>( &'life0 self, req: &'r Request<'life1>, data: Data<'r> ) -> Pin<Box<dyn Future<Output = Outcome<'r>> + Send + 'async_trait>>where Self: 'async_trait, 'r: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

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

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<'a, T> AsTaggedExplicit<'a> for Twhere T: 'a,

§

fn explicit(self, class: Class, tag: u32) -> TaggedParser<'a, Explicit, Self>

§

impl<'a, T> AsTaggedImplicit<'a> for Twhere T: 'a,

§

fn implicit( self, class: Class, constructed: bool, tag: u32 ) -> TaggedParser<'a, Implicit, Self>

source§

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

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

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

const: unstable · 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> IntoCollection<T> for T

source§

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

Converts self into a collection.
source§

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

source§

impl<T> Same<T> for T

§

type Output = T

Should always be Self
source§

impl<T> ToOwned for Twhere T: Clone,

§

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 Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
source§

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

§

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

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
§

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

§

fn vzip(self) -> V

source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more