Skip to content

Commit

Permalink
Merge pull request #275 from adorsys/274-add-cors-layer-to-the-mediat…
Browse files Browse the repository at this point in the history
…or-implementation

Add CORS layer to mediator server for cross-origin requests
  • Loading branch information
Hermann-Core authored Dec 3, 2024
2 parents 8095d32 + 31e01d9 commit 67393f1
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 13 deletions.
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

0 comments on commit 67393f1

Please sign in to comment.