15
15
// with this program; if not, write to the Free Software Foundation, Inc.,
16
16
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
17
18
- use std:: convert:: AsRef ;
19
18
use std:: fs:: write;
20
- use std:: io:: ErrorKind ;
21
19
use std:: net:: TcpListener ;
22
- use std:: path:: Path ;
23
20
24
- use log:: warn;
25
21
use tide:: { Body , Response , Server } ;
26
22
23
+ mod serve_dir;
24
+ use serve_dir:: serve_dir;
25
+
27
26
#[ cfg( feature = "demo_mode" ) ]
28
27
mod consts {
29
28
pub const WEBUI_DIR : & str = "web/build" ;
29
+ pub const EXTRA_DIR : & str = "demo_files/srv/www" ;
30
30
pub const FS_PREFIX : & str = "demo_files" ;
31
31
pub const FALLBACK_PORT : & str = "[::]:8080" ;
32
32
}
33
33
34
34
#[ cfg( not( feature = "demo_mode" ) ) ]
35
35
mod consts {
36
36
pub const WEBUI_DIR : & str = "/usr/share/tacd/webui" ;
37
+ pub const EXTRA_DIR : & str = "/srv/www" ;
37
38
pub const FS_PREFIX : & str = "" ;
38
39
pub const FALLBACK_PORT : & str = "[::]:80" ;
39
40
}
40
41
41
- use consts:: { FALLBACK_PORT , FS_PREFIX , WEBUI_DIR } ;
42
+ use consts:: { EXTRA_DIR , FALLBACK_PORT , FS_PREFIX , WEBUI_DIR } ;
42
43
43
44
// openapi.json is generated by build.rs from openapi.yaml
44
45
const OPENAPI_JSON : & [ u8 ] = include_bytes ! ( concat!( env!( "OUT_DIR" ) , "/openapi.json" ) ) ;
@@ -74,8 +75,8 @@ impl HttpServer {
74
75
) ;
75
76
76
77
this. expose_openapi_json ( ) ;
77
- this. expose_dir ( WEBUI_DIR , "/" ) ;
78
- this. expose_dir ( FS_PREFIX . to_owned ( ) + "/srv/www " , "/srv/" ) ;
78
+ this. expose_dir ( WEBUI_DIR , "/" , false ) ;
79
+ this. expose_dir ( EXTRA_DIR , "/srv" , true ) ;
79
80
80
81
for ( fs_path, web_path) in EXPOSED_FILES_RW {
81
82
let fs_path = FS_PREFIX . to_owned ( ) + * fs_path;
@@ -99,45 +100,12 @@ impl HttpServer {
99
100
}
100
101
101
102
/// Serve a directory from disk for reading
102
- fn expose_dir ( & mut self , fs_path : impl AsRef < Path > , web_path : & str ) {
103
- if let Err ( e) = self . server . at ( web_path) . serve_dir ( & fs_path) {
104
- // Don't crash if the directory does not exist.
105
- // Just print a warning.
106
- match e. kind ( ) {
107
- ErrorKind :: NotFound => {
108
- warn ! (
109
- "Can not serve {} at {}: Directory not found" ,
110
- fs_path. as_ref( ) . display( ) ,
111
- web_path
112
- ) ;
113
- }
114
- _ => Err ( e) . unwrap ( ) ,
115
- }
116
- }
103
+ fn expose_dir ( & mut self , fs_path : & ' static str , web_path : & str , directory_listings : bool ) {
104
+ let handler = move |req| async move { serve_dir ( fs_path, directory_listings, req) . await } ;
117
105
118
- // Serve an index.html if the bare directory path is requested.
119
- // This only works for the top level. If we want to serve index.htmls
120
- // from sub-directories we would have to modify serve_dir().
121
- // Which is something we will likely want anyways as it does not
122
- // support compression, caching headers or directory listings.
123
- if web_path. ends_with ( '/' ) {
124
- let index_html = fs_path. as_ref ( ) . join ( "index.html" ) ;
125
-
126
- if let Err ( e) = self . server . at ( web_path) . serve_file ( & index_html) {
127
- // Don't crash if the directory does not exist. Just print a
128
- // warning.
129
- match e. kind ( ) {
130
- ErrorKind :: NotFound => {
131
- warn ! (
132
- "Can not serve {} at {}: File not found" ,
133
- index_html. display( ) ,
134
- web_path
135
- ) ;
136
- }
137
- _ => Err ( e) . unwrap ( ) ,
138
- }
139
- }
140
- }
106
+ self . server . at ( web_path) . get ( handler) ;
107
+ self . server . at ( web_path) . at ( "" ) . get ( handler) ;
108
+ self . server . at ( web_path) . at ( "*rel_path" ) . get ( handler) ;
141
109
}
142
110
143
111
/// Serve a file from disk for reading and writing
0 commit comments