Skip to content

Commit

Permalink
allow all origins by default
Browse files Browse the repository at this point in the history
  • Loading branch information
zaknesler committed Apr 24, 2024
1 parent 2af2090 commit 31b7504
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
2 changes: 1 addition & 1 deletion crates/blend-config/stubs/default.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ debug = true
[web]
host = "127.0.0.1"
port = 4000
allowed_origins = ["http://localhost:4000"]
allowed_origins = ["*"]

[database]
file = "blend.db"
Expand Down
31 changes: 17 additions & 14 deletions crates/blend-web/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use axum::http::{header, HeaderValue, Method};
use context::Context;
use tokio::net::TcpListener;
use tower_cookies::CookieManagerLayer;
use tower_http::{cors::CorsLayer, trace::TraceLayer};
use tower_http::{cors, trace::TraceLayer};

pub mod context;
pub mod error;
Expand All @@ -19,20 +19,23 @@ pub async fn serve(ctx: Context) -> WebResult<()> {
ctx.blend.config.web.port,
);

let allowed_origins = ctx
.blend
.config
.web
.allowed_origins
.iter()
.map(|origin| origin.parse::<HeaderValue>().map_err(|err| err.into()))
.collect::<WebResult<Vec<_>>>()?;

let cors = CorsLayer::new()
let mut cors = cors::CorsLayer::new()
.allow_methods([Method::GET, Method::POST, Method::PATCH, Method::DELETE])
.allow_headers([header::AUTHORIZATION, header::ACCEPT, header::CONTENT_TYPE])
.allow_origin(allowed_origins)
.allow_credentials(true);
.allow_headers([header::AUTHORIZATION, header::ACCEPT, header::CONTENT_TYPE]);

let origins = &ctx.blend.config.web.allowed_origins;
if origins.contains(&"*".to_string()) {
cors = cors.allow_origin(cors::Any)
} else {
cors = cors
.allow_origin(
origins
.iter()
.map(|origin| origin.parse::<HeaderValue>().map_err(|err| err.into()))
.collect::<WebResult<Vec<_>>>()?,
)
.allow_credentials(true);
}

let app = crate::router::router(ctx.clone())
.layer(TraceLayer::new_for_http())
Expand Down

0 comments on commit 31b7504

Please sign in to comment.