Skip to content

Commit

Permalink
feat(viz): add serve function
Browse files Browse the repository at this point in the history
  • Loading branch information
fundon committed Oct 5, 2023
1 parent eeb9900 commit 9056447
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 170 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ http = "0.2"
http-body = "=1.0.0-rc.2"
http-body-util = "=0.1.0-rc.3"
hyper = { version = "=1.0.0-rc.4", features = ["server"] }
hyper-util = { git = "https://github.com/hyperium/hyper-util", rev = "63e84bf", features = ["auto"] }

futures-util = "0.3"
tokio = { version = "1.32", features = ["net"] }
Expand Down
9 changes: 3 additions & 6 deletions examples/hello-world/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use std::{net::SocketAddr, sync::Arc};
use tokio::net::TcpListener;
use viz::{server::conn::http1, Io, Request, Responder, Result, Router, Tree};
use viz::{serve, Request, Result, Router, Tree};

async fn index(_: Request) -> Result<&'static str> {
Ok("Hello, World!")
Expand All @@ -13,7 +13,7 @@ async fn index(_: Request) -> Result<&'static str> {
async fn main() -> Result<()> {
let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
let listener = TcpListener::bind(addr).await?;
println!("listening on {addr}");
println!("listening on http://{addr}");

let app = Router::new().get("/", index);
let tree = Arc::new(Tree::from(app));
Expand All @@ -22,10 +22,7 @@ async fn main() -> Result<()> {
let (stream, addr) = listener.accept().await?;
let tree = tree.clone();
tokio::task::spawn(async move {
if let Err(err) = http1::Builder::new()
.serve_connection(Io::new(stream), Responder::new(tree, Some(addr)))
.await
{
if let Err(err) = serve(stream, Some(addr), tree).await {
eprintln!("Error while serving HTTP connection: {err}");
}
});
Expand Down
1 change: 1 addition & 0 deletions viz-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ http-body.workspace = true
http-body-util.workspace = true

hyper.workspace = true
hyper-util.workspace = true

mime.workspace = true
thiserror.workspace = true
Expand Down
157 changes: 0 additions & 157 deletions viz-core/src/io.rs

This file was deleted.

5 changes: 2 additions & 3 deletions viz-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#![doc(html_logo_url = "https://viz.rs/logo.svg")]
#![doc(html_favicon_url = "https://viz.rs/logo.svg")]
#![allow(clippy::module_name_repetitions)]
// #![forbid(unsafe_code)]
#![forbid(unsafe_code)]
#![warn(
missing_debug_implementations,
missing_docs,
Expand Down Expand Up @@ -43,15 +43,13 @@ mod body;
mod error;
mod from_request;
mod into_response;
mod io;
mod request;
mod response;

pub use body::{IncomingBody, OutgoingBody};
pub use error::Error;
pub use from_request::FromRequest;
pub use into_response::IntoResponse;
pub use io::Io;
pub use request::RequestExt;
pub use response::ResponseExt;

Expand All @@ -61,6 +59,7 @@ pub use bytes::{Bytes, BytesMut};
pub use headers;
pub use http::{header, Method, StatusCode};
pub use hyper::body::{Body, Incoming};
pub use hyper_util::rt::TokioIo as Io;
pub use std::future::Future;
pub use thiserror::Error as ThisError;

Expand Down
1 change: 1 addition & 0 deletions viz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ viz-handlers = { workspace = true, optional = true }
viz-macros = { workspace = true, optional = true }

hyper.workspace = true
hyper-util.workspace = true
tokio.workspace = true

futures-util = { workspace = true, optional = true }
Expand Down
11 changes: 7 additions & 4 deletions viz/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@
#![doc(html_logo_url = "https://viz.rs/logo.svg")]
#![doc(html_favicon_url = "https://viz.rs/logo.svg")]
// #![forbid(unsafe_code)]
#![forbid(unsafe_code)]
#![warn(
missing_debug_implementations,
missing_docs,
Expand All @@ -543,6 +543,9 @@ mod responder;
#[cfg(any(feature = "http1", feature = "http2"))]
pub use responder::Responder;

mod serve;
pub use serve::serve;

/// TLS
pub mod tls;
pub use viz_core::*;
Expand All @@ -553,10 +556,10 @@ pub use viz_router::*;
#[doc(inline)]
pub use viz_handlers as handlers;

#[cfg(any(feature = "http1", feature = "http2"))]
pub use hyper::server;

#[cfg(feature = "macros")]
#[cfg_attr(docsrs, doc(cfg(feature = "macros")))]
#[doc(inline)]
pub use viz_macros::handler;

#[cfg(any(feature = "http1", feature = "http2"))]
pub use hyper::server;
24 changes: 24 additions & 0 deletions viz/src/serve.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use std::{net::SocketAddr, sync::Arc};

use hyper_util::{rt::TokioExecutor, server::conn::auto::Builder};
use tokio::io::{AsyncRead, AsyncWrite};

use viz_core::{Io, Result};
use viz_router::Tree;

use crate::Responder;

/// Serve the connections.
///
/// # Errors
///
/// Will return `Err` if the connection does not be served.
pub async fn serve<I>(stream: I, addr: Option<SocketAddr>, tree: Arc<Tree>) -> Result<()>
where
I: AsyncRead + AsyncWrite + Unpin + Send + 'static,
{
Builder::new(TokioExecutor::new())
.serve_connection_with_upgrades(Io::new(stream).into_inner(), Responder::new(tree, addr))
.await
.map_err(Into::into)
}

0 comments on commit 9056447

Please sign in to comment.