@@ -9,10 +9,10 @@ use dotenv::dotenv;
9
9
#[ macro_use]
10
10
extern crate rocket;
11
11
use rocket:: {
12
- http:: { Method , Status } ,
13
- Request ,
12
+ fairing:: { Fairing , Info , Kind } ,
13
+ http:: { Header , Status } ,
14
+ Request , Response ,
14
15
} ;
15
- use rocket_cors:: { AllowedOrigins , CorsOptions } ;
16
16
17
17
use crate :: controllers:: contactinfo_controller:: get_contactinfo;
18
18
use crate :: controllers:: location_controller:: get_locations;
@@ -27,7 +27,7 @@ fn default_catcher(status: Status, _: &Request) -> Option<CustomError> {
27
27
}
28
28
29
29
#[ launch]
30
- fn rocket ( ) -> _ {
30
+ async fn rocket ( ) -> _ {
31
31
dotenv ( ) . ok ( ) ;
32
32
let uri = match env:: var ( "DATABASE_URL" ) {
33
33
Ok ( v) => v. to_string ( ) ,
@@ -44,15 +44,32 @@ fn rocket() -> _ {
44
44
Err ( _) => panic ! ( "Couldn't connect to database" ) ,
45
45
} ;
46
46
47
- let cors = CorsOptions :: default ( )
48
- . allowed_origins ( AllowedOrigins :: all ( ) )
49
- . allowed_methods ( vec ! [ Method :: Get ] . into_iter ( ) . map ( From :: from) . collect ( ) )
50
- . allow_credentials ( true ) ;
47
+ pub struct CORS ( ) ;
48
+
49
+ #[ rocket:: async_trait]
50
+ impl Fairing for CORS {
51
+ fn info ( & self ) -> Info {
52
+ Info {
53
+ name : "Add CORS headers to requests" ,
54
+ kind : Kind :: Response ,
55
+ }
56
+ }
57
+
58
+ async fn on_response < ' r > ( & self , _request : & ' r Request < ' _ > , response : & mut Response < ' r > ) {
59
+ response. set_header ( Header :: new ( "Access-Control-Allow-Origin" , "*" ) ) ;
60
+ response. set_header ( Header :: new (
61
+ "Access-Control-Allow-Methods" ,
62
+ "POST, GET, PATCH, OPTIONS" ,
63
+ ) ) ;
64
+ response. set_header ( Header :: new ( "Access-Control-Allow-Headers" , "*" ) ) ;
65
+ response. set_header ( Header :: new ( "Access-Control-Allow-Credentials" , "true" ) ) ;
66
+ }
67
+ }
51
68
52
69
rocket:: build ( )
53
- . attach ( cors. to_cors ( ) . unwrap ( ) )
54
70
. register ( "/" , catchers ! [ default_catcher] )
55
71
. manage ( db)
72
+ . attach ( CORS ( ) )
56
73
. mount (
57
74
"/" ,
58
75
routes ! [
0 commit comments