From c233f4cf50331585261c3cd97242f05af6d26d4b Mon Sep 17 00:00:00 2001 From: Marie Ramlow Date: Mon, 1 Aug 2022 00:07:00 +0000 Subject: [PATCH] Add cors headers --- Cargo.lock | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++- Cargo.toml | 5 +++- src/main.rs | 22 +++++++++++++---- 3 files changed, 88 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 83f0b4f..5696b51 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -19,6 +19,21 @@ dependencies = [ "tokio-util", ] +[[package]] +name = "actix-cors" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "414360eed71ba2d5435b185ba43ecbe281dfab5df3898286d6b7be8074372c92" +dependencies = [ + "actix-utils", + "actix-web", + "derive_more", + "futures-util", + "log", + "once_cell", + "smallvec", +] + [[package]] name = "actix-http" version = "3.2.1" @@ -228,6 +243,17 @@ dependencies = [ "alloc-no-stdlib", ] +[[package]] +name = "atty" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" +dependencies = [ + "hermit-abi", + "libc", + "winapi", +] + [[package]] name = "autocfg" version = "1.1.0" @@ -482,6 +508,19 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "env_logger" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b2cf0344971ee6c64c31be0d530793fba457d322dfec2810c453d0ef228f9c3" +dependencies = [ + "atty", + "humantime", + "log", + "regex", + "termcolor", +] + [[package]] name = "exr" version = "1.4.2" @@ -728,6 +767,12 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c4a1e36c821dbe04574f602848a19f742f4fb3c98d40449f11bcad18d6b17421" +[[package]] +name = "humantime" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" + [[package]] name = "idna" version = "0.2.3" @@ -760,12 +805,15 @@ dependencies = [ [[package]] name = "image-color-service" -version = "0.1.4" +version = "0.1.5" dependencies = [ + "actix-cors", "actix-web", "color-thief", + "env_logger", "futures", "image", + "log", "serde", "serde_json", "thiserror", @@ -1329,6 +1377,15 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "termcolor" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bab24d30b911b2376f3a13cc2cd443142f0c81dda04c118693e35b3835757755" +dependencies = [ + "winapi-util", +] + [[package]] name = "thiserror" version = "1.0.31" @@ -1583,6 +1640,15 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" +[[package]] +name = "winapi-util" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" +dependencies = [ + "winapi", +] + [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" diff --git a/Cargo.toml b/Cargo.toml index 94579e2..a77076c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "image-color-service" -version = "0.1.4" +version = "0.1.5" edition = "2021" authors = ["Marie Ramlow "] license = "MIT" @@ -10,9 +10,12 @@ description = "Small microservice which calculates the most dominant colors in a [dependencies] actix-web = "4" +actix-cors = "0.6" futures = "0.3" color-thief = "0.2" serde = { version = "1.0", features = ["derive"] } serde_json = "1" image = "0.24" thiserror = "1.0" +env_logger = "0.9" +log = "0.4" diff --git a/src/main.rs b/src/main.rs index 5b28aa4..c5432ae 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,26 +1,38 @@ +use actix_cors::Cors; use actix_web::{ error::PayloadError, get, - http::{header::ToStrError, StatusCode}, + http::StatusCode, post, web::{self, Payload}, App, HttpMessage, HttpRequest, HttpResponse, HttpServer, ResponseError, }; use futures::StreamExt; use image::{ImageError, ImageFormat}; +use log::info; use serde_json::json; use thiserror::Error; #[actix_web::main] async fn main() -> std::io::Result<()> { + env_logger::builder() + .filter_level(log::LevelFilter::Info) + .init(); let port: u16 = std::env::var("PORT") .map(|env| env.parse()) .unwrap_or(Ok(8080)) .unwrap(); - let server = HttpServer::new(|| App::new().service(color).service(index)) - .bind(("0.0.0.0", port))? - .run(); - println!("Listening on http://0.0.0.0:{}", port); + let server = HttpServer::new(|| { + let cors = Cors::default() + .allow_any_origin() + .allow_any_method() + .allow_any_header() + .max_age(3600); + App::new().wrap(cors).service(color).service(index) + }) + .bind(("0.0.0.0", port))? + .run(); + info!("Listening on http://0.0.0.0:{}", port); server.await }