From d0571ffde6c780536e865a47e00de76c3d741c77 Mon Sep 17 00:00:00 2001 From: Bennekrouf Date: Sat, 20 Jan 2024 11:29:49 +0100 Subject: [PATCH] Try fix CORS --- src/cors.rs | 23 +++++++++++++++++++++++ src/main.rs | 1 + src/server.rs | 35 ++++++----------------------------- 3 files changed, 30 insertions(+), 29 deletions(-) create mode 100644 src/cors.rs diff --git a/src/cors.rs b/src/cors.rs new file mode 100644 index 0000000..1160418 --- /dev/null +++ b/src/cors.rs @@ -0,0 +1,23 @@ +// cors.rs +use rocket::{Request, Response}; +use rocket::fairing::{Fairing, Info, Kind}; +use rocket::http::Header; + +pub struct CORS; + +#[rocket::async_trait] +impl Fairing for CORS { + fn info(&self) -> Info { + Info { + name: "Add CORS headers to responses", + kind: Kind::Response + } + } + + async fn on_response<'r>(&self, _request: &'r Request<'_>, response: &mut Response<'r>) { + response.set_header(Header::new("Access-Control-Allow-Origin", "*")); + response.set_header(Header::new("Access-Control-Allow-Methods", "POST, GET, PATCH, OPTIONS, PUT, DELETE")); + response.set_header(Header::new("Access-Control-Allow-Headers", "Content-Type, Authorization, Accept")); + response.set_header(Header::new("Access-Control-Allow-Credentials", "true")); + } +} diff --git a/src/main.rs b/src/main.rs index c6a1f89..d6331dc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -56,6 +56,7 @@ mod utils { mod validator; mod server; mod xhr_guard; +mod cors; #[tokio::main] async fn main() { // Check deserialization of all files before starting the server diff --git a/src/server.rs b/src/server.rs index 6a79d55..bdc2ab4 100644 --- a/src/server.rs +++ b/src/server.rs @@ -1,44 +1,21 @@ -use rocket::{routes}; -use rocket::config::Config; -// use rocket::http::Method; -use rocket::fairing::{Fairing, Info, Kind}; -use rocket::http::Header; -use rocket::{Rocket, Build, Request, Response}; -use crate::api::ping::ping; - use std::env; use log::LevelFilter; +use rocket::routes; +use rocket::config::Config; +use rocket::{Rocket, Build}; +use crate::api::ping::ping; use crate::utils::data_folder_path; +use crate::utils::yml_path::load_config; use crate::domain::all_db; use crate::api::verse_by_chapter::get_verse; use crate::api::get_chapters::get_chapters; use crate::api::get_labels::get_labels; use crate::api::generate_exercise_endpoint::generate_exercise_list_endpoint; - -// use crate::api::verse_similar_by_chapter::static_rocket_route_info_for_get_verse_similar_by_chapter_route; use crate::api::verse_similar_by_chapter::get_verse_similar_by_chapter_route; -use crate::utils::yml_path::load_config; - -pub struct CORS; -#[rocket::async_trait] -impl Fairing for CORS { - fn info(&self) -> Info { - Info { - name: "Add CORS headers to responses", - kind: Kind::Response - } - } - - async fn on_response<'r>(&self, _request: &'r Request<'_>, response: &mut Response<'r>) { - response.set_header(Header::new("Access-Control-Allow-Origin", "*")); - response.set_header(Header::new("Access-Control-Allow-Methods", "POST, GET, PATCH, OPTIONS")); - response.set_header(Header::new("Access-Control-Allow-Headers", "*")); - response.set_header(Header::new("Access-Control-Allow-Credentials", "true")); - } -} +use crate::cors::CORS; pub async fn start_server() { // Set the log level based on the RUST_LOG environment variable