@@ -86,27 +86,30 @@ pub async fn serve(
86
86
static_prefix = String :: from ( "/" ) ;
87
87
}
88
88
89
- app = app. service (
90
- Files :: new ( & static_prefix, root_path. join ( "public" ) )
91
- . index_file ( "index.html" )
92
- // This handler check if there's an HTML file in the public folder that
93
- // can reply to the given request. For example, if someone request /about,
94
- // this handler will look for a /public/about.html file.
95
- . default_handler ( fn_service ( |req : ServiceRequest | async {
96
- let ( req, _) = req. into_parts ( ) ;
89
+ let public_dir = root_path. join ( "public" ) ;
90
+ if public_dir. exists ( ) {
91
+ app = app. service (
92
+ Files :: new ( & static_prefix, public_dir)
93
+ . index_file ( "index.html" )
94
+ // This handler check if there's an HTML file in the public folder that
95
+ // can reply to the given request. For example, if someone request /about,
96
+ // this handler will look for a /public/about.html file.
97
+ . default_handler ( fn_service ( |req : ServiceRequest | async {
98
+ let ( req, _) = req. into_parts ( ) ;
97
99
98
- match handle_assets ( & req) . await {
99
- Ok ( existing_file) => {
100
- let res = existing_file. into_response ( & req) ;
101
- Ok ( ServiceResponse :: new ( req, res) )
100
+ match handle_assets ( & req) . await {
101
+ Ok ( existing_file) => {
102
+ let res = existing_file. into_response ( & req) ;
103
+ Ok ( ServiceResponse :: new ( req, res) )
104
+ }
105
+ Err ( _) => {
106
+ let res = handle_not_found ( & req) . await ;
107
+ Ok ( ServiceResponse :: new ( req, res) )
108
+ }
102
109
}
103
- Err ( _) => {
104
- let res = handle_not_found ( & req) . await ;
105
- Ok ( ServiceResponse :: new ( req, res) )
106
- }
107
- }
108
- } ) ) ,
109
- ) ;
110
+ } ) ) ,
111
+ ) ;
112
+ }
110
113
111
114
app
112
115
} )
0 commit comments