Skip to content

Commit

Permalink
feat: add (hardcoded) cors headers
Browse files Browse the repository at this point in the history
  • Loading branch information
tonitert committed Aug 14, 2024
1 parent cf33a63 commit 9c76b8d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ serde = "1.0.195"
serde_derive = "1.0.195"
thiserror = "1.0.56"
tokio = { version = "1.35.1", features = ["full"] }
tower-http = { version = "0.5.1", features = ["trace", "limit"] }
tower-http = { version = "0.5.1", features = ["trace", "limit", "cors"] }
tracing = "0.1.40"
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
serde_json = "1.0.111"
Expand Down
9 changes: 8 additions & 1 deletion src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use axum::{
use std::sync::Arc;
use std::time::Duration;
use tower_governor::{governor::GovernorConfigBuilder, GovernorLayer};
use tower_http::{limit::RequestBodyLimitLayer, trace::TraceLayer};
use tower_http::{limit::RequestBodyLimitLayer, trace::TraceLayer, cors::CorsLayer};

pub mod invoices;

Expand All @@ -28,10 +28,17 @@ pub fn app() -> Router<crate::state::State> {
governor_limiter.retain_recent();
});

let cors_layer = CorsLayer::new()
.allow_origin([
"https://tietokilta.fi".parse().unwrap(),
"http://localhost:3000".parse().unwrap(),
]);

Router::new()
.route("/health", get(health))
.route("/invoices", post(invoices::create))
.layer(TraceLayer::new_for_http())
.layer(cors_layer)
.layer(DefaultBodyLimit::disable())
// Limit the body to 24 MiB since the email is limited to 25 MiB
.layer(RequestBodyLimitLayer::new(24 * 1024 * 1024))
Expand Down

0 comments on commit 9c76b8d

Please sign in to comment.