Skip to main content

ServerBuilder

Struct ServerBuilder 

Source
pub struct ServerBuilder<B, W = JsonCodec> {
    config: ServerConfig,
    byte_codec: B,
    ws_codec: W,
    executor: Option<Executor>,
}
Expand description

Builder for constructing an RPC Server.

§Example

use std::sync::Arc;
use serde_json::Value;
use karyon_jsonrpc::{error::RPCError, rpc_impl, server::ServerBuilder};

struct Ping {}

#[rpc_impl]
impl Ping {
    async fn ping(&self, _params: Value) -> Result<Value, RPCError> {
        Ok(serde_json::json!("pong"))
    }
}

async {
    let server = ServerBuilder::new("tcp://127.0.0.1:60000")
        .expect("create builder")
        .service(Arc::new(Ping {}))
        .build().await
        .expect("build server");

    server.start_block().await.expect("run server");
};

Fields§

§config: ServerConfig§byte_codec: B§ws_codec: W§executor: Option<Executor>

Implementations§

Source§

impl<B, W> ServerBuilder<B, W>
where B: JsonRpcCodec, W: WsCodec,

Source

pub fn service(self, service: Arc<dyn RPCService>) -> Self

Add an RPC service.

Source

pub fn pubsub_service(self, service: Arc<dyn PubSubRPCService>) -> Self

Add a PubSub RPC service.

Source

pub fn tcp_config(self, config: TcpConfig) -> Result<Self>

Set TCP config.

Source

pub fn tls_config(self, config: ServerTlsConfig) -> Result<Self>

Set TLS config.

Source

pub fn quic_config(self, config: ServerQuicConfig) -> Result<Self>

Set QUIC config.

Source

pub async fn with_executor(self, ex: Executor) -> Self

Set an executor.

Source

pub fn with_notification_encoder( self, encoder: fn(NewNotification) -> Notification, ) -> Self

Set a custom notification encoder.

Source

pub fn with_ws_codec<W2: WsCodec>(self, ws_codec: W2) -> ServerBuilder<B, W2>

Override the WebSocket codec. Only meaningful for ws:// / wss:// endpoints.

Source

pub async fn build(self) -> Result<Arc<Server>>

Build the server.

Source§

impl<B: JsonRpcCodec> ServerBuilder<B, JsonCodec>

Source

pub fn new_with_codec( endpoint: impl ToEndpoint, codec: B, ) -> Result<ServerBuilder<B, JsonCodec>>

Create a builder with a custom byte-stream codec. The default JsonCodec is used for ws:// / wss:// endpoints; override with with_ws_codec.

Source§

impl ServerBuilder<JsonCodec, JsonCodec>

Source

pub fn new( endpoint: impl ToEndpoint, ) -> Result<ServerBuilder<JsonCodec, JsonCodec>>

Create a builder with the default JSON codec.

Auto Trait Implementations§

§

impl<B, W> Freeze for ServerBuilder<B, W>
where B: Freeze, W: Freeze,

§

impl<B, W = JsonCodec> !RefUnwindSafe for ServerBuilder<B, W>

§

impl<B, W> Send for ServerBuilder<B, W>
where B: Send, W: Send,

§

impl<B, W> Sync for ServerBuilder<B, W>
where B: Sync, W: Sync,

§

impl<B, W> Unpin for ServerBuilder<B, W>
where B: Unpin, W: Unpin,

§

impl<B, W> UnsafeUnpin for ServerBuilder<B, W>
where B: UnsafeUnpin, W: UnsafeUnpin,

§

impl<B, W = JsonCodec> !UnwindSafe for ServerBuilder<B, W>

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.

§

impl<T> Instrument for T

§

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

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

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> Same for T

Source§

type Output = T

Should always be Self
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.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

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
§

fn with_current_subscriber(self) -> WithDispatch<Self>

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