Skip to content

Commit

Permalink
[server] Add CORS allow header tp responses #1313 (#1314)
Browse files Browse the repository at this point in the history
add cors headers for preflight requests
  • Loading branch information
michaelvlach authored Oct 20, 2024
1 parent 88b9b1a commit 35e80ae
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 0 deletions.
1 change: 1 addition & 0 deletions agdb/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ use crate::{
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
#[derive(Debug, PartialEq)]
#[expect(clippy::large_enum_variant)]
pub enum QueryType {
InsertAlias(InsertAliasesQuery),
InsertEdges(InsertEdgesQuery),
Expand Down
1 change: 1 addition & 0 deletions agdb_server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ serde_json = "1"
serde_yaml = "0.9"
tokio = { version = "1", features = ["full"] }
tower = "0.4"
tower-http = { version = "0.6", features = ["cors"] }
tracing = "0.1"
tracing-subscriber = "0.3"
url = { version = "2", features = ["serde"] }
Expand Down
7 changes: 7 additions & 0 deletions agdb_server/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ use crate::server_state::ServerState;
use axum::middleware;
use axum::routing;
use axum::Router;
use reqwest::Method;
use tokio::sync::broadcast::Sender;
use tower_http::cors::CorsLayer;
use utoipa::OpenApi;
use utoipa_rapidoc::RapiDoc;

Expand Down Expand Up @@ -142,13 +144,18 @@ pub(crate) fn app(
routing::put(routes::user::change_password),
);

let cors = CorsLayer::new()
.allow_methods([Method::GET, Method::POST, Method::PUT, Method::DELETE])
.allow_origin(tower_http::cors::Any);

let router = Router::new()
.merge(RapiDoc::with_openapi("/api/v1/openapi.json", Api::openapi()).path("/api/v1"))
.nest("/api/v1", api_v1)
.layer(middleware::from_fn_with_state(
state.clone(),
logger::logger,
))
.layer(cors)
.with_state(state);

if !basepath.is_empty() {
Expand Down

0 comments on commit 35e80ae

Please sign in to comment.