Skip to content

Commit

Permalink
Add cors headers
Browse files Browse the repository at this point in the history
  • Loading branch information
NyCodeGHG committed Aug 1, 2022
1 parent 3d65c7d commit c233f4c
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 7 deletions.
68 changes: 67 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "image-color-service"
version = "0.1.4"
version = "0.1.5"
edition = "2021"
authors = ["Marie Ramlow <me@nycode.dev>"]
license = "MIT"
Expand All @@ -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"
22 changes: 17 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -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
}

Expand Down

0 comments on commit c233f4c

Please sign in to comment.