Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(transport): Unify API for setting service to be via Routes instance #1586

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions examples/src/authentication/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ pub mod pb {
}

use pb::{EchoRequest, EchoResponse};
use tonic::{metadata::MetadataValue, transport::Server, Request, Response, Status};
use tonic::{
metadata::MetadataValue,
transport::{server::Routes, Server},
Request, Response, Status,
};

type EchoResult<T> = Result<Response<T>, Status>;

Expand All @@ -24,8 +28,9 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let server = EchoServer::default();

let svc = pb::echo_server::EchoServer::with_interceptor(server, check_auth);
let routes = Routes::builder().add_service(svc).build();

Server::builder().add_service(svc).serve(addr).await?;
Server::builder().add_routes(routes).serve(addr).await?;

Ok(())
}
Expand Down
7 changes: 5 additions & 2 deletions examples/src/autoreload/server.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use tonic::transport::server::Routes;
use tonic::{transport::Server, Request, Response, Status};

use hello_world::greeter_server::{Greeter, GreeterServer};
Expand Down Expand Up @@ -28,11 +29,13 @@ impl Greeter for MyGreeter {
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let addr = "[::1]:50051".parse().unwrap();
let greeter = MyGreeter::default();
let routes = Routes::builder()
.add_service(GreeterServer::new(MyGreeter::default()))
.build();

println!("GreeterServer listening on {}", addr);

let server = Server::builder().add_service(GreeterServer::new(greeter));
let server = Server::builder().add_routes(routes);

match listenfd::ListenFd::from_env().take_tcp_listener(0)? {
Some(listener) => {
Expand Down
9 changes: 5 additions & 4 deletions examples/src/blocking/server.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use tonic::transport::server::Routes;
use tonic::{transport::Server, Request, Response, Status};

use hello_world::greeter_server::{Greeter, GreeterServer};
Expand Down Expand Up @@ -30,12 +31,12 @@ impl Greeter for MyGreeter {

fn main() {
let addr = "[::1]:50051".parse().unwrap();
let greeter = MyGreeter::default();
let routes = Routes::builder()
.add_service(GreeterServer::new(MyGreeter::default()))
.build();

let rt = Runtime::new().expect("failed to obtain a new RunTime object");
let server_future = Server::builder()
.add_service(GreeterServer::new(greeter))
.serve(addr);
let server_future = Server::builder().add_routes(routes).serve(addr);
rt.block_on(server_future)
.expect("failed to successfully run the future on RunTime");
}
11 changes: 6 additions & 5 deletions examples/src/cancellation/server.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::future::Future;

use tokio_util::sync::CancellationToken;
use tonic::transport::server::Routes;
use tonic::{transport::Server, Request, Response, Status};

use hello_world::greeter_server::{Greeter, GreeterServer};
Expand Down Expand Up @@ -72,14 +73,14 @@ where
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let addr = "[::1]:50051".parse().unwrap();
let greeter = MyGreeter::default();

let routes = Routes::builder()
.add_service(GreeterServer::new(MyGreeter::default()))
.build();

println!("GreeterServer listening on {}", addr);

Server::builder()
.add_service(GreeterServer::new(greeter))
.serve(addr)
.await?;
Server::builder().add_routes(routes).serve(addr).await?;

Ok(())
}
12 changes: 7 additions & 5 deletions examples/src/compression/server.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use tonic::transport::server::Routes;
use tonic::{transport::Server, Request, Response, Status};

use hello_world::greeter_server::{Greeter, GreeterServer};
Expand Down Expand Up @@ -29,15 +30,16 @@ impl Greeter for MyGreeter {
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let addr = "[::1]:50051".parse().unwrap();
let greeter = MyGreeter::default();

println!("GreeterServer listening on {}", addr);

let service = GreeterServer::new(greeter)
let service = GreeterServer::new(MyGreeter::default())
.send_compressed(CompressionEncoding::Gzip)
.accept_compressed(CompressionEncoding::Gzip);

Server::builder().add_service(service).serve(addr).await?;
let routes = Routes::builder().add_service(service).build();

println!("GreeterServer listening on {}", addr);

Server::builder().add_routes(routes).serve(addr).await?;

Ok(())
}
47 changes: 20 additions & 27 deletions examples/src/dynamic/server.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::env;
use tonic::{transport::server::RoutesBuilder, transport::Server, Request, Response, Status};
use tonic::{transport::server::Routes, transport::Server, Request, Response, Status};

use hello_world::greeter_server::{Greeter, GreeterServer};
use hello_world::{HelloReply, HelloRequest};
Expand Down Expand Up @@ -31,15 +31,6 @@ impl Echo for MyEcho {
}
}

fn init_echo(args: &[String], builder: &mut RoutesBuilder) {
let enabled = args.iter().any(|arg| arg.as_str() == "echo");
if enabled {
println!("Adding Echo service...");
let svc = EchoServer::new(MyEcho::default());
builder.add_service(svc);
}
}

#[derive(Default)]
pub struct MyGreeter {}

Expand All @@ -58,31 +49,33 @@ impl Greeter for MyGreeter {
}
}

fn init_greeter(args: &[String], builder: &mut RoutesBuilder) {
let enabled = args.iter().any(|arg| arg.as_str() == "greeter");

if enabled {
println!("Adding Greeter service...");
let svc = GreeterServer::new(MyGreeter::default());
builder.add_service(svc);
}
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let args: Vec<String> = env::args().collect();
let mut routes_builder = RoutesBuilder::default();
init_greeter(&args, &mut routes_builder);
init_echo(&args, &mut routes_builder);

let routes_builder = Routes::builder();

let routes_builder = if args.iter().any(|arg| arg.as_str() == "greeter") {
println!("Adding Greeter service...");
routes_builder.add_service(GreeterServer::new(MyGreeter::default()))
} else {
routes_builder
};

let routes_builder = if args.iter().any(|arg| arg.as_str() == "echo") {
println!("Adding Echo service...");
routes_builder.add_service(EchoServer::new(MyEcho::default()))
} else {
routes_builder
};

let routes = routes_builder.build();

let addr = "[::1]:50051".parse().unwrap();

println!("Grpc server listening on {}", addr);

Server::builder()
.add_routes(routes_builder.routes())
.serve(addr)
.await?;
Server::builder().add_routes(routes).serve(addr).await?;

Ok(())
}
13 changes: 8 additions & 5 deletions examples/src/dynamic_load_balance/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ pub mod pb {

use std::net::SocketAddr;
use tokio::sync::mpsc;
use tonic::{transport::Server, Request, Response, Status};
use tonic::{
transport::{server::Routes, Server},
Request, Response, Status,
};

use pb::{EchoRequest, EchoResponse};

Expand Down Expand Up @@ -34,10 +37,10 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let addr = addr.parse()?;
let tx = tx.clone();

let server = EchoServer { addr };
let serve = Server::builder()
.add_service(pb::echo_server::EchoServer::new(server))
.serve(addr);
let routes = Routes::builder()
.add_service(pb::echo_server::EchoServer::new(EchoServer { addr }))
.build();
let serve = Server::builder().add_routes(routes).serve(addr);

tokio::spawn(async move {
if let Err(e) = serve.await {
Expand Down
9 changes: 5 additions & 4 deletions examples/src/grpc-web/server.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use tonic::transport::server::Routes;
use tonic::{transport::Server, Request, Response, Status};

use hello_world::greeter_server::{Greeter, GreeterServer};
Expand Down Expand Up @@ -30,16 +31,16 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
tracing_subscriber::fmt::init();

let addr = "127.0.0.1:3000".parse().unwrap();

let greeter = MyGreeter::default();
let greeter = GreeterServer::new(greeter);
let routes = Routes::builder()
.add_service(GreeterServer::new(MyGreeter::default()))
.build();

println!("GreeterServer listening on {}", addr);

Server::builder()
// GrpcWeb is over http1 so we must enable it.
.accept_http1(true)
.add_service(tonic_web::enable(greeter))
.add_routes(routes)
.serve(addr)
.await?;

Expand Down
5 changes: 3 additions & 2 deletions examples/src/h2c/server.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use tonic::{transport::Server, Request, Response, Status};
use tonic::{transport::server::Routes, Request, Response, Status};

use hello_world::greeter_server::{Greeter, GreeterServer};
use hello_world::{HelloReply, HelloRequest};
Expand Down Expand Up @@ -33,8 +33,9 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {

println!("GreeterServer listening on {}", addr);

let svc = Server::builder()
let svc = Routes::builder()
.add_service(GreeterServer::new(greeter))
.build()
.into_router();

let h2c = h2c::H2c { s: svc };
Expand Down
13 changes: 7 additions & 6 deletions examples/src/health/server.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use tonic::transport::server::Routes;
use tonic::{transport::Server, Request, Response, Status};

use hello_world::greeter_server::{Greeter, GreeterServer};
Expand Down Expand Up @@ -53,15 +54,15 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
tokio::spawn(twiddle_service_status(health_reporter.clone()));

let addr = "[::1]:50051".parse().unwrap();
let greeter = MyGreeter::default();

let routes = Routes::builder()
.add_service(health_service)
.add_service(GreeterServer::new(MyGreeter::default()))
.build();

println!("HealthServer + GreeterServer listening on {}", addr);

Server::builder()
.add_service(health_service)
.add_service(GreeterServer::new(greeter))
.serve(addr)
.await?;
Server::builder().add_routes(routes).serve(addr).await?;

Ok(())
}
10 changes: 5 additions & 5 deletions examples/src/helloworld/server.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use tonic::transport::server::Routes;
use tonic::{transport::Server, Request, Response, Status};

use hello_world::greeter_server::{Greeter, GreeterServer};
Expand Down Expand Up @@ -28,14 +29,13 @@ impl Greeter for MyGreeter {
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let addr = "[::1]:50051".parse().unwrap();
let greeter = MyGreeter::default();
let routes = Routes::builder()
.add_service(GreeterServer::new(MyGreeter::default()))
.build();

println!("GreeterServer listening on {}", addr);

Server::builder()
.add_service(GreeterServer::new(greeter))
.serve(addr)
.await?;
Server::builder().add_routes(routes).serve(addr).await?;

Ok(())
}
7 changes: 4 additions & 3 deletions examples/src/hyper_warp_multiplex/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ use std::{
pin::Pin,
task::{Context, Poll},
};
use tonic::{transport::Server as TonicServer, Request, Response, Status};
use tonic::transport::server::Routes;
use tonic::{Request, Response, Status};
use tower::Service;
use warp::Filter;

Expand Down Expand Up @@ -75,10 +76,10 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let greeter = GreeterServer::new(MyGreeter::default());
let echo = EchoServer::new(MyEcho::default());

let mut tonic = TonicServer::builder()
let mut tonic = Routes::builder()
.add_service(greeter)
.add_service(echo)
.into_service();
.build();

std::future::ready(Ok::<_, Infallible>(tower::service_fn(
move |req: hyper::Request<hyper::Body>| match req.version() {
Expand Down
4 changes: 3 additions & 1 deletion examples/src/interceptor/server.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use tonic::transport::server::Routes;
use tonic::{transport::Server, Request, Response, Status};

use hello_world::greeter_server::{Greeter, GreeterServer};
Expand Down Expand Up @@ -35,10 +36,11 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
// named interceptor that can be returned from functions or stored in
// structs.
let svc = GreeterServer::with_interceptor(greeter, intercept);
let routes = Routes::builder().add_service(svc).build();

println!("GreeterServer listening on {}", addr);

Server::builder().add_service(svc).serve(addr).await?;
Server::builder().add_routes(routes).serve(addr).await?;

Ok(())
}
Expand Down
14 changes: 8 additions & 6 deletions examples/src/json-codec/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
//! in the `examples/build.rs` file. As defined there, the generated code assumes that a module
//! `crate::common` exists which defines `HelloRequest`, `HelloResponse`, and `JsonCodec`.

use tonic::{transport::Server, Request, Response, Status};
use tonic::{
transport::{server::Routes, Server},
Request, Response, Status,
};

pub mod common;
use common::{HelloRequest, HelloResponse};
Expand Down Expand Up @@ -35,14 +38,13 @@ impl Greeter for MyGreeter {
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let addr = "[::1]:50051".parse().unwrap();
let greeter = MyGreeter::default();
let routes = Routes::builder()
.add_service(GreeterServer::new(MyGreeter::default()))
.build();

println!("GreeterServer listening on {}", addr);

Server::builder()
.add_service(GreeterServer::new(greeter))
.serve(addr)
.await?;
Server::builder().add_routes(routes).serve(addr).await?;

Ok(())
}
Loading