From 2e86c05debebcbdfbc90f6d52f86689c4657204d Mon Sep 17 00:00:00 2001 From: Andre Henriques Date: Mon, 10 Jul 2023 09:53:36 +0100 Subject: [PATCH] chore: added cors --- src/main.rs | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 34fef06..21da9c0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,7 +12,7 @@ use animations::Animation; use r2d2::PooledConnection; use utils::ApiResponse; -use rocket::{serde::json::serde_json::json, State}; +use rocket::{serde::json::serde_json::json, State, fairing::{Fairing, Info, Kind}, http::Header, Request, Response}; use r2d2_sqlite::SqliteConnectionManager; @@ -154,6 +154,24 @@ fn ligth_controll( println!("stoped main loop"); } +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")); + } +} + #[rocket::main] async fn main() -> GResult { println!("Program start"); @@ -212,6 +230,7 @@ async fn main() -> GResult { .manage(conn_pool_arc.clone()) .manage(Arc::new(Mutex::new(action_sender))) .manage(Arc::new(Mutex::new(messager_revicer))) + .attach(CORS) .launch() .await?; ligths_controll.join().unwrap();