@@ -52,6 +52,7 @@ const DEFAULT_CAPACITY: usize = 65536;
52
52
#[ derive( Clone , Debug ) ]
53
53
pub struct ServeDir < F = DefaultServeDirFallback > {
54
54
base : PathBuf ,
55
+ prepend_path : String ,
55
56
buf_chunk_size : usize ,
56
57
precompressed_variants : Option < PrecompressedVariants > ,
57
58
// This is used to specialise implementation for
@@ -72,6 +73,7 @@ impl ServeDir<DefaultServeDirFallback> {
72
73
73
74
Self {
74
75
base,
76
+ prepend_path : "" . to_string ( ) ,
75
77
buf_chunk_size : DEFAULT_CAPACITY ,
76
78
precompressed_variants : None ,
77
79
variant : ServeVariant :: Directory {
@@ -88,6 +90,7 @@ impl ServeDir<DefaultServeDirFallback> {
88
90
{
89
91
Self {
90
92
base : path. as_ref ( ) . to_owned ( ) ,
93
+ prepend_path : "" . to_string ( ) ,
91
94
buf_chunk_size : DEFAULT_CAPACITY ,
92
95
precompressed_variants : None ,
93
96
variant : ServeVariant :: SingleFile { mime } ,
@@ -115,6 +118,19 @@ impl<F> ServeDir<F> {
115
118
}
116
119
}
117
120
121
+ /// Sets a path to be prepended when performing a trailing slash redirect.
122
+ ///
123
+ /// This is useful when you want to serve the files at another location than "/", for example
124
+ /// when you are using multiple services and want this instance to handle `/static/<path>`.
125
+ /// In that example, you should pass in "/static" so that a trailing slash redirect does not
126
+ /// redirect to `/<path>/` but instead to `/static/<path>/`
127
+ ///
128
+ /// The default is the empty string.
129
+ pub fn prepend_path ( mut self , path : String ) -> Self {
130
+ self . prepend_path = path;
131
+ self
132
+ }
133
+
118
134
/// Set a specific read buffer chunk size.
119
135
///
120
136
/// The default capacity is 64kb.
@@ -211,6 +227,7 @@ impl<F> ServeDir<F> {
211
227
/// ```
212
228
pub fn fallback < F2 > ( self , new_fallback : F2 ) -> ServeDir < F2 > {
213
229
ServeDir {
230
+ prepend_path : "" . to_string ( ) ,
214
231
base : self . base ,
215
232
buf_chunk_size : self . buf_chunk_size ,
216
233
precompressed_variants : self . precompressed_variants ,
@@ -358,6 +375,8 @@ impl<F> ServeDir<F> {
358
375
}
359
376
} ;
360
377
378
+ let prepend_path = self . prepend_path . clone ( ) ;
379
+
361
380
let buf_chunk_size = self . buf_chunk_size ;
362
381
let range_header = req
363
382
. headers ( )
@@ -375,6 +394,7 @@ impl<F> ServeDir<F> {
375
394
376
395
let open_file_future = Box :: pin ( open_file:: open_file (
377
396
variant,
397
+ prepend_path,
378
398
path_to_file,
379
399
req,
380
400
negotiated_encodings,
0 commit comments