Skip to content

Commit d8f5b9c

Browse files
committed
add: cors orgin layer
1 parent e5ce7a8 commit d8f5b9c

File tree

3 files changed

+46
-11
lines changed

3 files changed

+46
-11
lines changed

Cargo.lock

Lines changed: 34 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,6 @@ sqlx = { version = "0.7.1", features = ["chrono"] }
1616
tokio = "1.28.2"
1717
hmac = "0.12.1"
1818
sha2 = "0.10.8"
19-
hex = "0.4.3"
19+
hex = "0.4.3"
20+
tower-http = { version = "0.6.1", features = ["cors"] }
21+
tower = "0.5.1"

src/main.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use tokio::time::{ sleep_until, Instant};
55
use std::time::Duration;
66
use async_graphql_axum::GraphQL;
77
use axum::{routing::get, Router};
8+
use tower_http::cors::{Any, CorsLayer};
89
use chrono::{ Local, NaiveTime};
910

1011
use db::member::Member;
@@ -40,10 +41,16 @@ async fn main(#[shuttle_shared_db::Postgres] pool: PgPool,#[shuttle_runtime::Sec
4041
.finish();
4142

4243
let state = MyState { pool: pool.clone() , secret_key: secret_key.clone()};
43-
44+
45+
let cors = CorsLayer::new()
46+
.allow_origin(Any) // Allow any origin
47+
.allow_methods(tower_http::cors::Any) // Allow any HTTP method
48+
.allow_headers(tower_http::cors::Any);
49+
4450
let router = Router::new()
4551
.route("/", get(graphiql).post_service(GraphQL::new(schema.clone())))
46-
.with_state(state);
52+
.with_state(state)
53+
.layer(cors);
4754
task::spawn(async move {
4855

4956
schedule_task_at_midnight(pool.clone()).await; // Call the function after 10 seconds

0 commit comments

Comments
 (0)