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();