chore: added cors
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Andre Henriques 2023-07-10 09:53:36 +01:00
parent 099261247e
commit 2e86c05deb

View File

@ -12,7 +12,7 @@ use animations::Animation;
use r2d2::PooledConnection; use r2d2::PooledConnection;
use utils::ApiResponse; 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; use r2d2_sqlite::SqliteConnectionManager;
@ -154,6 +154,24 @@ fn ligth_controll(
println!("stoped main loop"); 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] #[rocket::main]
async fn main() -> GResult { async fn main() -> GResult {
println!("Program start"); println!("Program start");
@ -212,6 +230,7 @@ async fn main() -> GResult {
.manage(conn_pool_arc.clone()) .manage(conn_pool_arc.clone())
.manage(Arc::new(Mutex::new(action_sender))) .manage(Arc::new(Mutex::new(action_sender)))
.manage(Arc::new(Mutex::new(messager_revicer))) .manage(Arc::new(Mutex::new(messager_revicer)))
.attach(CORS)
.launch() .launch()
.await?; .await?;
ligths_controll.join().unwrap(); ligths_controll.join().unwrap();