Skip to content

Commit d585681

Browse files
committed
docs, example that doesnt use macro
1 parent 2ed30f5 commit d585681

File tree

3 files changed

+73
-2
lines changed

3 files changed

+73
-2
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//! Run with
2+
//!
3+
//! ```sh
4+
//! cargo build -p wstd-axum --examples --target wasm32-wasip2
5+
//! wasmtime serve -Scli target/wasm32-wasip2/debug/examples/hello-world-nomacro.wasm
6+
//! ```
7+
8+
use axum::{Router, response::Html, routing::get};
9+
use wstd::http::{Body, Error, Request, Response};
10+
11+
#[wstd::http_server]
12+
async fn main(request: Request<Body>) -> Result<Response<Body>, Error> {
13+
let service = Router::new().route("/", get(handler));
14+
wstd_axum::serve(request, service).await
15+
}
16+
17+
async fn handler() -> Html<&'static str> {
18+
Html("<h1>Hello, World!</h1>")
19+
}

axum/src/lib.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,44 @@
1+
//! Support for the [`axum`] web server framework in wasi-http components, via
2+
//! [`wstd`].
3+
//!
4+
//! This crate is a pretty thin wrapper on [`wstd`] that allows users to
5+
//! use the [`axum`] crate on top of wstd's http support. This means that
6+
//! axum services can run anywhere the [wasi-http proxy world] is supported,
7+
//! e.g. in [`wasmtime serve`].
8+
//!
9+
//! Users of this crate should depend on `axum` with `default-features =
10+
//! false`, and opt in to any features that they require (e.g. form, json,
11+
//! matched-path, original-uri, query, tower-log, tracing). The axum crate
12+
//! features that require `hyper` or `tokio` are NOT supported (e.g. http1,
13+
//! http2, ws), because unlike in native applications, wasi-http components
14+
//! have an http implementation provided as imported interfaces (i.e.
15+
//! implemented the Wasm host), and do not use raw sockets inside of this
16+
//! program.
17+
//!
18+
//! # Examples
19+
//!
20+
//! The simplest use is via the `wstd_axum::http_server` proc macro.
21+
//! This macro can be applied to a sync or `async` `fn main` which returns
22+
//! an impl of the `tower_service::Service` trait, typically an
23+
//! `axum::Router`:
24+
//!
25+
//! ```rust,no_run
26+
#![doc = include_str!("../examples/hello_world.rs")]
27+
//! ```
28+
//!
29+
//! If users desire, they can instead use a `wstd::http_server` entry point
30+
//! and then use `wstd_axum::serve` directly. The following is equivelant
31+
//! to the above example:
32+
//!
33+
//! ```rust,no_run
34+
#![doc = include_str!("../examples/hello_world_nomacro.rs")]
35+
//! ```
36+
//!
37+
//! [`axum`]: https://docs.rs/axum/latest/axum/
38+
//! [`wstd`]: https://docs.rs/wstd/latest/wstd/
39+
//! [wasi-http proxy world]: https://github.com/WebAssembly/wasi-http
40+
//! [`wasmtime serve`]: https://wasmtime.dev/
41+
142
use axum::extract::Request;
243
use axum::response::Response;
344
use std::convert::Infallible;

test-programs/tests/axum_hello_world.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,18 @@ impl Drop for DontOrphan {
1414
}
1515

1616
#[test_log::test]
17-
fn http_server() -> Result<()> {
17+
fn hello_world() -> Result<()> {
18+
run(test_programs::axum::HELLO_WORLD)
19+
}
20+
21+
#[test_log::test]
22+
fn hello_world_nomacro() -> Result<()> {
23+
run(test_programs::axum::HELLO_WORLD_NOMACRO)
24+
}
25+
26+
// The hello_world.rs and hello_world_nomacro.rs are identical in
27+
// functionality
28+
fn run(guest: &str) -> Result<()> {
1829
// Run wasmtime serve.
1930
// Enable -Scli because we currently don't have a way to build with the
2031
// proxy adapter, so we build with the default adapter.
@@ -23,7 +34,7 @@ fn http_server() -> Result<()> {
2334
.arg("serve")
2435
.arg("-Scli")
2536
.arg("--addr=127.0.0.1:8081")
26-
.arg(test_programs::axum::HELLO_WORLD)
37+
.arg(guest)
2738
.spawn()?,
2839
);
2940

0 commit comments

Comments
 (0)