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
impl FileServer
sourcepub fn from<P: AsRef<Path>>(path: P) -> Self
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))
}
sourcepub fn new<P: AsRef<Path>>(path: P, options: Options) -> Self
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))
}
sourcepub fn rank(self, rank: isize) -> Self
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
impl Clone for FileServer
source§fn clone(&self) -> FileServer
fn clone(&self) -> FileServer
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl Debug for FileServer
impl Debug for FileServer
source§impl From<FileServer> for Vec<Route>
impl From<FileServer> for Vec<Route>
source§fn from(server: FileServer) -> Self
fn from(server: FileServer) -> Self
source§impl Handler for FileServer
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,
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,
Request
with its associated Data
should be
handled by this handler. Read moreAuto Trait Implementations§
impl Freeze for FileServer
impl RefUnwindSafe for FileServer
impl Send for FileServer
impl Sync for FileServer
impl Unpin for FileServer
impl UnwindSafe for FileServer
Blanket Implementations§
source§impl<'a, T> AsTaggedExplicit<'a> for Twhere
T: 'a,
impl<'a, T> AsTaggedExplicit<'a> for Twhere
T: 'a,
source§impl<'a, T> AsTaggedImplicit<'a> for Twhere
T: 'a,
impl<'a, T> AsTaggedImplicit<'a> for Twhere
T: 'a,
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
§impl<T> IntoCollection<T> for T
impl<T> IntoCollection<T> for T
source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§impl<T> Paint for Twhere
T: ?Sized,
impl<T> Paint for Twhere
T: ?Sized,
source§fn fg(&self, value: Color) -> Painted<&T>
fn fg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self
with the foreground set to
value
.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like red()
and
green()
, which have the same functionality but are
pithier.
§Example
Set foreground color to white using fg()
:
use yansi::{Paint, Color};
painted.fg(Color::White);
Set foreground color to white using white()
.
use yansi::Paint;
painted.white();
source§fn bright_black(&self) -> Painted<&T>
fn bright_black(&self) -> Painted<&T>
Returns self
with the
fg()
set to
Color::BrightBlack
.
§Example
println!("{}", value.bright_black());
source§fn bright_red(&self) -> Painted<&T>
fn bright_red(&self) -> Painted<&T>
source§fn bright_green(&self) -> Painted<&T>
fn bright_green(&self) -> Painted<&T>
Returns self
with the
fg()
set to
Color::BrightGreen
.
§Example
println!("{}", value.bright_green());
source§fn bright_yellow(&self) -> Painted<&T>
fn bright_yellow(&self) -> Painted<&T>
Returns self
with the
fg()
set to
Color::BrightYellow
.
§Example
println!("{}", value.bright_yellow());
source§fn bright_blue(&self) -> Painted<&T>
fn bright_blue(&self) -> Painted<&T>
source§fn bright_magenta(&self) -> Painted<&T>
fn bright_magenta(&self) -> Painted<&T>
Returns self
with the
fg()
set to
Color::BrightMagenta
.
§Example
println!("{}", value.bright_magenta());
source§fn bright_cyan(&self) -> Painted<&T>
fn bright_cyan(&self) -> Painted<&T>
source§fn bright_white(&self) -> Painted<&T>
fn bright_white(&self) -> Painted<&T>
Returns self
with the
fg()
set to
Color::BrightWhite
.
§Example
println!("{}", value.bright_white());
source§fn bg(&self, value: Color) -> Painted<&T>
fn bg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self
with the background set to
value
.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like on_red()
and
on_green()
, which have the same functionality but
are pithier.
§Example
Set background color to red using fg()
:
use yansi::{Paint, Color};
painted.bg(Color::Red);
Set background color to red using on_red()
.
use yansi::Paint;
painted.on_red();
source§fn on_primary(&self) -> Painted<&T>
fn on_primary(&self) -> Painted<&T>
source§fn on_magenta(&self) -> Painted<&T>
fn on_magenta(&self) -> Painted<&T>
source§fn on_bright_black(&self) -> Painted<&T>
fn on_bright_black(&self) -> Painted<&T>
Returns self
with the
bg()
set to
Color::BrightBlack
.
§Example
println!("{}", value.on_bright_black());
source§fn on_bright_red(&self) -> Painted<&T>
fn on_bright_red(&self) -> Painted<&T>
source§fn on_bright_green(&self) -> Painted<&T>
fn on_bright_green(&self) -> Painted<&T>
Returns self
with the
bg()
set to
Color::BrightGreen
.
§Example
println!("{}", value.on_bright_green());
source§fn on_bright_yellow(&self) -> Painted<&T>
fn on_bright_yellow(&self) -> Painted<&T>
Returns self
with the
bg()
set to
Color::BrightYellow
.
§Example
println!("{}", value.on_bright_yellow());
source§fn on_bright_blue(&self) -> Painted<&T>
fn on_bright_blue(&self) -> Painted<&T>
Returns self
with the
bg()
set to
Color::BrightBlue
.
§Example
println!("{}", value.on_bright_blue());
source§fn on_bright_magenta(&self) -> Painted<&T>
fn on_bright_magenta(&self) -> Painted<&T>
Returns self
with the
bg()
set to
Color::BrightMagenta
.
§Example
println!("{}", value.on_bright_magenta());
source§fn on_bright_cyan(&self) -> Painted<&T>
fn on_bright_cyan(&self) -> Painted<&T>
Returns self
with the
bg()
set to
Color::BrightCyan
.
§Example
println!("{}", value.on_bright_cyan());
source§fn on_bright_white(&self) -> Painted<&T>
fn on_bright_white(&self) -> Painted<&T>
Returns self
with the
bg()
set to
Color::BrightWhite
.
§Example
println!("{}", value.on_bright_white());
source§fn attr(&self, value: Attribute) -> Painted<&T>
fn attr(&self, value: Attribute) -> Painted<&T>
Enables the styling Attribute
value
.
This method should be used rarely. Instead, prefer to use
attribute-specific builder methods like bold()
and
underline()
, which have the same functionality
but are pithier.
§Example
Make text bold using attr()
:
use yansi::{Paint, Attribute};
painted.attr(Attribute::Bold);
Make text bold using using bold()
.
use yansi::Paint;
painted.bold();
source§fn underline(&self) -> Painted<&T>
fn underline(&self) -> Painted<&T>
Returns self
with the
attr()
set to
Attribute::Underline
.
§Example
println!("{}", value.underline());
source§fn rapid_blink(&self) -> Painted<&T>
fn rapid_blink(&self) -> Painted<&T>
Returns self
with the
attr()
set to
Attribute::RapidBlink
.
§Example
println!("{}", value.rapid_blink());
source§fn quirk(&self, value: Quirk) -> Painted<&T>
fn quirk(&self, value: Quirk) -> Painted<&T>
Enables the yansi
Quirk
value
.
This method should be used rarely. Instead, prefer to use quirk-specific
builder methods like mask()
and
wrap()
, which have the same functionality but are
pithier.
§Example
Enable wrapping using .quirk()
:
use yansi::{Paint, Quirk};
painted.quirk(Quirk::Wrap);
Enable wrapping using wrap()
.
use yansi::Paint;
painted.wrap();
source§fn clear(&self) -> Painted<&T>
👎Deprecated since 1.0.1: renamed to resetting()
due to conflicts with Vec::clear()
.
The clear()
method will be removed in a future release.
fn clear(&self) -> Painted<&T>
resetting()
due to conflicts with Vec::clear()
.
The clear()
method will be removed in a future release.source§fn whenever(&self, value: Condition) -> Painted<&T>
fn whenever(&self, value: Condition) -> Painted<&T>
Conditionally enable styling based on whether the Condition
value
applies. Replaces any previous condition.
See the crate level docs for more details.
§Example
Enable styling painted
only when both stdout
and stderr
are TTYs:
use yansi::{Paint, Condition};
painted.red().on_yellow().whenever(Condition::STDOUTERR_ARE_TTY);