Skip to content

Commit

Permalink
got it working!
Browse files Browse the repository at this point in the history
  • Loading branch information
tr8dr committed Sep 9, 2024
1 parent 00e62fd commit 795b427
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 27 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ tower-service = "0.3"

## proxy-protocol
ppp = { version = "2.2.0", optional = true }
tower = "0.4.13"
tower = { version = "0.4.13", features = ["limit"] }
nix = { version = "0.29.0", features = ["signal"] }

[[example]]
Expand Down
64 changes: 38 additions & 26 deletions examples/tower-server2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,50 +9,57 @@ use hyper::{
};
use hyper_util::rt::TokioIo;
use hyper_util::service::TowerToHyperService;
use http_body_util::Full;
use std::{convert::Infallible, future, net::SocketAddr};
use hyper::service::Service;
use http_body_util::{Full, BodyExt};
use std::net::SocketAddr;
use tokio::net::TcpListener;

use hyper::service::Service as HyperService;
use tower::{Layer, Service as TowerService};
use tower::layer::util::Stack;

type BoxBody = http_body_util::combinators::BoxBody<Bytes, hyper::Error>;


type BoxBody = http_body_util::combinators::BoxBody<hyper::body::Bytes, hyper::Error>;
pub struct ServerBuilder<L> {
layers: L,
}

pub struct Server<S> {
service: S,
}

impl Server<()> {
impl ServerBuilder<tower::layer::util::Identity> {
pub fn new() -> Self {
Server { service: () }
ServerBuilder {
layers: tower::layer::util::Identity::new(),
}
}
}

impl<S> Server<S>
where
S: TowerService<Request<Incoming>, Response = Response<BoxBody>, Error = hyper::Error> + Clone + Send + 'static,
S::Future: Send + 'static,
{
pub fn layer<L>(self, layer: L) -> Server<L::Service>
impl<L> ServerBuilder<L> {
pub fn layer<NewLayer>(self, layer: NewLayer) -> ServerBuilder<Stack<NewLayer, L>> {
ServerBuilder {
layers: Stack::new(layer, self.layers),
}
}

pub fn service<S>(self, service: S) -> Server<L::Service>
where
L: Layer<S> + Send + Sync + 'static,
S: TowerService<Request<Incoming>, Response = Response<BoxBody>, Error = hyper::Error> + Clone + Send + 'static,
S::Future: Send + 'static,
L::Service: TowerService<Request<Incoming>, Response = Response<BoxBody>, Error = hyper::Error> + Clone + Send + 'static,
<L::Service as TowerService<Request<Incoming>>>::Future: Send + 'static,
{
Server {
service: layer.layer(self.service),
service: self.layers.layer(service),
}
}
}

pub fn service<NewS>(self, service: NewS) -> Server<NewS>
where
NewS: TowerService<Request<Incoming>, Response = Response<BoxBody>, Error = hyper::Error> + Clone + Send + 'static,
NewS::Future: Send + 'static,
{
Server { service }
}

impl<S> Server<S>
where
S: TowerService<Request<Incoming>, Response = Response<BoxBody>, Error = hyper::Error> + Clone + Send + 'static,
S::Future: Send + 'static,
{
pub async fn serve(self, addr: SocketAddr) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let listener = TcpListener::bind(addr).await?;
let http = http1::Builder::new();
Expand Down Expand Up @@ -107,8 +114,12 @@ where
///
/// This function serves as our basic request handler, demonstrating a minimal
/// HTTP service implementation.
pub async fn hello(_: Request<Incoming>) -> Result<Response<Full<Bytes>>, Infallible> {
Ok(Response::new(Full::new(Bytes::from("Hello, World!"))))
pub async fn hello(_: Request<Incoming>) -> Result<Response<BoxBody>, hyper::Error> {
Ok(Response::new(
Full::new(Bytes::from("Hello, World!"))
.map_err(|never| match never {})
.boxed()
))
}


Expand All @@ -117,7 +128,8 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let addr = SocketAddr::from(([127, 0, 0, 1], 54321));
let svc = tower::service_fn(hello);

Server::new()
ServerBuilder::new()
.layer(tower::limit::ConcurrencyLimitLayer::new(64))
.service(svc)
.serve(addr)
.await?;
Expand Down

0 comments on commit 795b427

Please sign in to comment.