@@ -23,7 +23,7 @@ impl FileServer {
23
23
}
24
24
}
25
25
26
- pub fn add_dir_mount ( mut self , route : & str , dir_path : & str ) -> Result < Self > {
26
+ pub fn map_dir ( mut self , route : & str , dir_path : & str ) -> Result < Self > {
27
27
let mount_point = MountPoint {
28
28
route : route. to_owned ( ) ,
29
29
fs_path : PathBuf :: from ( dir_path) ,
@@ -38,8 +38,10 @@ impl FileServer {
38
38
}
39
39
40
40
pub fn map_file ( mut self , route : & str , file_path : & str ) -> Result < Self > {
41
+ let route = route. strip_suffix ( '/' ) . unwrap_or ( route) . to_owned ( ) ;
42
+
41
43
let mount_point = MountPoint {
42
- route : route . to_owned ( ) ,
44
+ route,
43
45
fs_path : PathBuf :: from ( file_path) ,
44
46
is_directory : false ,
45
47
} ;
@@ -136,24 +138,24 @@ impl Router {
136
138
let route = Route :: from_str ( & route_def) ?;
137
139
trace ! ( "trying to match route: {route_def}" ) ;
138
140
139
- if let Some ( file_server) = & self . file_server {
140
- match file_server. handle_static_file_access ( & route. path ) {
141
- Ok ( file_path) => {
142
- let mime_type = mime_guess:: from_path ( & file_path) . first_or_octet_stream ( ) ;
143
- let content = fs:: read ( file_path) ?;
144
-
145
- return HttpResponseBuilder :: new ( )
146
- . set_raw_body ( content)
147
- . set_content_type ( mime_type. as_ref ( ) )
148
- . build ( ) ;
149
- }
150
- Err ( e) => warn ! ( "failed to match file: {e}" ) ,
151
- }
152
- }
153
-
154
141
let response = if let Some ( route_callback) = self . routes . get ( & route) {
155
142
route_callback ( request)
156
143
} else {
144
+ if let Some ( file_server) = & self . file_server {
145
+ match file_server. handle_static_file_access ( & route. path ) {
146
+ Ok ( file_path) => {
147
+ let mime_type = mime_guess:: from_path ( & file_path) . first_or_octet_stream ( ) ;
148
+ let content = fs:: read ( file_path) ?;
149
+
150
+ return HttpResponseBuilder :: new ( )
151
+ . set_raw_body ( content)
152
+ . set_content_type ( mime_type. as_ref ( ) )
153
+ . build ( ) ;
154
+ }
155
+ Err ( e) => warn ! ( "failed to match file: {e}" ) ,
156
+ }
157
+ }
158
+
157
159
let catch_all_route = Route :: from_str ( "GET /*" ) ?;
158
160
if let Some ( catch_all_callback) = self . routes . get ( & catch_all_route) {
159
161
return catch_all_callback ( request) ;
@@ -249,16 +251,11 @@ impl FromStr for Route {
249
251
type Err = anyhow:: Error ;
250
252
251
253
fn from_str ( s : & str ) -> std:: result:: Result < Self , Self :: Err > {
252
- let ( verb, path) = s. split_once ( " " ) . context ( "route should have: VERB PATH" ) ?;
253
- let verb = HttpMethod :: from_str ( verb) ?;
254
-
255
- let path = if path. ends_with ( '/' ) {
256
- path. to_owned ( )
257
- } else {
258
- format ! ( "{}/" , path)
259
- } ;
254
+ let ( method, path) = s. split_once ( " " ) . context ( "route should have: VERB PATH" ) ?;
255
+ let method = HttpMethod :: from_str ( method) ?;
256
+ let path = path. strip_suffix ( '/' ) . unwrap_or ( path) . to_owned ( ) ;
260
257
261
- Ok ( Route { method : verb , path } )
258
+ Ok ( Route { method, path } )
262
259
}
263
260
}
264
261
0 commit comments