Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tokio feature & make tokio optional for WASM support (#1382)
* add server feature and make tokio and hyper/server and tcp optional * address review comments * don't mention any specific runtimes in the example * sort deps * add `tokio` feature when adding `ws` * don't always pull in tower feature that pulls in tokio io stuff * remove usage of `tokio_cr` * changelog * depend on tokio version that supports wasm * don't make it sound like tokio doesn't support wasm * call out new default feature Co-authored-by: Fisher Darling <fdarlingco@gmail.com> Co-authored-by: David Pedersen <david.pdrsn@gmail.com>
- Loading branch information
1 parent
83ba8c3
commit 31638a2
Showing
14 changed files
with
132 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
[package] | ||
name = "example-simple-router-wasm" | ||
version = "0.1.0" | ||
edition = "2018" | ||
publish = false | ||
|
||
[dependencies] | ||
# `default-features = false` to not depend on tokio features which don't support wasm | ||
# you can still pull in tokio manually and only add features that tokio supports for wasm | ||
axum = { path = "../../axum", default-features = false } | ||
futures-executor = "0.3.21" | ||
http = "0.2.7" | ||
tower-service = "0.3.1" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
//! Run with | ||
//! | ||
//! ```not_rust | ||
//! cd examples && cargo run -p example-simple-router-wasm | ||
//! ``` | ||
//! | ||
//! This example shows what using axum in a wasm context might look like. This example should | ||
//! always compile with `--target wasm32-unknown-unknown`. | ||
//! | ||
//! [`mio`](https://docs.rs/mio/latest/mio/index.html), tokio's IO layer, does not support the | ||
//! `wasm32-unknown-unknown` target which is why this crate requires `default-features = false` | ||
//! for axum. | ||
//! | ||
//! Most serverless runtimes expect an exported function that takes in a single request and returns | ||
//! a single response, much like axum's `Handler` trait. In this example, the handler function is | ||
//! `app` with `main` acting as the serverless runtime which originally receives the request and | ||
//! calls the app function. | ||
//! | ||
//! We can use axum's routing, extractors, tower services, and everything else to implement | ||
//! our serverless function, even though we are running axum in a wasm context. | ||
|
||
use axum::{ | ||
response::{Html, Response}, | ||
routing::get, | ||
Router, | ||
}; | ||
use futures_executor::block_on; | ||
use http::Request; | ||
use tower_service::Service; | ||
|
||
fn main() { | ||
let request: Request<String> = Request::builder() | ||
.uri("https://serverless.example/api/") | ||
.body("Some Body Data".into()) | ||
.unwrap(); | ||
|
||
let response: Response = block_on(app(request)); | ||
assert_eq!(200, response.status()); | ||
} | ||
|
||
#[allow(clippy::let_and_return)] | ||
async fn app(request: Request<String>) -> Response { | ||
let mut router = Router::new().route("/api/", get(index)).into_service(); | ||
|
||
let response = router.call(request).await.unwrap(); | ||
response | ||
} | ||
|
||
async fn index() -> Html<&'static str> { | ||
Html("<h1>Hello, World!</h1>") | ||
} |