Skip to content

Commit ed36824

Browse files
committed
feat: add CORS headers
1 parent c35c02d commit ed36824

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

Cargo.lock

Lines changed: 37 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ license = "GPL-3.0"
1010
rocket = {version = "0.5.0", features = ["json", "tls"]}
1111
serde = "1.0.197"
1212
dotenv = "0.15.0"
13+
rocket_cors = "0.6.0"
1314

1415
[dependencies.mongodb]
1516
version = "2.8.2"

src/main.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ use dotenv::dotenv;
88

99
#[macro_use]
1010
extern crate rocket;
11-
use rocket::{http::Status, Request};
11+
use rocket::{
12+
http::{Method, Status},
13+
Request,
14+
};
15+
use rocket_cors::{AllowedOrigins, CorsOptions};
1216

1317
use crate::controllers::contactinfo_controller::get_contactinfo;
1418
use crate::controllers::location_controller::get_locations;
@@ -39,7 +43,14 @@ fn rocket() -> _ {
3943
Ok(db) => db,
4044
Err(_) => panic!("Couldn't connect to database"),
4145
};
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);
51+
4252
rocket::build()
53+
.attach(cors.to_cors().unwrap())
4354
.register("/", catchers![default_catcher])
4455
.manage(db)
4556
.mount(

0 commit comments

Comments
 (0)