Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CORS layer to mediator server for cross-origin requests #275

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ serde_json.workspace = true
hyper.workspace = true
tokio = { workspace = true, features = ["full"] }
tracing-subscriber = { workspace = true, features = ["json"] }
tower-http = { workspace = true, features = ["catch-panic", "trace"] }
tower-http = { workspace = true, features = ["catch-panic", "trace", "cors"] }
chrono = { workspace = true, optional = true }
did-endpoint = { workspace = true, optional = true }
oob-messages = { workspace = true, optional = true }
Expand Down
16 changes: 9 additions & 7 deletions crates/web-plugins/did-endpoint/src/didgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ use crate::util;
pub enum Error {
#[error("Key Generation Error")]
KeyGenerationError,
#[error("Key Storing Error")]
KeyStoringError,
#[error("Key Conversion Error")]
KeyConversionError,
#[error("DID Generation Error")]
Expand Down Expand Up @@ -128,13 +130,13 @@ where

// Store the secret in the keystore
task::block_in_place(move || {
Handle::current().block_on(async move {
match keystore.store(secret).await {
Ok(_) => tracing::info!("Successfully stored secret."),
Err(error) => tracing::error!("Error storing secret: {:?}", error),
}
})
});
Handle::current().block_on(async move { keystore.store(secret).await })
})
.map(|_| tracing::info!("Successfully stored secret."))
.map_err(|err| {
tracing::error!("Error storing secret: {err:?}");
Error::KeyStoringError
})?;

Ok(())
}
Expand Down
15 changes: 13 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,28 @@ pub mod plugins;

use axum::Router;
use eyre::{eyre, Result};
use hyper::Method;
use plugins::manager::PluginContainer;
use tower_http::{catch_panic::CatchPanicLayer, trace::TraceLayer};
use tower_http::{
catch_panic::CatchPanicLayer,
cors::{Any, CorsLayer},
trace::TraceLayer,
};

pub fn app() -> Result<(PluginContainer<'static>, Router)> {
let mut container = PluginContainer::new();
container.load().map_err(|e| eyre!(e))?;

let cors = CorsLayer::new()
.allow_origin(Any)
.allow_methods([Method::GET, Method::POST, Method::OPTIONS])
.allow_headers(Any);

let router = Router::new()
.merge(container.routes().unwrap_or_default())
.layer(TraceLayer::new_for_http())
.layer(CatchPanicLayer::new());
.layer(CatchPanicLayer::new())
.layer(cors);

Ok((container, router))
}
6 changes: 3 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ async fn main() -> Result<()> {

generic_server_with_graceful_shutdown(addr)
.await
.map_err(|e| {
tracing::error!("{:?}", e);
e
.map_err(|err| {
tracing::error!("{err:?}");
err
})?;

Ok(())
Expand Down
Loading