Skip to main content

Server

Struct Server 

Source
pub struct Server { /* private fields */ }
Available on crate feature server only.
Expand description

ConnectRPC server built on hyper.

Implementations§

Source§

impl Server

Source

pub fn new(router: Router) -> Self

Create a new server with the given router.

Source

pub fn from_service(service: ConnectRpcService) -> Self

Create a new server from an existing ConnectRpcService.

Source

pub fn with_tls(self, config: Arc<ServerConfig>) -> Self

Available on crate feature server-tls only.

Enable TLS with the given rustls server configuration.

The configuration controls all TLS behavior including certificate selection, client authentication, and protocol versions. For dynamic certificate rotation, use a rustls::server::ResolvesServerCert implementation in the config.

§Example
use std::sync::Arc;

let tls_config = Arc::new(rustls::ServerConfig::builder()
    .with_no_client_auth()
    .with_single_cert(certs, key)?);

Server::new(router)
    .with_tls(tls_config)
    .serve(addr).await?;
Source

pub fn tls_handshake_timeout(self, timeout: Duration) -> Self

Available on crate feature server-tls only.

Set the TLS handshake timeout.

Defaults to DEFAULT_TLS_HANDSHAKE_TIMEOUT (10 seconds). A client that connects via TCP but does not complete the TLS handshake within this duration is disconnected.

Source

pub fn router(&self) -> &Router

Get a reference to the underlying router.

Source

pub async fn serve( self, addr: SocketAddr, ) -> Result<(), Box<dyn Error + Send + Sync>>

Bind and serve on the given address.

This runs forever until the process is killed. For graceful shutdown, use Server::bind + BoundServer::serve_with_graceful_shutdown.

Source

pub fn from_listener(listener: TcpListener) -> BoundServer

Bind to the given address and return a BoundServer.

Accepts anything implementing tokio::net::ToSocketAddrs:

  • "127.0.0.1:8080" — IPv4 loopback (safest default for dev)
  • "[::1]:8080" — IPv6 loopback
  • "0.0.0.0:8080" — all IPv4 interfaces (only for trusted networks)
  • "[::]:8080" — all IPv6 interfaces (on Linux, also accepts IPv4 via IPv4-mapped addresses by default)
  • "localhost:8080" — resolves via DNS/hosts (may yield v4, v6, or both)

Wrap a pre-bound TcpListener.

Use this instead of Server::bind when you need to configure socket options before binding — e.g. IPV6_V6ONLY=false for dual-stack listening, SO_REUSEPORT for multi-process accept, or binding to a listener inherited from a parent process.

Source

pub async fn bind( addr: impl ToSocketAddrs, ) -> Result<BoundServer, Box<dyn Error + Send + Sync>>

When multiple addresses are returned (e.g. localhost resolving to both ::1 and 127.0.0.1), the first that successfully binds is used.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more