File tree 2 files changed +34
-2
lines changed
2 files changed +34
-2
lines changed Original file line number Diff line number Diff line change
1
+ use rocket:: fairing:: { Fairing , Info , Kind } ;
2
+ use rocket:: http:: Header ;
3
+ use rocket:: response:: status:: NoContent ;
4
+ use rocket:: { Request , Response } ;
5
+
6
+ pub struct Cors ;
7
+
8
+ #[ rocket:: async_trait]
9
+ impl Fairing for Cors {
10
+ fn info ( & self ) -> Info {
11
+ Info {
12
+ name : "Add CORS headers to responses" ,
13
+ kind : Kind :: Response ,
14
+ }
15
+ }
16
+
17
+ async fn on_response < ' r > ( & self , _request : & ' r Request < ' _ > , response : & mut Response < ' r > ) {
18
+ response. set_header ( Header :: new ( "Access-Control-Allow-Origin" , "*" ) ) ;
19
+ response. set_header ( Header :: new ( "Access-Control-Allow-Methods" , "*" ) ) ;
20
+ response. set_header ( Header :: new ( "Access-Control-Allow-Headers" , "*" ) ) ;
21
+ response. set_header ( Header :: new ( "Access-Control-Expose-Headers" , "*" ) ) ;
22
+ response. set_header ( Header :: new ( "Access-Control-Allow-Credentials" , "true" ) ) ;
23
+ response. set_header ( Header :: new ( "Access-Control-Max-Age" , "7200" ) ) ;
24
+ }
25
+ }
26
+
27
+ #[ options( "/<_..>" ) ]
28
+ pub fn handle_preflight ( ) -> NoContent {
29
+ NoContent
30
+ }
Original file line number Diff line number Diff line change 1
1
#[ macro_use]
2
2
extern crate rocket;
3
3
4
+ mod cors;
5
+
4
6
/// Serve the current system state
5
7
#[ get( "/state" ) ]
6
8
fn serve_state ( ) {
@@ -12,8 +14,8 @@ fn rocket() -> _ {
12
14
// TODO: spawn discovery tokio task here
13
15
14
16
rocket:: build ( )
15
- . mount ( "/" , routes ! [ status ] )
16
- . mount ( "/" , routes ! [ serve_state] )
17
+ . attach ( cors :: Cors )
18
+ . mount ( "/" , routes ! [ status , cors :: handle_preflight , serve_state] )
17
19
}
18
20
19
21
#[ get( "/status" ) ]
You can’t perform that action at this time.
0 commit comments