Skip to content

Commit

Permalink
fix: axum cors settings (#108)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gumichocopengin8 authored Dec 10, 2023
1 parent b234c2d commit aa7f93f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
4 changes: 2 additions & 2 deletions api/Cargo.lock

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

2 changes: 1 addition & 1 deletion api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ rust-version = "1.71"
[dependencies]
anyhow = "1"
axum = "0.7.2"
crates_io_api = "0.8"
crates_io_api = "0.9"
env_logger = "0.10"
http-body-util = "0.1.0"
serde_json = "1.0.107"
Expand Down
14 changes: 11 additions & 3 deletions api/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,17 @@ use axum::{
extract::Path,
http::{
header::{ACCEPT, AUTHORIZATION},
HeaderValue, Method, StatusCode,
request, HeaderValue, Method, StatusCode,
},
response::IntoResponse,
routing::get,
Json, Router,
};
use std::time::Duration;
use tower_http::{cors::CorsLayer, trace::TraceLayer};
use tower_http::{
cors::{AllowOrigin, CorsLayer},
trace::TraceLayer,
};
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};

#[tokio::main]
Expand Down Expand Up @@ -58,7 +61,12 @@ fn app() -> Router {
)
.layer(
CorsLayer::new()
.allow_origin("http://localhost:3000".parse::<HeaderValue>().unwrap())
.allow_origin(AllowOrigin::predicate(
|origin: &HeaderValue, _request_parts: &request::Parts| {
origin.as_bytes() == b"http://localhost:3000"
|| origin.as_bytes().ends_with(b".vercel.app")
},
))
.allow_methods([Method::GET])
.allow_headers([AUTHORIZATION, ACCEPT])
.max_age(Duration::from_secs(60) * 5),
Expand Down

0 comments on commit aa7f93f

Please sign in to comment.