axum-browser-adapter

Crates.io npm

A collection of tools to make integrating Axum with the browser easier

Documentation

Example

```rust use axumbrowseradapter::{ wasmrequesttoaxumrequest, axumresponsetowasmresponse, wasmcompat, WasmRequest, WasmResponse }; use axum::Router; use axum::routing::get; use wasmbindgen::prelude::wasmbindgen; use towerservice::Service;

[wasm_compat]

pub async fn index() -> &'static str { "Hello World" }

[wasm_bindgen]

pub async fn wasmapp(wasmrequest: WasmRequest) -> WasmResponse { let mut router: Router = Router::new().route("/", get(index));

let request = wasmrequesttoaxumrequest(&wasm_request).unwrap();

let axum_response = router.call(request).await.unwrap();

let response = axumresponsetowasmresponse(axum_response).await.unwrap();

response } ``` Integrating w/ the browser

```html

```

Service worker ```js importScripts("/node_modules/axum-browser-adapter/index.js");

// load the WASM app

self.addEventListener('fetch', (event) => { event.respondWith((async () => { const {wasmapp, WasmRequest} = wasmbindgen; const request = event.request; const wasmRequest = await requestToWasmRequest(request, WasmRequest);

    const wasmResponse = await wasm_app(wasmRequest);

    return wasmResponseToJsResponse(wasmResponse);
})());

});

```

Running the Example

An example lives in /example 1. Compile the rust app to WASM: . ./build.sh 2. Serve index.html via basic-http-server or your favorite web server