diff --git a/Cargo.lock b/Cargo.lock index 4d27990..b82e952 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -340,6 +340,7 @@ dependencies = [ "serde", "tokio", "toml", + "tower-http", ] [[package]] @@ -928,6 +929,22 @@ dependencies = [ "tracing", ] +[[package]] +name = "tower-http" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e9cd434a998747dd2c4276bc96ee2e0c7a2eadf3cae88e52be55a05fa9053f5" +dependencies = [ + "bitflags", + "bytes", + "http", + "http-body", + "http-body-util", + "pin-project-lite", + "tower-layer", + "tower-service", +] + [[package]] name = "tower-layer" version = "0.3.2" diff --git a/Cargo.toml b/Cargo.toml index 38c82ec..9bf774a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,4 +13,5 @@ serde = { "version" = "1.0", features = ["derive"] } toml = "0.8" axum = { "version" = "0.7", features = ["macros"] } tokio = { "version" = "1.37" } -chrono = "0.4.38" +chrono = "0.4" +tower-http = { "version" = "0.5", features = ["cors"] } diff --git a/src/server.rs b/src/server.rs index bf284c5..40c0ff6 100644 --- a/src/server.rs +++ b/src/server.rs @@ -1,11 +1,12 @@ use axum::{ extract::{Path, State}, - http::StatusCode, + http::{Method, StatusCode}, response::IntoResponse, routing::get, Json, Router, }; use serde::{Deserialize, Serialize}; +use tower_http::cors::{Any, CorsLayer}; use crate::{ actuators, @@ -22,11 +23,16 @@ pub async fn run_server(program_state: ProgramStateShared) { } fn setup_router(program_state: ProgramStateShared) -> Router { + let cors = CorsLayer::new() + .allow_methods([Method::GET, Method::POST]) + .allow_origin(Any); + Router::new() .route("/info", get(info_handler)) .route("/switch/:device/:state", get(switch_handler)) .route("/pump/:quantity", get(pump_handler)) .with_state(program_state) + .layer(cors) } async fn pump_handler(