axum-cloudflare-adapter

Crates.io

An adapter to easily allow an Axum server to be run within a Cloudflare worker.

Usage

```rust use worker::*; use axum::{ response::{Html}, routing::get, Router as AxumRouter, extract::State, }; use axumcloudflareadapter::{toaxumrequest, toworkerresponse, workerroutecompat, EnvWrapper}; use tower_service::Service; use std::ops::Deref;

[derive(Clone)]

pub struct AxumState { pub env_wrapper: EnvWrapper, }

[workerroutecompat]

async fn index(State(state): State) -> Html<&'static str> { let env: &Env = state.envwrapper.env.deref(); let workerrsversion: Var = env.var("WORKERSRSVERSION").unwrap(); consolelog!("WORKERSRSVERSION: {}", workerrsversion.to_string()); Html("

Hello from Axum!

") }

[event(fetch)]

pub async fn main(req: Request, env: Env, _ctx: worker::Context) -> Result { let mut _router: AxumRouter = AxumRouter::new() .route("/", get(index)) .withstate(AxumState { envwrapper: EnvWrapper::new(env), }); let axumrequest = toaxumrequest(req).await.unwrap(); let axumresponse = _router.call(axumrequest).await.unwrap(); let response = toworkerresponse(axum_response).await.unwrap(); Ok(response) } ```

Running tests

cd adapter && wasm-pack test --firefox --headless

Building

cd adapter && cargo build --target wasm32-unknown-unknown

Example

The /example directory contains a Cloudflare worker running an Axum sever