Skip to content

Commit

Permalink
[feat] Add CORS
Browse files Browse the repository at this point in the history
  • Loading branch information
ppodolsky committed Apr 26, 2024
1 parent a3eb4a0 commit 1008ed4
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
20 changes: 20 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@ thiserror = "1.0"
tokio = { version = "1", features = ["full"] }
tokio-stream = "0.1"
tokio-util = "0.7"
tower-http = { version = "0.5", features = ["trace"] }
tower-http = { version = "0.5", features = ["trace", "cors"] }
tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
futures = "0.3"
lru = "0.12"
tokio-task-pool = "0.1"
url = "2.5.0"
mime_guess = "2.0.4"
6 changes: 6 additions & 0 deletions bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use tokio_util::io::{ReaderStream, StreamReader};
use tokio_util::sync::CancellationToken;
use tokio_util::task::TaskTracker;
use tower_http::trace::{self, TraceLayer};
use tower_http::cors::CorsLayer;
use tracing::{info, info_span, Instrument, Level};
use tracing_subscriber::EnvFilter;
use trident_storage::config::{Config, TableConfig};
Expand Down Expand Up @@ -166,6 +167,7 @@ async fn app() -> Result<(), Error> {
.make_span_with(trace::DefaultMakeSpan::new().level(Level::INFO))
.on_response(trace::DefaultOnResponse::new().level(Level::INFO)),
)
.layer(CorsLayer::permissive())
.with_state(state);

// run our app with hyper, listening globally on port 3000
Expand Down Expand Up @@ -263,11 +265,13 @@ async fn blobs_get(
Ok(Some((reader, file_size))) => match method {
Method::HEAD => Response::builder()
.header("Content-Length", file_size)
.header("Content-Type", mime_guess::mime::OCTET_STREAM.to_string())
.header("X-Iroh-Hash", hash_str)
.body(Body::default())
.unwrap(),
Method::GET => Response::builder()
.header("Content-Length", file_size)
.header("Content-Type", mime_guess::mime::OCTET_STREAM.to_string())
.header("X-Iroh-Hash", hash_str)
.body(Body::from_stream(ReaderStream::new(reader)))
.unwrap(),
Expand Down Expand Up @@ -501,11 +505,13 @@ async fn table_get(
Ok(Some((reader, file_size, hash))) => match method {
Method::HEAD => Response::builder()
.header("Content-Length", file_size)
.header("Content-Type", mime_guess::from_ext(&key).first_or_octet_stream().to_string())
.header("X-Iroh-Hash", hash.to_string())
.body(Body::default())
.unwrap(),
Method::GET => Response::builder()
.header("Content-Length", file_size)
.header("Content-Type", mime_guess::from_ext(&key).first_or_octet_stream().to_string())
.header("X-Iroh-Hash", hash.to_string())
.body(Body::from_stream(ReaderStream::new(reader)))
.unwrap(),
Expand Down

0 comments on commit 1008ed4

Please sign in to comment.