Skip to content

Add cors origin layer. #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 34 additions & 8 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
11 changes: 9 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down