From 878d2f0482066ab395c9a055b840d120d82c464c Mon Sep 17 00:00:00 2001 From: Silesmo Date: Sat, 27 Dec 2025 14:49:23 +0100 Subject: [PATCH 1/4] Update rust http template to use axum --- templates/rust-http/Cargo.toml | 10 ++++++++-- templates/rust-http/src/lib.rs | 21 +++++++++++++-------- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/templates/rust-http/Cargo.toml b/templates/rust-http/Cargo.toml index be8bd6b..e13d655 100644 --- a/templates/rust-http/Cargo.toml +++ b/templates/rust-http/Cargo.toml @@ -11,9 +11,15 @@ package = "myorg:{{name}}" proxy = false [dependencies] -waki = { version = "0.5.0", features = ["json"] } +wstd = { version = "0.6.3", features = ["json"] } +wstd-axum = { version = "0.6.3" } +axum = { version = "0.8.8", default-features = false, features = [ + "query", + "json", + "macros", +] } # reduce wasm binary size [profile.release] lto = true -strip = "symbols" \ No newline at end of file +strip = "symbols" diff --git a/templates/rust-http/src/lib.rs b/templates/rust-http/src/lib.rs index a8c53e7..bd7a91c 100644 --- a/templates/rust-http/src/lib.rs +++ b/templates/rust-http/src/lib.rs @@ -1,9 +1,14 @@ -use waki::{handler, ErrorCode, Request, Response}; +use axum::{ + http::StatusCode, + response::IntoResponse, + routing::{get, Router}, +}; -#[handler] -fn hello(req: Request) -> Result { - Response::builder() - .status_code(200) - .body("Hello World") - .build() -} \ No newline at end of file +#[wstd_axum::http_server] +fn main() -> Router { + Router::new().route("/hello", get(handler)) +} + +async fn handler() -> impl IntoResponse { + (StatusCode::Ok, "Hello, World!").into_response() +} From 04a9d6fbb90b901d110fe35e23a2bf3cb0c12f2f Mon Sep 17 00:00:00 2001 From: Silesmo Date: Sat, 27 Dec 2025 15:43:27 +0100 Subject: [PATCH 2/4] Updated 2 rust examples to use axum --- .../Cargo.toml | 13 ++++-- .../src/lib.rs | 40 ++++++++----------- .../Rust/basic-rust-application/Cargo.toml | 12 ++++-- .../Rust/basic-rust-application/src/lib.rs | 21 ++++++---- templates/rust-http/Cargo.toml | 2 +- 5 files changed, 48 insertions(+), 40 deletions(-) diff --git a/examples/Rust/basic-rust-application-extended/Cargo.toml b/examples/Rust/basic-rust-application-extended/Cargo.toml index 3df2162..616b3ef 100644 --- a/examples/Rust/basic-rust-application-extended/Cargo.toml +++ b/examples/Rust/basic-rust-application-extended/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "myapp" version = "0.1.0" -edition = "2021" +edition = "2024" [lib] crate-type = ["cdylib"] @@ -11,11 +11,16 @@ package = "myorg:myapp" proxy = false [dependencies] -waki = { version = "0.5.0", features = ["json"] } +wstd = { version = "0.6.3", features = ["json"] } +wstd-axum = { version = "0.6.3" } +axum = { version = "0.8.8", default-features = false, features = [ + "query", + "json", + "macros", +] } serde = { version = "1.0.216", features = ["derive"] } -serde_json = "1.0.133" # reduce wasm binary size [profile.release] lto = true -strip = "symbols" \ No newline at end of file +strip = "symbols" diff --git a/examples/Rust/basic-rust-application-extended/src/lib.rs b/examples/Rust/basic-rust-application-extended/src/lib.rs index a473d4b..28d38bc 100644 --- a/examples/Rust/basic-rust-application-extended/src/lib.rs +++ b/examples/Rust/basic-rust-application-extended/src/lib.rs @@ -1,5 +1,8 @@ +use axum::{ + Json, + routing::{Router, get}, +}; use serde::{Deserialize, Serialize}; -use waki::{handler, ErrorCode, Request, Response}; #[derive(Deserialize)] struct MyRequestBody { @@ -13,29 +16,18 @@ struct MyResponseBody { msg: Option, } -#[handler] -fn hello(req: Request) -> Result { +#[wstd_axum::http_server] +fn main() -> Router { + Router::new().route("/mypath", get(handler)) +} + +async fn handler(Json(body): Json) -> Json { let message = std::env::var("HELLO_MESSAGE").expect("HELLO_MESSAGE is not set"); - let body = match req.json::() { - Ok(b) => b, - Err(_) => { - return Response::builder() - .status_code(400) - .json(&MyResponseBody { - error: Some("Missing request body".to_string()), - name: None, - msg: None, - }) - .build(); - } - }; - Response::builder() - .status_code(200) - .json(&MyResponseBody { - error: None, - name: Some(body.name.clone()), - msg: Some(format!("{} {}", message, body.name)), - }) - .build() + MyResponseBody { + error: None, + msg: Some(format!("{message} {}", body.name)), + name: Some(body.name), + } + .into() } diff --git a/examples/Rust/basic-rust-application/Cargo.toml b/examples/Rust/basic-rust-application/Cargo.toml index 4f10a73..d881b7f 100644 --- a/examples/Rust/basic-rust-application/Cargo.toml +++ b/examples/Rust/basic-rust-application/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "myapp" version = "0.1.0" -edition = "2021" +edition = "2024" [lib] crate-type = ["cdylib"] @@ -11,9 +11,15 @@ package = "myorg:myapp" proxy = false [dependencies] -waki = { version = "0.5.0", features = ["json"] } +wstd = { version = "0.6.3", features = ["json"] } +wstd-axum = { version = "0.6.3" } +axum = { version = "0.8.8", default-features = false, features = [ + "query", + "json", + "macros", +] } # reduce wasm binary size [profile.release] lto = true -strip = "symbols" \ No newline at end of file +strip = "symbols" diff --git a/examples/Rust/basic-rust-application/src/lib.rs b/examples/Rust/basic-rust-application/src/lib.rs index a8c53e7..1e7ef3c 100644 --- a/examples/Rust/basic-rust-application/src/lib.rs +++ b/examples/Rust/basic-rust-application/src/lib.rs @@ -1,9 +1,14 @@ -use waki::{handler, ErrorCode, Request, Response}; +use axum::{ + http::StatusCode, + response::IntoResponse, + routing::{Router, get}, +}; -#[handler] -fn hello(req: Request) -> Result { - Response::builder() - .status_code(200) - .body("Hello World") - .build() -} \ No newline at end of file +#[wstd_axum::http_server] +fn main() -> Router { + Router::new().route("/myapp", get(handler)) +} + +async fn handler() -> impl IntoResponse { + (StatusCode::OK, "Hello, World!").into_response() +} diff --git a/templates/rust-http/Cargo.toml b/templates/rust-http/Cargo.toml index e13d655..cb14491 100644 --- a/templates/rust-http/Cargo.toml +++ b/templates/rust-http/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "{{name}}" version = "0.1.0" -edition = "2021" +edition = "2024" [lib] crate-type = ["cdylib"] From 215d69127bd60de049e0a18cb4c303a0206d66ac Mon Sep 17 00:00:00 2001 From: Silesmo Date: Sat, 27 Dec 2025 16:13:32 +0100 Subject: [PATCH 3/4] Converts last rust example to use axum as well --- .../Rust/multi-path-application/Cargo.toml | 15 ++- .../Rust/multi-path-application/src/lib.rs | 121 +++++++++--------- 2 files changed, 67 insertions(+), 69 deletions(-) diff --git a/examples/Rust/multi-path-application/Cargo.toml b/examples/Rust/multi-path-application/Cargo.toml index a697bf0..dec7d1e 100644 --- a/examples/Rust/multi-path-application/Cargo.toml +++ b/examples/Rust/multi-path-application/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "multi-path-app" version = "0.1.0" -edition = "2021" +edition = "2024" [lib] crate-type = ["cdylib"] @@ -11,11 +11,16 @@ package = "myorg:multi-path-app" proxy = false [dependencies] -waki = { version = "0.5.0", features = ["json"] } -serde = { version = "1.0.216", features = ["derive"] } -serde_json = "1.0.133" +wstd = { version = "0.6.3", features = ["json"] } +wstd-axum = { version = "0.6.3" } +axum = { version = "0.8.8", default-features = false, features = [ + "query", + "json", + "macros", +] } +serde = { version = "1.0.228", features = ["derive"] } # reduce wasm binary size [profile.release] lto = true -strip = "symbols" \ No newline at end of file +strip = "symbols" diff --git a/examples/Rust/multi-path-application/src/lib.rs b/examples/Rust/multi-path-application/src/lib.rs index 97d9b85..d03336a 100644 --- a/examples/Rust/multi-path-application/src/lib.rs +++ b/examples/Rust/multi-path-application/src/lib.rs @@ -1,9 +1,21 @@ +use axum::{ + Json, + extract::Query, + http::{Response, StatusCode, header::CONTENT_TYPE}, + response::IntoResponse, + routing::{Router, get, post}, +}; use serde::{Deserialize, Serialize}; use std::{collections::HashMap, time::Duration}; -use waki::{handler, header::ACCEPT, header::CONTENT_TYPE, ErrorCode, Method, Request, Response}; +use wstd::http::Body; -pub const GET_QUERY_PATH: &str = "/multi-path-app/v1/get"; -pub const POST_BODY_PATH: &str = "/multi-path-app/v1/post"; +#[wstd_axum::http_server] +fn main() -> Router { + Router::new() + .route("/multi-path-app/v1/get", get(get_body_handler)) + .route("/multi-path-app/v1/post", post(post_body_handler)) + .fallback(async || (StatusCode::NOT_FOUND, "NOT FOUND!").into_response()) +} #[derive(Debug, Clone, Serialize, Deserialize)] pub struct SearchQuery { @@ -11,78 +23,59 @@ pub struct SearchQuery { result_limit: i32, } -#[handler] -fn hello(req: Request) -> Result { - let path = &req.path().to_string(); - let query = &req.query(); - let method = &req.method(); - - //Here we have implemented a really simple router based on method and path in the request. - match (method, path.as_str()) { - (Method::Post, POST_BODY_PATH) => { - let body = match &req.body() { - Ok(body_value) => body_value.clone(), - Err(err) => { - return bad_request(format!("Invalid body request sent, {err:#?}").as_str()) - } - }; - post_body_handler(&body) +#[axum::debug_handler] +async fn get_body_handler(Query(params): Query>) -> Response { + let _search: String = match params.get("search") { + Some(search) => search.to_string(), + None => { + return Response::builder() + .status(StatusCode::BAD_REQUEST) + .body("We are missing the required query param called Search".to_string()) + .unwrap(); } - (Method::Get, GET_QUERY_PATH) => get_body_handler(query), - _ => not_found(), - } -} + }; -pub fn not_found() -> Result { - Response::builder() - .status_code(404) - .body("NOT FOUND!".to_string()) - .build() -} + let mut client = wstd::http::Client::new(); + client.set_connect_timeout(Duration::from_secs(1)); -/// Bad request response -pub fn bad_request(msg: &str) -> Result { - Response::builder() - .status_code(400) - .body(msg.to_string()) - .build() -} + let request = wstd::http::Request::get("https://dummyjson.com/recipes/1") + .header("Content-Type", "application/json") + .header("Accept", "*") + .body(Body::empty()) + .unwrap(); -pub fn get_body_handler(query: &HashMap) -> Result { - let _search: String = match query.get("search") { - Some(search) => search.to_string(), - None => return bad_request("We are missing the required query param called Search"), + let result = match client.send(request).await { + Ok(result) => result, + Err(e) => { + return Response::builder() + .status(StatusCode::INTERNAL_SERVER_ERROR) + .body(format!("Failed to send request with error: {e}")) + .unwrap(); + } }; - let client = waki::Client::new(); - let result = client - .get("https://dummyjson.com/recipes/1") - .headers([("Content-Type", "application/json"), ("Accept", "*")]) - .connect_timeout(Duration::from_secs(1)) - .send() - .and_then(|res| res.body().map_err(|e| e.into())) - .and_then(|body| String::from_utf8(body).map_err(|e| e.into())); - let body = match result { - Ok(text_body) => text_body, - Err(err) => format!("We failed to retrieve the response: {err}"), + let body = match result.into_body().str_contents().await { + Ok(text_body) => text_body.to_string(), + Err(e) => { + return Response::builder() + .status(StatusCode::INTERNAL_SERVER_ERROR) + .body(format!("We failed to retrieve the response: {e}")) + .unwrap(); + } }; Response::builder() - .status_code(200) - .headers([(CONTENT_TYPE, "application/json")]) + .status(StatusCode::OK) + .header(CONTENT_TYPE, "application/json") .body(body) - .build() + .unwrap() } /// Post body response -pub fn post_body_handler(body: &Vec) -> Result { - match serde_json::from_slice::(&body) { - Ok(value) => Response::builder() - .status_code(200) - .body(format!( - "We received the following search query: {value:#?}" - )) - .build(), - Err(err) => bad_request(&format!("Failed deserialize the json body: {err}")), - } +async fn post_body_handler(Json(body): Json) -> impl IntoResponse { + ( + StatusCode::OK, + format!("We received the following search query: {body:#?}"), + ) + .into_response() } From 1bd3b0b22d8a5aaa7f75c3ae0a6e46efa864b39e Mon Sep 17 00:00:00 2001 From: Silesmo Date: Sat, 27 Dec 2025 16:13:58 +0100 Subject: [PATCH 4/4] Updated error message in example --- examples/Rust/multi-path-application/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/Rust/multi-path-application/src/lib.rs b/examples/Rust/multi-path-application/src/lib.rs index d03336a..8376273 100644 --- a/examples/Rust/multi-path-application/src/lib.rs +++ b/examples/Rust/multi-path-application/src/lib.rs @@ -59,7 +59,7 @@ async fn get_body_handler(Query(params): Query>) -> Resp Err(e) => { return Response::builder() .status(StatusCode::INTERNAL_SERVER_ERROR) - .body(format!("We failed to retrieve the response: {e}")) + .body(format!("Failed to retrieve the response: {e}")) .unwrap(); } };