Skip to content

Commit aafb9f2

Browse files
committed
added cors
1 parent e6061ca commit aafb9f2

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ axum = { version = "0.7.5", features = [
4747
"http2",
4848
], default-features = false }
4949
axum-server = { version = "0.6.0", features = ["tls-rustls"] }
50-
tower-http = { version = "0.5.2", features = ["compression-full"] }
50+
tower-http = { version = "0.5.2", features = ["compression-full", "cors"] }
5151
juniper_graphql_ws = "0.4.0"
5252
pretty_env_logger = "0.5.0"
5353
log = { version = "0.4.21" }

src/main.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,20 @@ use std::{
2121

2222
use axum::{
2323
body::Body,
24+
http::Method,
2425
response::Response,
2526
routing::{get, on, MethodFilter},
2627
Extension, Router,
2728
};
2829

30+
use crate::{cache::Multithreaded, fetch::make_client};
2931
use juniper::{graphql_object, EmptyMutation, EmptySubscription, RootNode};
3032
use juniper_axum::{graphiql, graphql, playground, ws};
3133
use juniper_graphql_ws::ConnectionConfig;
3234
use parse::Locations;
3335
use tokio::{net::TcpListener, sync::OnceCell, time::sleep};
34-
use tower_http::compression::CompressionLayer;
35-
36-
use crate::{cache::Multithreaded, fetch::make_client};
36+
use tower_http::cors::CorsLayer;
37+
use tower_http::{compression::CompressionLayer, cors::Any};
3738

3839
#[derive(Clone, Copy, Debug)]
3940
pub struct Query;
@@ -94,6 +95,9 @@ async fn main() {
9495
.deflate(true)
9596
.gzip(true)
9697
.zstd(true);
98+
let cors_layer = CorsLayer::new()
99+
.allow_methods([Method::GET, Method::POST]) // intentionally excludes request-refresh/PUT
100+
.allow_origin(Any);
97101
pretty_env_logger::init();
98102

99103
let app = Router::new()
@@ -111,6 +115,7 @@ async fn main() {
111115
.route("/request-refresh", on(MethodFilter::PUT, refresh))
112116
.route("/graphiql", get(graphiql("/graphql", "/subscriptions")))
113117
.route("/playground", get(playground("/graphql", "/subscriptions")))
118+
.layer(cors_layer)
114119
.layer(Extension(Arc::new(schema)))
115120
.layer(comression_layer);
116121
tokio::spawn(async move {

0 commit comments

Comments
 (0)