@@ -15,7 +15,7 @@ use crate::fs::rewrite::*;
15
15
///
16
16
/// This handler makes is simple to serve static files from a directory on the
17
17
/// local file system. To use it, construct a `FileServer` using
18
- /// [`FileServer::from ()`], then simply `mount` the handler. When mounted, the
18
+ /// [`FileServer::new ()`], then simply `mount` the handler. When mounted, the
19
19
/// handler serves files from the specified directory. If the file is not found,
20
20
/// the handler _forwards_ the request. By default, `FileServer` has a rank of
21
21
/// `10`. Use [`FileServer::new()`] to create a handler with a custom rank.
@@ -26,7 +26,7 @@ use crate::fs::rewrite::*;
26
26
/// the use of [`Rewriter`]s. See [`Rewriter`] for more detailed documentation
27
27
/// on how to take full advantage of `FileServer`'s extensibility.
28
28
///
29
- /// [`FileServer::from()`] and [`FileServer:: new()`] construct a `FileServer`
29
+ /// [`FileServer::new()`] construct a `FileServer`
30
30
/// with common rewrites: they filter out dotfiles, redirect requests to
31
31
/// directories to include a trailing slash, and use `index.html` to respond to
32
32
/// requests for a directory. If you want to customize or replace these default
@@ -43,7 +43,7 @@ use crate::fs::rewrite::*;
43
43
///
44
44
/// #[launch]
45
45
/// fn rocket() -> _ {
46
- /// rocket::build().mount("/public", FileServer::from ("/static"))
46
+ /// rocket::build().mount("/public", FileServer::new ("/static"))
47
47
/// }
48
48
/// ```
49
49
///
@@ -65,7 +65,7 @@ use crate::fs::rewrite::*;
65
65
///
66
66
/// #[launch]
67
67
/// fn rocket() -> _ {
68
- /// rocket::build().mount("/", FileServer::from (relative!("static")))
68
+ /// rocket::build().mount("/", FileServer::new (relative!("static")))
69
69
/// }
70
70
/// ```
71
71
#[ derive( Clone ) ]
@@ -94,6 +94,20 @@ impl FileServer {
94
94
. rewrite ( DirIndex :: unconditional ( "index.html" ) )
95
95
}
96
96
97
+ /// Constructs a new `FileServer` that serves files from the file system
98
+ /// `path` with a default rank.
99
+ ///
100
+ /// Adds a set of default rewrites:
101
+ /// - [`filter_dotfiles`]: Hides all dotfiles.
102
+ /// - [`prefix(path)`](prefix): Applies the root path.
103
+ /// - [`normalize_dirs`]: Normalizes directories to have a trailing slash.
104
+ pub fn directory < P : AsRef < Path > > ( path : P ) -> Self {
105
+ Self :: empty ( )
106
+ . filter ( |f, _| f. is_visible ( ) )
107
+ . rewrite ( Prefix :: checked ( path) )
108
+ . rewrite ( TrailingDirs )
109
+ }
110
+
97
111
/// Constructs a new `FileServer`, with default rank, and no rewrites.
98
112
///
99
113
/// See [`FileServer::empty_ranked()`].
@@ -126,9 +140,8 @@ impl FileServer {
126
140
/// Redirects all requests that have been filtered to the root of the `FileServer`.
127
141
///
128
142
/// ```rust,no_run
129
- /// # use rocket::{Rocket, Build, Request};
130
143
/// # use rocket::fs::{FileServer, Rewrite};
131
- /// # use rocket::{response::Redirect, # uri, Build, Rocket, Request};
144
+ /// # use rocket::{response::Redirect, uri, Build, Rocket, Request};
132
145
/// fn redir_missing<'r>(p: Option<Rewrite<'r>>, _req: &Request<'_>) -> Option<Rewrite<'r>> {
133
146
/// match p {
134
147
/// None => Redirect::temporary(uri!("/")).into(),
@@ -138,7 +151,7 @@ impl FileServer {
138
151
///
139
152
/// # fn launch() -> Rocket<Build> {
140
153
/// rocket::build()
141
- /// .mount("/", FileServer::from ("static").rewrite(redir_missing))
154
+ /// .mount("/", FileServer::new ("static").rewrite(redir_missing))
142
155
/// # }
143
156
/// ```
144
157
///
@@ -161,7 +174,7 @@ impl FileServer {
161
174
///
162
175
/// #[launch]
163
176
/// fn rocket() -> _ {
164
- /// let server = FileServer::from ("static")
177
+ /// let server = FileServer::new ("static")
165
178
/// .filter(|f, _| f.path.file_name() != Some("hidden".as_ref()));
166
179
///
167
180
/// rocket::build()
0 commit comments