Skip to content

Commit

Permalink
fix ssr_router example
Browse files Browse the repository at this point in the history
  • Loading branch information
ranile committed Aug 3, 2024
1 parent 4c1cbe7 commit 13b36b3
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 25 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions examples/ssr_router/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ yew = { path = "../../packages/yew" }
function_router = { path = "../function_router" }
log = "0.4"
futures = { version = "0.3", features = ["std"], default-features = false }
hyper-util = "0.1.6"

[target.'cfg(target_arch = "wasm32")'.dependencies]
wasm-bindgen-futures = "0.4"
Expand Down
84 changes: 59 additions & 25 deletions examples/ssr_router/src/bin/ssr_router_server.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
use std::collections::HashMap;
use std::convert::Infallible;
use std::future::Future;
use std::net::SocketAddr;
use std::path::PathBuf;

use axum::body::StreamBody;
use axum::error_handling::HandleError;
use axum::extract::{Query, State};
use axum::body::Body;
use axum::extract::{Query, Request, State};
use axum::handler::HandlerWithoutStateExt;
use axum::http::{StatusCode, Uri};
use axum::http::Uri;
use axum::response::IntoResponse;
use axum::routing::get;
use axum::Router;
use clap::Parser;
use function_router::{ServerApp, ServerAppProps};
use futures::stream::{self, StreamExt};
use hyper::server::Server;
use tower::ServiceExt;
use hyper::body::Incoming;
use hyper_util::rt::TokioIo;
use hyper_util::server;
use tokio::net::TcpListener;
use tower::Service;
use tower_http::services::ServeDir;
use yew::platform::Runtime;

Expand Down Expand Up @@ -44,7 +47,7 @@ async fn render(
queries,
});

StreamBody::new(
Body::from_stream(
stream::once(async move { index_html_before })
.chain(renderer.render_stream())
.chain(stream::once(async move { index_html_after }))
Expand Down Expand Up @@ -75,7 +78,7 @@ where
}

#[tokio::main]
async fn main() {
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let exec = Executor::default();

env_logger::init();
Expand All @@ -92,30 +95,61 @@ async fn main() {

let index_html_after = index_html_after.to_owned();

let handle_error = |e| async move {
(
StatusCode::INTERNAL_SERVER_ERROR,
format!("error occurred: {e}"),
)
};

let app = Router::new().fallback_service(HandleError::new(
let app = Router::new().fallback_service(
ServeDir::new(opts.dir)
.append_index_html_on_directories(false)
.fallback(
get(render)
.with_state((index_html_before.clone(), index_html_after.clone()))
.into_service()
.map_err(|err| -> std::io::Error { match err {} }),
.into_service(),
),
handle_error,
));
);

let addr: SocketAddr = ([127, 0, 0, 1], 8080).into();

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

Server::bind(&"127.0.0.1:8080".parse().unwrap())
.executor(exec)
.serve(app.into_make_service())
.await
.unwrap();
let listener = TcpListener::bind(addr).await?;

// Continuously accept new connections.
loop {
// In this example we discard the remote address. See `fn serve_with_connect_info` for how
// to expose that.
let (socket, _remote_addr) = listener.accept().await.unwrap();

// We don't need to call `poll_ready` because `Router` is always ready.
let tower_service = app.clone();

let exec = exec.clone();
// Spawn a task to handle the connection. That way we can handle multiple connections
// concurrently.
tokio::spawn(async move {
// Hyper has its own `AsyncRead` and `AsyncWrite` traits and doesn't use tokio.
// `TokioIo` converts between them.
let socket = TokioIo::new(socket);

// Hyper also has its own `Service` trait and doesn't use tower. We can use
// `hyper::service::service_fn` to create a hyper `Service` that calls our app through
// `tower::Service::call`.
let hyper_service = hyper::service::service_fn(move |request: Request<Incoming>| {
// We have to clone `tower_service` because hyper's `Service` uses `&self` whereas
// tower's `Service` requires `&mut self`.
//
// We don't need to call `poll_ready` since `Router` is always ready.
tower_service.clone().call(request)
});

// `server::conn::auto::Builder` supports both http1 and http2.
//
// `TokioExecutor` tells hyper to use `tokio::spawn` to spawn tasks.
if let Err(err) = server::conn::auto::Builder::new(exec)
// `serve_connection_with_upgrades` is required for websockets. If you don't need
// that you can use `serve_connection` instead.
.serve_connection_with_upgrades(socket, hyper_service)
.await
{
eprintln!("failed to serve connection: {err:#}");
}
});
}
}

0 comments on commit 13b36b3

Please sign in to comment.