@@ -5,7 +5,8 @@ use std::{marker::PhantomData, sync::Arc, env, net::{Ipv4Addr, IpAddr}};
5
5
use anyhow:: anyhow;
6
6
use config:: { deserialize_mars_config, MarsConfig } ;
7
7
use database:: { Database , cache:: { Cache , get_redis_pool, RedisAdapter } , models:: { player:: Player , r#match:: Match } } ;
8
- use rocket:: { Build , Rocket , Shutdown , Config , figment:: Figment } ;
8
+ use rocket:: { figment:: Figment , http:: Method , Build , Config , Rocket , Shutdown } ;
9
+ use rocket_cors:: { AllowedOrigins , CorsOptions } ;
9
10
use socket:: leaderboard:: MarsLeaderboards ;
10
11
use crate :: database:: migrations:: MigrationExecutor ;
11
12
@@ -82,8 +83,21 @@ fn rocket(state: MarsAPIState) -> Rocket<Build> {
82
83
rocket_build
83
84
}
84
85
86
+ fn get_cors_configuration ( ) -> CorsOptions {
87
+ CorsOptions :: default ( )
88
+ . allowed_origins ( AllowedOrigins :: all ( ) )
89
+ . allowed_methods (
90
+ vec ! [ Method :: Get , Method :: Post ]
91
+ . into_iter ( )
92
+ . map ( From :: from)
93
+ . collect ( ) ,
94
+ )
95
+ . allow_credentials ( true )
96
+ }
97
+
85
98
async fn spawn_rocket ( state : MarsAPIState ) -> Result < Shutdown , String > {
86
- let rocket = match rocket ( state) . ignite ( ) . await {
99
+ let cors = get_cors_configuration ( ) . to_cors ( ) . unwrap ( ) ;
100
+ let rocket = match rocket ( state) . attach ( cors) . ignite ( ) . await {
87
101
Ok ( rocket) => rocket,
88
102
Err ( rocket_err) => return Err ( format ! ( "{}" , rocket_err) )
89
103
} ;
0 commit comments