@@ -64,27 +64,30 @@ pub async fn serve(
64
64
static_prefix = String :: from ( "/" ) ;
65
65
}
66
66
67
- app = app. service (
68
- Files :: new ( & static_prefix, root_path. join ( "public" ) )
69
- . index_file ( "index.html" )
70
- // This handler check if there's an HTML file in the public folder that
71
- // can reply to the given request. For example, if someone request /about,
72
- // this handler will look for a /public/about.html file.
73
- . default_handler ( fn_service ( |req : ServiceRequest | async {
74
- let ( req, _) = req. into_parts ( ) ;
67
+ let public_dir = root_path. join ( "public" ) ;
68
+ if public_dir. exists ( ) {
69
+ app = app. service (
70
+ Files :: new ( & static_prefix, public_dir)
71
+ . index_file ( "index.html" )
72
+ // This handler check if there's an HTML file in the public folder that
73
+ // can reply to the given request. For example, if someone request /about,
74
+ // this handler will look for a /public/about.html file.
75
+ . default_handler ( fn_service ( |req : ServiceRequest | async {
76
+ let ( req, _) = req. into_parts ( ) ;
75
77
76
- match handle_assets ( & req) . await {
77
- Ok ( existing_file) => {
78
- let res = existing_file. into_response ( & req) ;
79
- Ok ( ServiceResponse :: new ( req, res) )
78
+ match handle_assets ( & req) . await {
79
+ Ok ( existing_file) => {
80
+ let res = existing_file. into_response ( & req) ;
81
+ Ok ( ServiceResponse :: new ( req, res) )
82
+ }
83
+ Err ( _) => {
84
+ let res = handle_not_found ( & req) . await ;
85
+ Ok ( ServiceResponse :: new ( req, res) )
86
+ }
80
87
}
81
- Err ( _) => {
82
- let res = handle_not_found ( & req) . await ;
83
- Ok ( ServiceResponse :: new ( req, res) )
84
- }
85
- }
86
- } ) ) ,
87
- ) ;
88
+ } ) ) ,
89
+ ) ;
90
+ }
88
91
89
92
app
90
93
} )
0 commit comments