Skip to content

Commit

Permalink
Try fix CORS
Browse files Browse the repository at this point in the history
  • Loading branch information
bennekrouf committed Jan 20, 2024
1 parent e1a43e7 commit d0571ff
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 29 deletions.
23 changes: 23 additions & 0 deletions src/cors.rs
Original file line number Diff line number Diff line change
@@ -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"));
}
}
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 6 additions & 29 deletions src/server.rs
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit d0571ff

Please sign in to comment.