Skip to content

Commit ec520d5

Browse files
LucioFrancoseanmonstar
authored andcommitted
feat(service): use tower_service::Service for hyper::service
1 parent 53a437c commit ec520d5

File tree

11 files changed

+230
-111
lines changed

11 files changed

+230
-111
lines changed

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ pin-utils = "0.1.0-alpha.4"
3838
time = "0.1"
3939
tokio = { version = "0.2.0-alpha.2", optional = true, default-features = false, features = ["rt-full"] }
4040
#tokio-buf = "0.2.0-alpha.1"
41+
tower-service = "=0.3.0-alpha.1"
4142
tokio-executor = "0.2.0-alpha.2"
4243
tokio-io = "0.2.0-alpha.2"
4344
tokio-sync = "0.2.0-alpha.2"

examples/tower_server.rs

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#![feature(async_await)]
2+
#![deny(warnings)]
3+
4+
use hyper::{Body, Request, Response, Server};
5+
use tower_service::Service;
6+
use futures_util::future;
7+
use std::task::{Context, Poll};
8+
9+
const ROOT: &'static str = "/";
10+
11+
#[derive(Debug)]
12+
pub struct Svc;
13+
14+
impl Service<Request<Body>> for Svc {
15+
type Response = Response<Body>;
16+
type Error = hyper::Error;
17+
type Future = future::Ready<Result<Self::Response, Self::Error>>;
18+
19+
fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
20+
Ok(()).into()
21+
}
22+
23+
fn call(&mut self, req: Request<Body>) -> Self::Future {
24+
let mut rsp = Response::builder();
25+
26+
let uri = req.uri();
27+
if uri.path() != ROOT {
28+
let body = Body::from(Vec::new());
29+
let rsp = rsp.status(404).body(body).unwrap();
30+
return future::ok(rsp);
31+
}
32+
33+
let body = Body::from(Vec::from(&b"heyo!"[..]));
34+
let rsp = rsp.status(200).body(body).unwrap();
35+
future::ok(rsp)
36+
}
37+
}
38+
39+
pub struct MakeSvc;
40+
41+
impl<T> Service<T> for MakeSvc {
42+
type Response = Svc;
43+
type Error = std::io::Error;
44+
type Future = future::Ready<Result<Self::Response, Self::Error>>;
45+
46+
fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
47+
Ok(()).into()
48+
}
49+
50+
fn call(&mut self, _: T) -> Self::Future {
51+
future::ok(Svc)
52+
}
53+
}
54+
55+
#[tokio::main]
56+
async fn main() -> Result<(), Box<dyn std::error::Error>> {
57+
pretty_env_logger::init();
58+
59+
let addr = "127.0.0.1:1337".parse().unwrap();
60+
61+
62+
let server = Server::bind(&addr)
63+
.serve(MakeSvc);
64+
65+
println!("Listening on http://{}", addr);
66+
67+
server.await?;
68+
69+
Ok(())
70+
}

src/common/exec.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::sync::Arc;
55

66
use tokio_executor::{SpawnError, TypedExecutor};
77

8-
use crate::body::Payload;
8+
use crate::body::{Payload, Body};
99
use crate::proto::h2::server::H2Stream;
1010
use crate::server::conn::spawn_all::{NewSvcTask, Watcher};
1111
use crate::service::Service;
@@ -14,7 +14,7 @@ pub trait H2Exec<F, B: Payload>: Clone {
1414
fn execute_h2stream(&mut self, fut: H2Stream<F, B>) -> crate::Result<()>;
1515
}
1616

17-
pub trait NewSvcExec<I, N, S: Service, E, W: Watcher<I, S, E>>: Clone {
17+
pub trait NewSvcExec<I, N, S: Service<Body>, E, W: Watcher<I, S, E>>: Clone {
1818
fn execute_new_svc(&mut self, fut: NewSvcTask<I, N, S, E, W>) -> crate::Result<()>;
1919
}
2020

@@ -119,7 +119,7 @@ where
119119
impl<I, N, S, E, W> NewSvcExec<I, N, S, E, W> for Exec
120120
where
121121
NewSvcTask<I, N, S, E, W>: Future<Output=()> + Send + 'static,
122-
S: Service,
122+
S: Service<Body>,
123123
W: Watcher<I, S, E>,
124124
{
125125
fn execute_new_svc(&mut self, fut: NewSvcTask<I, N, S, E, W>) -> crate::Result<()> {
@@ -148,7 +148,7 @@ impl<I, N, S, E, W> NewSvcExec<I, N, S, E, W> for E
148148
where
149149
E: TypedExecutor<NewSvcTask<I, N, S, E, W>> + Clone,
150150
NewSvcTask<I, N, S, E, W>: Future<Output=()>,
151-
S: Service,
151+
S: Service<Body>,
152152
W: Watcher<I, S, E>,
153153
{
154154
fn execute_new_svc(&mut self, fut: NewSvcTask<I, N, S, E, W>) -> crate::Result<()> {

src/proto/h1/dispatch.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pub(crate) trait Dispatch {
3030
fn should_poll(&self) -> bool;
3131
}
3232

33-
pub struct Server<S: Service> {
33+
pub struct Server<S: Service<B>, B> {
3434
in_flight: Pin<Box<Option<S::Future>>>,
3535
pub(crate) service: S,
3636
}
@@ -412,11 +412,11 @@ impl<'a, T> Drop for OptGuard<'a, T> {
412412

413413
// ===== impl Server =====
414414

415-
impl<S> Server<S>
415+
impl<S, B> Server<S, B>
416416
where
417-
S: Service,
417+
S: Service<B>,
418418
{
419-
pub fn new(service: S) -> Server<S> {
419+
pub fn new(service: S) -> Server<S, B> {
420420
Server {
421421
in_flight: Box::pin(None),
422422
service: service,
@@ -429,11 +429,11 @@ where
429429
}
430430

431431
// Service is never pinned
432-
impl<S: Service> Unpin for Server<S> {}
432+
impl<S: Service<B>, B> Unpin for Server<S, B> {}
433433

434-
impl<S, Bs> Dispatch for Server<S>
434+
impl<S, Bs> Dispatch for Server<S, Body>
435435
where
436-
S: Service<ReqBody=Body, ResBody=Bs>,
436+
S: Service<Body, ResBody=Bs>,
437437
S::Error: Into<Box<dyn StdError + Send + Sync>>,
438438
Bs: Payload,
439439
{

src/proto/h2/server.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use crate::{Body, Response};
2020

2121
pub(crate) struct Server<T, S, B, E>
2222
where
23-
S: Service,
23+
S: Service<Body>,
2424
B: Payload,
2525
{
2626
exec: E,
@@ -29,7 +29,7 @@ where
2929
}
3030

3131
// TODO: fix me
32-
impl<T, S: Service, B: Payload, E> Unpin for Server<T, S, B, E> {}
32+
impl<T, S: Service<Body>, B: Payload, E> Unpin for Server<T, S, B, E> {}
3333

3434
enum State<T, B>
3535
where
@@ -52,7 +52,7 @@ where
5252
impl<T, S, B, E> Server<T, S, B, E>
5353
where
5454
T: AsyncRead + AsyncWrite + Unpin,
55-
S: Service<ReqBody=Body, ResBody=B>,
55+
S: Service<Body, ResBody=B>,
5656
S::Error: Into<Box<dyn StdError + Send + Sync>>,
5757
B: Payload,
5858
B::Data: Unpin,
@@ -90,7 +90,7 @@ where
9090
impl<T, S, B, E> Future for Server<T, S, B, E>
9191
where
9292
T: AsyncRead + AsyncWrite + Unpin,
93-
S: Service<ReqBody=Body, ResBody=B>,
93+
S: Service<Body, ResBody=B>,
9494
S::Error: Into<Box<dyn StdError + Send + Sync>>,
9595
B: Payload,
9696
B::Data: Unpin,
@@ -133,7 +133,7 @@ where
133133
fn poll_server<S, E>(&mut self, cx: &mut task::Context<'_>, service: &mut S, exec: &mut E) -> Poll<crate::Result<()>>
134134
where
135135
S: Service<
136-
ReqBody=Body,
136+
Body,
137137
ResBody=B,
138138
>,
139139
S::Error: Into<Box<dyn StdError + Send + Sync>>,

0 commit comments

Comments
 (0)