Skip to content

Commit 88aed0e

Browse files
authored
feat(server): Allow server used independently of router (#2154)
1 parent a7de991 commit 88aed0e

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

tonic/Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ codegen = ["dep:async-trait"]
2727
gzip = ["dep:flate2"]
2828
deflate = ["dep:flate2"]
2929
zstd = ["dep:zstd"]
30-
default = ["transport", "codegen", "prost"]
30+
default = ["router", "transport", "codegen", "prost"]
3131
prost = ["dep:prost"]
3232
_tls-any = ["dep:tokio-rustls", "dep:tokio", "tokio?/rt", "tokio?/macros"] # Internal. Please choose one of `tls-ring` or `tls-aws-lc`
3333
tls-ring = ["_tls-any", "tokio-rustls/ring"]
@@ -36,7 +36,6 @@ tls-native-roots = ["_tls-any", "channel", "dep:rustls-native-certs"]
3636
tls-webpki-roots = ["_tls-any","channel", "dep:webpki-roots"]
3737
router = ["dep:axum", "dep:tower", "tower?/util"]
3838
server = [
39-
"router",
4039
"dep:h2",
4140
"dep:hyper", "hyper?/server",
4241
"dep:hyper-util", "hyper-util?/service", "hyper-util?/server-auto",

tonic/src/transport/server/mod.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ mod unix;
1212
use tokio_stream::StreamExt as _;
1313
use tracing::{debug, trace};
1414

15-
use crate::service::Routes;
15+
#[cfg(feature = "router")]
16+
use crate::{server::NamedService, service::Routes};
17+
18+
#[cfg(feature = "router")]
19+
use std::convert::Infallible;
1620

1721
pub use conn::{Connected, TcpConnectInfo};
1822
use hyper_util::{
@@ -40,15 +44,13 @@ use crate::transport::Error;
4044
use self::service::{ConnectInfoLayer, RecoverError, ServerIo};
4145
use super::service::GrpcTimeout;
4246
use crate::body::Body;
43-
use crate::server::NamedService;
4447
use bytes::Bytes;
4548
use http::{Request, Response};
4649
use http_body_util::BodyExt;
4750
use hyper::{body::Incoming, service::Service as HyperService};
4851
use pin_project::pin_project;
4952
use std::future::pending;
5053
use std::{
51-
convert::Infallible,
5254
fmt,
5355
future::{self, poll_fn, Future},
5456
marker::PhantomData,
@@ -132,6 +134,7 @@ impl Default for Server<Identity> {
132134
}
133135

134136
/// A stack based [`Service`] router.
137+
#[cfg(feature = "router")]
135138
#[derive(Debug)]
136139
pub struct Router<L = Identity> {
137140
server: Server<L>,
@@ -393,6 +396,7 @@ impl<L> Server<L> {
393396
///
394397
/// This will clone the `Server` builder and create a router that will
395398
/// route around different services.
399+
#[cfg(feature = "router")]
396400
pub fn add_service<S>(&mut self, svc: S) -> Router<L>
397401
where
398402
S: Service<Request<Body>, Error = Infallible>
@@ -416,6 +420,7 @@ impl<L> Server<L> {
416420
/// # Note
417421
/// Even when the argument given is `None` this will capture *all* requests to this service name.
418422
/// As a result, one cannot use this to toggle between two identically named implementations.
423+
#[cfg(feature = "router")]
419424
pub fn add_optional_service<S>(&mut self, svc: Option<S>) -> Router<L>
420425
where
421426
S: Service<Request<Body>, Error = Infallible>
@@ -436,6 +441,7 @@ impl<L> Server<L> {
436441
///
437442
/// This will clone the `Server` builder and create a router that will
438443
/// route around different services that were already added to the provided `routes`.
444+
#[cfg(feature = "router")]
439445
pub fn add_routes(&mut self, routes: Routes) -> Router<L>
440446
where
441447
L: Clone,
@@ -815,12 +821,14 @@ async fn sleep_or_pending(wait_for: Option<Duration>) {
815821
};
816822
}
817823

824+
#[cfg(feature = "router")]
818825
impl<L> Router<L> {
819826
pub(crate) fn new(server: Server<L>, routes: Routes) -> Self {
820827
Self { server, routes }
821828
}
822829
}
823830

831+
#[cfg(feature = "router")]
824832
impl<L> Router<L> {
825833
/// Add a new service to this router.
826834
pub fn add_service<S>(mut self, svc: S) -> Self

0 commit comments

Comments
 (0)