Skip to content

Commit

Permalink
Merge pull request #51 from krivahtoo/fix/cors
Browse files Browse the repository at this point in the history
fix: Add CORS support to Axum server in Cargo.toml and main.rs
  • Loading branch information
krivahtoo authored Sep 12, 2024
2 parents dc62988 + a9e3c79 commit e41619b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
2 changes: 1 addition & 1 deletion crates/server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ serde = { version = "1.0.203", features = ["derive"] }
serde_json = "1.0.117"
sqlx = { version = "0.7.4", features = ["chrono", "sqlite", "macros", "uuid", "migrate", "runtime-tokio"] }
tokio = { version = "1.37.0", features = ["rt", "rt-multi-thread", "macros", "net", "signal"] }
tower-http = { version = "0.5.2", features = ["fs", "trace", "timeout"] }
tower-http = { version = "0.5.2", features = ["fs", "trace", "timeout", "cors"] }
tracing = "0.1.40"
tracing-subscriber = "0.3.18"
apalis = { version = "0.5.3", features = ["sqlite", "retry", "timeout", "cron"] }
Expand Down
6 changes: 5 additions & 1 deletion crates/server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ use apalis::{
prelude::*,
sqlite::SqliteStorage,
};
use axum::{routing::get, Router};
use axum::{http::HeaderValue, routing::get, Router};
use sqlx::{migrate::MigrateDatabase, Sqlite, SqlitePool};
use tokio::{signal, sync::broadcast};
use tower_http::{
cors::CorsLayer,
services::{ServeDir, ServeFile},
timeout::TimeoutLayer as HttpTimeoutLayer,
trace::{DefaultMakeSpan, DefaultOnRequest, DefaultOnResponse, TraceLayer as HttpTraceLayer},
Expand Down Expand Up @@ -96,6 +97,9 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Graceful shutdown will wait for outstanding requests to complete. Add a timeout so
// requests don't hang forever.
HttpTimeoutLayer::new(Duration::from_secs(10)),
CorsLayer::new()
.allow_origin("http://localhost:5173".parse::<HeaderValue>().unwrap())
.allow_methods(tower_http::cors::Any),
));

// run our app with hyper, listening globally on env.port
Expand Down

0 comments on commit e41619b

Please sign in to comment.