diff --git a/Cargo.lock b/Cargo.lock index b08cbdc..d2f154b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -246,7 +246,7 @@ dependencies = [ "rustversion", "serde", "sync_wrapper 0.1.2", - "tower", + "tower 0.4.13", "tower-layer", "tower-service", ] @@ -282,7 +282,7 @@ dependencies = [ "sync_wrapper 1.0.1", "tokio", "tokio-tungstenite", - "tower", + "tower 0.4.13", "tower-layer", "tower-service", "tracing", @@ -1979,6 +1979,8 @@ dependencies = [ "shuttle-shared-db", "sqlx", "tokio", + "tower 0.5.1", + "tower-http", ] [[package]] @@ -2222,7 +2224,7 @@ dependencies = [ "serde_json", "strum 0.26.3", "thiserror", - "tower", + "tower 0.4.13", "tracing", "tracing-opentelemetry", "tracing-subscriber", @@ -2904,7 +2906,7 @@ dependencies = [ "prost", "tokio", "tokio-stream", - "tower", + "tower 0.4.13", "tower-layer", "tower-service", "tracing", @@ -2930,17 +2932,41 @@ dependencies = [ "tracing", ] +[[package]] +name = "tower" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2873938d487c3cfb9aed7546dc9f2711d867c9f90c46b889989a2cb84eba6b4f" +dependencies = [ + "tower-layer", + "tower-service", +] + +[[package]] +name = "tower-http" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8437150ab6bbc8c5f0f519e3d5ed4aa883a83dd4cdd3d1b21f9482936046cb97" +dependencies = [ + "bitflags 2.5.0", + "bytes", + "http 1.1.0", + "pin-project-lite", + "tower-layer", + "tower-service", +] + [[package]] name = "tower-layer" -version = "0.3.2" +version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c20c8dbed6283a09604c3e69b4b7eeb54e298b8a600d4d5ecb5ad39de609f1d0" +checksum = "121c2a6cda46980bb0fcd1647ffaf6cd3fc79a013de288782836f6df9c48780e" [[package]] name = "tower-service" -version = "0.3.2" +version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b6bc1c9ce2b5135ac7f93c72918fc37feb872bdc6a5533a8b85eb4b86bfdae52" +checksum = "8df9b6e13f2d32c91b9bd719c00d1958837bc7dec474d94952798cc8e69eeec3" [[package]] name = "tracing" diff --git a/Cargo.toml b/Cargo.toml index 5a315f4..55c1a58 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,4 +17,6 @@ tokio = "1.28.2" hmac = "0.12.1" sha2 = "0.10.8" hex = "0.4.3" +tower-http = { version = "0.6.1", features = ["cors"] } +tower = "0.5.1" chrono-tz = "0.10.0" diff --git a/src/main.rs b/src/main.rs index ed37c1f..a20ace8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,7 @@ use tokio::time::{ sleep_until, Instant}; use std::time::Duration; use async_graphql_axum::GraphQL; use axum::{routing::get, Router}; +use tower_http::cors::{Any, CorsLayer}; use chrono::{ Local, NaiveTime}; use chrono_tz::Asia::Kolkata; use db::member::Member; @@ -40,10 +41,16 @@ async fn main(#[shuttle_shared_db::Postgres] pool: PgPool,#[shuttle_runtime::Sec .finish(); let state = MyState { pool: pool.clone() , secret_key: secret_key.clone()}; - + + let cors = CorsLayer::new() + .allow_origin(Any) // Allow any origin + .allow_methods(tower_http::cors::Any) // Allow any HTTP method + .allow_headers(tower_http::cors::Any); + let router = Router::new() .route("/", get(graphiql).post_service(GraphQL::new(schema.clone()))) - .with_state(state); + .with_state(state) + .layer(cors); task::spawn(async move { schedule_task_at_midnight(pool.clone()).await; // Call the function after 10 seconds