-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat(examples): tower * fix(clippy): allow missing panics doc * chore: renaming * chore: tower example link * chore: supports tower
- Loading branch information
Showing
14 changed files
with
269 additions
and
45 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
[package] | ||
name = "tower-example" | ||
version = "0.1.0" | ||
edition.workspace = true | ||
publish = false | ||
|
||
[dependencies] | ||
viz.workspace = true | ||
viz-tower.workspace = true | ||
|
||
tokio = { workspace = true, features = ["rt-multi-thread", "macros"] } | ||
tracing.workspace = true | ||
tracing-subscriber = { workspace = true, features = ["env-filter"] } | ||
tower.workspace = true | ||
tower-http = { workspace = true, features = ["full"] } | ||
|
||
[lints] | ||
workspace = true |
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,75 @@ | ||
//! Viz + Tower services | ||
use std::{net::SocketAddr, sync::Arc}; | ||
use tokio::net::TcpListener; | ||
use tower::{ | ||
service_fn, | ||
util::{MapErrLayer, MapRequestLayer, MapResponseLayer}, | ||
ServiceBuilder, | ||
}; | ||
use tower_http::{ | ||
limit::RequestBodyLimitLayer, | ||
request_id::{MakeRequestUuid, SetRequestIdLayer}, | ||
trace::TraceLayer, | ||
}; | ||
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt}; | ||
use viz::{serve, Body, Error, IntoResponse, Request, Response, Result, Router, Tree}; | ||
use viz_tower::{Layered, ServiceHandler}; | ||
|
||
async fn index(_: Request) -> Result<Response> { | ||
Ok("Hello, World!".into_response()) | ||
} | ||
|
||
async fn about(_: Request) -> Result<&'static str> { | ||
Ok("About me!") | ||
} | ||
|
||
async fn any(_: Request) -> Result<String> { | ||
Ok("std::any::Any".to_string()) | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<()> { | ||
tracing_subscriber::registry() | ||
.with( | ||
tracing_subscriber::EnvFilter::try_from_default_env() | ||
.unwrap_or_else(|_| "tower-example=debug,tower_http=debug".into()), | ||
) | ||
.with(tracing_subscriber::fmt::layer()) | ||
.init(); | ||
|
||
let index_svc = ServiceBuilder::new() | ||
.layer(MapRequestLayer::new(|req: Request<_>| req.map(Body::wrap))) | ||
.service(service_fn(index)); | ||
let index_handler = ServiceHandler::new(index_svc); | ||
|
||
let any_svc = ServiceBuilder::new() | ||
.layer(MapResponseLayer::new(IntoResponse::into_response)) | ||
.service_fn(any); | ||
let any_handler = ServiceHandler::new(any_svc); | ||
|
||
let layer = ServiceBuilder::new() | ||
.layer(TraceLayer::new_for_http()) | ||
.layer(RequestBodyLimitLayer::new(1024)) | ||
.layer(SetRequestIdLayer::x_request_id(MakeRequestUuid)) | ||
.layer(MapErrLayer::new(Error::from)) | ||
.layer(MapResponseLayer::new(IntoResponse::into_response)) | ||
.layer(MapRequestLayer::new(|req: Request<_>| req.map(Body::wrap))); | ||
|
||
let app = Router::new() | ||
.get("/", index_handler) | ||
.get("/about", about) | ||
.any("/*", any_handler) | ||
.with(Layered::new(layer)); | ||
let tree = Arc::new(Tree::from(app)); | ||
|
||
let addr = SocketAddr::from(([127, 0, 0, 1], 3000)); | ||
let listener = TcpListener::bind(addr).await?; | ||
println!("listening on http://{addr}"); | ||
|
||
loop { | ||
let (stream, addr) = listener.accept().await?; | ||
let tree = tree.clone(); | ||
tokio::task::spawn(serve(stream, tree, Some(addr))); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,46 +1,38 @@ | ||
use http_body_util::BodyExt; | ||
use hyper::service::Service; | ||
|
||
use crate::{async_trait, Bytes, Error, Handler, HttpBody, Request, Response, Result}; | ||
use crate::{ | ||
async_trait, Body, BoxError, Bytes, Error, Handler, HttpBody, Request, Response, Result, | ||
}; | ||
|
||
/// Converts a hyper [`Service`] to a viz [`Handler`]. | ||
#[derive(Debug, Clone)] | ||
pub struct ServiceHandler<S> { | ||
s: S, | ||
} | ||
pub struct ServiceHandler<S>(S); | ||
|
||
impl<S> ServiceHandler<S> { | ||
/// Creates a new [`ServiceHandler`]. | ||
pub fn new(s: S) -> Self { | ||
Self { s } | ||
Self(s) | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl<I, O, S> Handler<Request<I>> for ServiceHandler<S> | ||
where | ||
I: HttpBody + Send + Unpin + 'static, | ||
I: HttpBody + Send + 'static, | ||
O: HttpBody + Send + 'static, | ||
O::Data: Into<Bytes>, | ||
O::Error: Into<Error>, | ||
O::Error: Into<BoxError>, | ||
S: Service<Request<I>, Response = Response<O>> + Send + Sync + Clone + 'static, | ||
S::Future: Send, | ||
S::Error: Into<Error>, | ||
S::Error: Into<BoxError>, | ||
{ | ||
type Output = Result<Response>; | ||
|
||
async fn call(&self, req: Request<I>) -> Self::Output { | ||
self.s | ||
self.0 | ||
.call(req) | ||
.await | ||
.map(|resp| { | ||
resp.map(|body| { | ||
body.map_frame(|f| f.map_data(Into::into)) | ||
.map_err(Into::into) | ||
.boxed_unsync() | ||
.into() | ||
}) | ||
}) | ||
.map_err(Into::into) | ||
.map(|resp| resp.map(Body::wrap)) | ||
.map_err(Error::boxed) | ||
} | ||
} |
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,9 @@ | ||
viz-tower | ||
--------- | ||
|
||
An adapter that makes a tower [`Service`] into a [`Handler`]. | ||
|
||
See [tower example](../examples/tower/). | ||
|
||
[`Service`]: https://docs.rs/tower/latest/tower/trait.Service.html | ||
[`Handler`]: https://docs.rs/viz/latest/viz/trait.Handler.html |
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
Oops, something went wrong.