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 15, 2024
1 parent a7ab204 commit c2c3db8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 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
15 changes: 13 additions & 2 deletions src/api/mod.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
use axum::{extract::DefaultBodyLimit, routing::get, routing::post, Router};
use tower_http::{limit::RequestBodyLimitLayer, trace::TraceLayer};
use axum::{
extract::DefaultBodyLimit,
routing::{get, post},
Router,
};
use tower_http::{limit::RequestBodyLimitLayer, trace::TraceLayer, cors::CorsLayer};

pub mod invoices;

pub fn app() -> Router<crate::state::State> {
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 c2c3db8

Please sign in to comment.