1
1
use crate :: service:: ServiceContext ;
2
2
use api_docs:: ApiDocs ;
3
3
use log:: info;
4
- use rocket:: http:: { Method , Status } ;
4
+ use rocket:: http:: Method ;
5
5
use rocket:: { catch, catchers, routes, Build , Config , Request , Rocket } ;
6
6
use rocket_cors:: { AllowedHeaders , AllowedOrigins , CorsOptions } ;
7
7
use serde:: Serialize ;
@@ -75,8 +75,10 @@ pub fn rocket_main(context: ServiceContext) -> Rocket<Build> {
75
75
76
76
let rocket = rocket:: custom ( config)
77
77
. attach ( cors. clone ( ) )
78
+ // catchers for CORS and API errors
78
79
. mount ( "/api/" , rocket_cors:: catch_all_options_routes ( ) )
79
- . register ( "/" , catchers ! [ default_catcher, not_found] )
80
+ . mount ( "/api/" , routes ! [ handlers:: default_api_error_catcher] )
81
+ . register ( "/api/" , catchers ! [ not_found] )
80
82
. manage ( context)
81
83
. manage ( cors)
82
84
. mount ( "/api/exit" , routes ! [ handlers:: exit] )
@@ -179,14 +181,15 @@ pub fn rocket_main(context: ServiceContext) -> Rocket<Build> {
179
181
] ,
180
182
)
181
183
. mount (
182
- "/api/" ,
183
- SwaggerUi :: new ( "/swagger-ui/<_..>" ) . url ( "/api-docs/openapi.json" , ApiDocs :: openapi ( ) ) ,
184
+ "/" ,
185
+ SwaggerUi :: new ( "/api/swagger-ui/<_..>" )
186
+ . url ( "/api/api-docs/openapi.json" , ApiDocs :: openapi ( ) ) ,
184
187
)
188
+ // Routes for the frontend - lower rank means higher prio
185
189
. mount (
186
190
& CONFIG . frontend_url_path ,
187
191
FileServer :: from ( & CONFIG . frontend_serve_folder ) . rank ( 5 ) ,
188
192
)
189
- // TODO: fall back to index, but serve static files first
190
193
. mount ( & CONFIG . frontend_url_path , routes ! [ handlers:: serve_frontend] ) ;
191
194
192
195
info ! ( "HTTP Server Listening on {}" , conf. http_listen_url( ) ) ;
@@ -205,17 +208,8 @@ pub fn rocket_main(context: ServiceContext) -> Rocket<Build> {
205
208
rocket
206
209
}
207
210
208
- #[ catch( default ) ]
209
- pub fn default_catcher ( status : Status , _req : & Request ) -> Json < ErrorResponse > {
210
- Json ( ErrorResponse :: new (
211
- "error" ,
212
- status. reason ( ) . unwrap_or ( "Unknown error" ) . to_string ( ) ,
213
- status. code ,
214
- ) )
215
- }
216
-
217
211
#[ catch( 404 ) ]
218
- pub fn not_found ( req : & Request ) -> Json < ErrorResponse > {
212
+ fn not_found ( req : & Request ) -> Json < ErrorResponse > {
219
213
Json ( ErrorResponse :: new (
220
214
"not_found" ,
221
215
format ! ( "We couldn't find the requested path '{}'" , req. uri( ) ) ,
0 commit comments