From a8720a87211637cb335637b96e3487d0788104c1 Mon Sep 17 00:00:00 2001 From: James Date: Mon, 14 Apr 2025 15:02:08 -0400 Subject: [PATCH 1/2] feat: cors example --- Cargo.toml | 2 ++ examples/cors.rs | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 examples/cors.rs diff --git a/Cargo.toml b/Cargo.toml index 2b004f5..4a1f0a1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -42,6 +42,8 @@ futures-util = { version = "0.3.31", optional = true } tempfile = "3.15.0" tracing-subscriber = "0.3.19" axum = { version = "0.8.1", features = ["macros"] } +tower-http = { version = "0.6.2", features = ["cors"] } +tokio = { version = "1.43.0", features = ["full"] } [features] default = ["axum", "ws", "ipc"] diff --git a/examples/cors.rs b/examples/cors.rs new file mode 100644 index 0000000..71795c2 --- /dev/null +++ b/examples/cors.rs @@ -0,0 +1,44 @@ +use axum::http::{HeaderValue, Method}; +use std::{future::IntoFuture, net::SocketAddr}; +use tower_http::cors::{AllowOrigin, Any, CorsLayer}; + +fn make_cors(cors: Option<&str>) -> tower_http::cors::CorsLayer { + let cors = cors + .unwrap_or("*") + .parse::() + .map(Into::::into) + .unwrap_or_else(|_| AllowOrigin::any()); + + CorsLayer::new() + .allow_methods([Method::GET, Method::POST]) + .allow_origin(cors) + .allow_headers(Any) +} + +#[tokio::main] +async fn main() -> Result<(), Box> { + let cors = std::env::args().nth(1); + let port = std::env::args() + .nth(2) + .map(|src| u16::from_str_radix(&src, 10).ok()) + .flatten() + .unwrap_or(8080); + + let router = ajj::Router::<()>::new() + .route("helloWorld", || async { + tracing::info!("serving hello world"); + Ok::<_, ()>("Hello, world!") + }) + .route("addNumbers", |(a, b): (u32, u32)| async move { + tracing::info!("serving addNumbers"); + Ok::<_, ()>(a + b) + }) + .into_axum("/") + .layer(make_cors(cors.as_deref())); + + let addr = SocketAddr::from(([127, 0, 0, 1], port)); + let listener = tokio::net::TcpListener::bind(addr).await?; + + axum::serve(listener, router).into_future().await?; + Ok(()) +} From d06028345f2af9803082448f23d216343129a19c Mon Sep 17 00:00:00 2001 From: James Date: Mon, 28 Apr 2025 08:15:07 -0400 Subject: [PATCH 2/2] fix: clippy and feature checks --- Cargo.toml | 3 +++ examples/cors.rs | 5 ++--- src/lib.rs | 1 - 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 4a1f0a1..1825ea9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -39,12 +39,15 @@ tokio-tungstenite = { version = "0.26.1", features = ["rustls-tls-webpki-roots"] futures-util = { version = "0.3.31", optional = true } [dev-dependencies] +ajj = { path = "./", features = ["axum", "ws", "ipc"] } + tempfile = "3.15.0" tracing-subscriber = "0.3.19" axum = { version = "0.8.1", features = ["macros"] } tower-http = { version = "0.6.2", features = ["cors"] } tokio = { version = "1.43.0", features = ["full"] } + [features] default = ["axum", "ws", "ipc"] axum = ["dep:axum", "dep:mime"] diff --git a/examples/cors.rs b/examples/cors.rs index 71795c2..1173b71 100644 --- a/examples/cors.rs +++ b/examples/cors.rs @@ -20,9 +20,8 @@ async fn main() -> Result<(), Box> { let cors = std::env::args().nth(1); let port = std::env::args() .nth(2) - .map(|src| u16::from_str_radix(&src, 10).ok()) - .flatten() - .unwrap_or(8080); + .and_then(|src| src.parse().ok()) + .unwrap_or(8080u16); let router = ajj::Router::<()>::new() .route("helloWorld", || async { diff --git a/src/lib.rs b/src/lib.rs index cb67dc9..a5da1e6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -136,7 +136,6 @@ //! //! [`axum`]: https://docs.rs/axum/latest/axum/index.html //! [`axum::Router`]: https://docs.rs/axum/latest/axum/routing/struct.Router.html -//! [`ResponsePayload`]: alloy::rpc::json_rpc::ResponsePayload //! [`interprocess::local_socket::ListenerOptions`]: https://docs.rs/interprocess/latest/interprocess/local_socket/struct.ListenerOptions.html #![warn(