From a0edcc9ddfa66c77e20857464cc642ea2d53e2e9 Mon Sep 17 00:00:00 2001 From: Fleeym <61891787+Fleeym@users.noreply.github.com> Date: Tue, 5 Mar 2024 00:52:53 +0200 Subject: [PATCH] add cors --- Cargo.lock | 16 ++++++++++++++++ Cargo.toml | 1 + src/main.rs | 15 +++++++++++++++ 3 files changed, 32 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index e63b3eb..080eedf 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.5.1" @@ -1024,6 +1039,7 @@ dependencies = [ name = "geode-index" version = "0.1.0" dependencies = [ + "actix-cors", "actix-web", "anyhow", "chrono", diff --git a/Cargo.toml b/Cargo.toml index d68a5f9..c9ff956 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,3 +29,4 @@ semver = "1.0.21" clap = { version = "4.5.1", features = ["derive"] } regex = "1.10.3" chrono = "0.4.34" +actix-cors = "0.7.0" diff --git a/src/main.rs b/src/main.rs index fdbd336..ba519df 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,7 @@ +use actix_cors::Cors; use actix_web::{ get, + http::header, middleware::Logger, web::{self, QueryConfig}, App, HttpServer, Responder, @@ -77,12 +79,25 @@ async fn main() -> anyhow::Result<()> { log::info!("Job {} completed", s); return anyhow::Ok(()); } + let cors = Cors::default() + .allow_any_origin() + .allow_any_method() + .allow_any_header() + .max_age(3600); info!("Starting server on {}:{}", addr, port); let server = HttpServer::new(move || { App::new() .app_data(web::Data::new(app_data.clone())) .app_data(QueryConfig::default().error_handler(api::query_error_handler)) + .wrap( + Cors::default() + .allow_any_origin() + .allowed_methods(vec!["GET", "POST", "PUT", "PATCH", "DELETE", "HEAD"]) + .allow_any_header() + .supports_credentials() + .max_age(3600), + ) .wrap(Logger::default()) .service(endpoints::mods::index) .service(endpoints::mods::get_mod_updates)