Build serverless applications in Rust by defining an async handler function that accepts a Request and returns a Response, then compile it for the wasm32-unknown-unknown target. Use the axum crate to define your routing logic and futures_executor to block on the response if running in a synchronous context.
use axum::{response::Html, routing::get, Router};
use http::Request;
use tower_service::Service;
async fn index() -> Html<&'static str> {
Html("<h1>Hello, World!</h1>")
}
async fn app(request: Request<String>) -> Response {
let router = Router::new().route("/api/", get(index));
router.call(request).await.unwrap()
}
Compile your project for WebAssembly with the following command:
cargo build --target wasm32-unknown-unknown --release