From 54d6026553009b46be7b56f57809101e430f191b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20Gu=CC=88ldenstein?= Date: Sun, 12 May 2024 08:58:44 +0200 Subject: [PATCH] Add cors --- Cargo.lock | 17 +++++++++++++++++ Cargo.toml | 1 + src/main.rs | 9 ++++++++- 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index f480d0d..a7d59cc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -19,6 +19,21 @@ dependencies = [ "tracing", ] +[[package]] +name = "actix-cors" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f9e772b3bcafe335042b5db010ab7c09013dad6eac4915c91d8d50902769f331" +dependencies = [ + "actix-utils", + "actix-web", + "derive_more", + "futures-util", + "log", + "once_cell", + "smallvec", +] + [[package]] name = "actix-http" version = "3.6.0" @@ -564,6 +579,7 @@ dependencies = [ "futures-task", "pin-project-lite", "pin-utils", + "slab", ] [[package]] @@ -1653,6 +1669,7 @@ dependencies = [ name = "xstatus" version = "0.1.0" dependencies = [ + "actix-cors", "actix-web", "config", "log", diff --git a/Cargo.toml b/Cargo.toml index e759fd2..f5f2955 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,7 @@ edition = "2021" [dependencies] config = "0.14.0" actix-web = "^4" +actix-cors = "0.7" serde = { version = "^1", features = ["derive"] } spaceapi = "^0.9" log = "^0.4" diff --git a/src/main.rs b/src/main.rs index e5c0cfb..d2307d1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,7 +2,8 @@ mod api; mod errors; mod settings; -use actix_web::{middleware::Logger, web::Data, App, HttpServer}; +use actix_cors::Cors; +use actix_web::{http, middleware::Logger, web::Data, App, HttpServer}; use spaceapi::Status; use std::sync::Mutex; @@ -29,8 +30,14 @@ async fn main() -> std::io::Result<()> { }); HttpServer::new(move || { let logger = Logger::default(); + let cors = Cors::default() + .allow_any_origin() + .allowed_methods(vec!["GET", "POST"]) + .allowed_headers(vec![http::header::AUTHORIZATION, http::header::ACCEPT]) + .allowed_header(http::header::CONTENT_TYPE); App::new() .wrap(logger) + .wrap(cors) .service(api::get_status) .service(api::set_state) .app_data(app_state_data.clone())