Skip to content

Commit

Permalink
use axum (#2669)
Browse files Browse the repository at this point in the history
  • Loading branch information
ranile committed May 8, 2022
1 parent 333af62 commit 3760c5f
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 20 deletions.
4 changes: 3 additions & 1 deletion examples/ssr_router/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ wasm-logger = "0.2"

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
tokio = { version = "1.15.0", features = ["full"] }
warp = "0.3"
axum = "0.5"
tower = { version = "0.4", features = ["make"] }
tower-http = { version = "0.3", features = ["fs"] }
env_logger = "0.9"
num_cpus = "1.13"
tokio-util = { version = "0.7", features = ["rt"] }
Expand Down
61 changes: 42 additions & 19 deletions examples/ssr_router/src/bin/ssr_router_server.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
use std::collections::HashMap;
use std::path::PathBuf;

use axum::body::Body;
use axum::error_handling::HandleError;
use axum::extract::Query;
use axum::handler::Handler;
use axum::http::{Request, StatusCode};
use axum::response::Html;
use axum::routing::get;
use axum::{Extension, Router};
use clap::Parser;
use function_router::{ServerApp, ServerAppProps};
use once_cell::sync::Lazy;
use tokio_util::task::LocalPoolHandle;
use warp::Filter;
use tower::ServiceExt;
use tower_http::services::ServeDir;

// We spawn a local pool that is as big as the number of cpu threads.
static LOCAL_POOL: Lazy<LocalPoolHandle> = Lazy::new(|| LocalPoolHandle::new(num_cpus::get()));
Expand All @@ -14,12 +23,16 @@ static LOCAL_POOL: Lazy<LocalPoolHandle> = Lazy::new(|| LocalPoolHandle::new(num
#[derive(Parser, Debug)]
struct Opt {
/// the "dist" created by trunk directory to be served for hydration.
#[structopt(short, long, parse(from_os_str))]
#[clap(short, long, parse(from_os_str))]
dir: PathBuf,
}

async fn render(index_html_s: &str, url: &str, queries: HashMap<String, String>) -> String {
let url = url.to_string();
async fn render(
Extension(index_html_s): Extension<String>,
url: Request<Body>,
Query(queries): Query<HashMap<String, String>>,
) -> Html<String> {
let url = url.uri().to_string();

let content = LOCAL_POOL
.spawn_pinned(move || async move {
Expand All @@ -37,7 +50,7 @@ async fn render(index_html_s: &str, url: &str, queries: HashMap<String, String>)

// Good enough for an example, but developers should avoid the replace and extra allocation
// here in an actual app.
index_html_s.replace("<body>", &format!("<body>{}", content))
Html(index_html_s.replace("<body>", &format!("<body>{}", content)))
}

#[tokio::main]
Expand All @@ -50,23 +63,33 @@ async fn main() {
.await
.expect("failed to read index.html");

let render = move |s: warp::filters::path::FullPath, queries: HashMap<String, String>| {
let index_html_s = index_html_s.clone();

async move { warp::reply::html(render(&index_html_s, s.as_str(), queries).await) }
let handle_error = |e| async move {
(
StatusCode::INTERNAL_SERVER_ERROR,
format!("error occurred: {}", e),
)
};

let html = warp::path::end().and(
warp::path::full()
.and(warp::filters::query::query())
.then(render.clone()),
);

let routes = html.or(warp::fs::dir(opts.dir)).or(warp::path::full()
.and(warp::filters::query::query())
.then(render));
let app = Router::new()
.route("/api/test", get(|| async move { "Hello World" }))
// needed because https://github.com/tower-rs/tower-http/issues/262
.route("/", get(render))
.fallback(HandleError::new(
ServeDir::new(opts.dir)
.append_index_html_on_directories(false)
.fallback(
render
.layer(Extension(index_html_s))
.into_service()
.map_err(|err| -> std::io::Error { match err {} }),
),
handle_error,
));

println!("You can view the website at: http://localhost:8080/");

warp::serve(routes).run(([127, 0, 0, 1], 8080)).await;
axum::Server::bind(&"0.0.0.0:8080".parse().unwrap())
.serve(app.into_make_service())
.await
.unwrap();
}

1 comment on commit 3760c5f

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yew master branch benchmarks (Lower is better)

Benchmark suite Current: 3760c5f Previous: 333af62 Ratio
yew-struct-keyed 01_run1k 159.72500000000002 185.6265 0.86
yew-struct-keyed 02_replace1k 163.327 206.2025 0.79
yew-struct-keyed 03_update10th1k_x16 249.5885 483.4295 0.52
yew-struct-keyed 04_select1k 41.0075 100.1775 0.41
yew-struct-keyed 05_swap1k 65.4005 130.89 0.50
yew-struct-keyed 06_remove-one-1k 23.472 43.9925 0.53
yew-struct-keyed 07_create10k 2650.714 3189.9979999999996 0.83
yew-struct-keyed 08_create1k-after1k_x2 353.8735 459.921 0.77
yew-struct-keyed 09_clear1k_x8 149.8995 202.6665 0.74
yew-struct-keyed 21_ready-memory 1.459808349609375 1.459808349609375 1
yew-struct-keyed 22_run-memory 1.660259246826172 1.664287567138672 1.00
yew-struct-keyed 23_update5-memory 1.699298858642578 1.7092056274414062 0.99
yew-struct-keyed 24_run5-memory 1.7175369262695312 1.7175369262695312 1
yew-struct-keyed 25_run-clear-memory 1.4279136657714844 1.3334579467773438 1.07
yew-struct-keyed 31_startup-ci 1731.044 1731.264 1.00
yew-struct-keyed 32_startup-bt 23.427999999999997 35.696 0.66
yew-struct-keyed 33_startup-mainthreadcost 202.692 259.4920000000001 0.78
yew-struct-keyed 34_startup-totalbytes 328.7373046875 328.7373046875 1

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.