Skip to content

Commit

Permalink
feat(server): Allow server used independently of router
Browse files Browse the repository at this point in the history
  • Loading branch information
tottoto committed Jan 19, 2025
1 parent a7de991 commit b1153f6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
3 changes: 1 addition & 2 deletions tonic/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ codegen = ["dep:async-trait"]
gzip = ["dep:flate2"]
deflate = ["dep:flate2"]
zstd = ["dep:zstd"]
default = ["transport", "codegen", "prost"]
default = ["router", "transport", "codegen", "prost"]
prost = ["dep:prost"]
_tls-any = ["dep:tokio-rustls", "dep:tokio", "tokio?/rt", "tokio?/macros"] # Internal. Please choose one of `tls-ring` or `tls-aws-lc`
tls-ring = ["_tls-any", "tokio-rustls/ring"]
Expand All @@ -36,7 +36,6 @@ tls-native-roots = ["_tls-any", "channel", "dep:rustls-native-certs"]
tls-webpki-roots = ["_tls-any","channel", "dep:webpki-roots"]
router = ["dep:axum", "dep:tower", "tower?/util"]
server = [
"router",
"dep:h2",
"dep:hyper", "hyper?/server",
"dep:hyper-util", "hyper-util?/service", "hyper-util?/server-auto",
Expand Down
14 changes: 11 additions & 3 deletions tonic/src/transport/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ mod unix;
use tokio_stream::StreamExt as _;
use tracing::{debug, trace};

use crate::service::Routes;
#[cfg(feature = "router")]
use crate::{server::NamedService, service::Routes};

#[cfg(feature = "router")]
use std::convert::Infallible;

pub use conn::{Connected, TcpConnectInfo};
use hyper_util::{
Expand Down Expand Up @@ -40,15 +44,13 @@ use crate::transport::Error;
use self::service::{ConnectInfoLayer, RecoverError, ServerIo};
use super::service::GrpcTimeout;
use crate::body::Body;
use crate::server::NamedService;
use bytes::Bytes;
use http::{Request, Response};
use http_body_util::BodyExt;
use hyper::{body::Incoming, service::Service as HyperService};
use pin_project::pin_project;
use std::future::pending;
use std::{
convert::Infallible,
fmt,
future::{self, poll_fn, Future},
marker::PhantomData,
Expand Down Expand Up @@ -132,6 +134,7 @@ impl Default for Server<Identity> {
}

/// A stack based [`Service`] router.
#[cfg(feature = "router")]
#[derive(Debug)]
pub struct Router<L = Identity> {
server: Server<L>,
Expand Down Expand Up @@ -393,6 +396,7 @@ impl<L> Server<L> {
///
/// This will clone the `Server` builder and create a router that will
/// route around different services.
#[cfg(feature = "router")]
pub fn add_service<S>(&mut self, svc: S) -> Router<L>
where
S: Service<Request<Body>, Error = Infallible>
Expand All @@ -416,6 +420,7 @@ impl<L> Server<L> {
/// # Note
/// Even when the argument given is `None` this will capture *all* requests to this service name.
/// As a result, one cannot use this to toggle between two identically named implementations.
#[cfg(feature = "router")]
pub fn add_optional_service<S>(&mut self, svc: Option<S>) -> Router<L>
where
S: Service<Request<Body>, Error = Infallible>
Expand All @@ -436,6 +441,7 @@ impl<L> Server<L> {
///
/// This will clone the `Server` builder and create a router that will
/// route around different services that were already added to the provided `routes`.
#[cfg(feature = "router")]
pub fn add_routes(&mut self, routes: Routes) -> Router<L>
where
L: Clone,
Expand Down Expand Up @@ -815,12 +821,14 @@ async fn sleep_or_pending(wait_for: Option<Duration>) {
};
}

#[cfg(feature = "router")]
impl<L> Router<L> {
pub(crate) fn new(server: Server<L>, routes: Routes) -> Self {
Self { server, routes }
}
}

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

0 comments on commit b1153f6

Please sign in to comment.