-
-
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.
- Loading branch information
Showing
12 changed files
with
255 additions
and
40 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
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,70 @@ | ||
//! Viz + Tower services | ||
use std::{net::SocketAddr, sync::Arc}; | ||
use tokio::net::TcpListener; | ||
use tower::util::{MapErrLayer, MapRequestLayer, MapResponseLayer}; | ||
use tower::{service_fn, ServiceBuilder}; | ||
use tower_http::limit::RequestBodyLimitLayer; | ||
use tower_http::request_id::{MakeRequestUuid, SetRequestIdLayer}; | ||
use tower_http::trace::TraceLayer; | ||
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt}; | ||
use viz::{serve, Body, Error, IntoResponse, Request, Response, Result, Router, Tree}; | ||
use viz_tower::{Layered, TowerServiceHandler}; | ||
|
||
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 = TowerServiceHandler::new(index_svc); | ||
|
||
let any_svc = ServiceBuilder::new() | ||
.layer(MapResponseLayer::new(IntoResponse::into_response)) | ||
.service_fn(any); | ||
let any_handler = TowerServiceHandler::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
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,68 @@ | ||
use tower::{Layer, Service, ServiceExt}; | ||
use viz_core::{ | ||
async_trait, Body, BoxError, Bytes, Error, Handler, HttpBody, Request, Response, Result, | ||
Transform, | ||
}; | ||
|
||
use crate::HandlerService; | ||
|
||
/// Transforms a Tower layer into Viz Middleware. | ||
#[derive(Debug)] | ||
pub struct Layered<L>(L); | ||
|
||
impl<L> Layered<L> { | ||
/// Creates a new tower layer. | ||
pub fn new(l: L) -> Self { | ||
Self(l) | ||
} | ||
} | ||
|
||
impl<L, H> Transform<H> for Layered<L> | ||
where | ||
L: Clone, | ||
{ | ||
type Output = Middleware<L, H>; | ||
|
||
fn transform(&self, h: H) -> Self::Output { | ||
Middleware::new(self.0.clone(), h) | ||
} | ||
} | ||
|
||
/// A [`Service`] created from a [`Handler`] by applying a Tower middleware. | ||
#[derive(Debug, Clone)] | ||
pub struct Middleware<L, H> { | ||
l: L, | ||
h: H, | ||
} | ||
|
||
impl<L, H> Middleware<L, H> { | ||
/// Creates a new tower middleware. | ||
pub fn new(l: L, h: H) -> Self { | ||
Self { l, h } | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl<O, L, H> Handler<Request> for Middleware<L, H> | ||
where | ||
L: Layer<HandlerService<H>> + Send + Sync + Clone + 'static, | ||
H: Handler<Request, Output = Result<Response>> + Send + Sync + Clone + 'static, | ||
O: HttpBody + Send + 'static, | ||
O::Data: Into<Bytes>, | ||
O::Error: Into<BoxError>, | ||
L::Service: Service<Request, Response = Response<O>> + Send + Sync + Clone + 'static, | ||
<L::Service as Service<Request>>::Future: Send, | ||
<L::Service as Service<Request>>::Error: Into<BoxError>, | ||
{ | ||
type Output = Result<Response>; | ||
|
||
async fn call(&self, req: Request) -> Self::Output { | ||
self.l | ||
.clone() | ||
.layer(HandlerService::new(self.h.clone())) | ||
.oneshot(req) | ||
.await | ||
.map(|resp| resp.map(Body::wrap)) | ||
.map_err(Error::boxed) | ||
} | ||
} |
Oops, something went wrong.