Skip to content

Commit

Permalink
Merge pull request #24 from Tietokilta/feat/add-cors
Browse files Browse the repository at this point in the history
feat: add cors headers
  • Loading branch information
tonitert authored Aug 15, 2024
2 parents a7ab204 + aa1aac5 commit d39519d
Show file tree
Hide file tree
Showing 2 changed files with 13 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
14 changes: 12 additions & 2 deletions src/api/mod.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
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::{cors::CorsLayer, limit::RequestBodyLimitLayer, trace::TraceLayer};

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 d39519d

Please sign in to comment.