#[cfg(feature = "axum")]
pub mod axum_adapter;
pub mod cors;
#[cfg(feature = "grpc")]
pub mod grpc;
pub mod jsonrpc;
pub mod rest;
#[cfg(feature = "websocket")]
pub mod websocket;
pub use cors::CorsConfig;
#[cfg(feature = "grpc")]
pub use grpc::{GrpcConfig, GrpcDispatcher};
pub use jsonrpc::JsonRpcDispatcher;
pub use rest::RestDispatcher;
#[cfg(feature = "websocket")]
pub use websocket::WebSocketDispatcher;
#[derive(Debug, Clone)]
pub struct DispatchConfig {
pub max_request_body_size: usize,
pub body_read_timeout: std::time::Duration,
pub max_query_string_length: usize,
pub sse_keep_alive_interval: std::time::Duration,
pub sse_channel_capacity: usize,
pub max_batch_size: usize,
}
impl Default for DispatchConfig {
fn default() -> Self {
Self {
max_request_body_size: 4 * 1024 * 1024,
body_read_timeout: std::time::Duration::from_secs(30),
max_query_string_length: 4096,
sse_keep_alive_interval: std::time::Duration::from_secs(30),
sse_channel_capacity: 64,
max_batch_size: 100,
}
}
}
impl DispatchConfig {
#[must_use]
pub const fn with_max_request_body_size(mut self, size: usize) -> Self {
self.max_request_body_size = size;
self
}
#[must_use]
pub const fn with_body_read_timeout(mut self, timeout: std::time::Duration) -> Self {
self.body_read_timeout = timeout;
self
}
#[must_use]
pub const fn with_max_query_string_length(mut self, length: usize) -> Self {
self.max_query_string_length = length;
self
}
#[must_use]
pub const fn with_sse_keep_alive_interval(mut self, interval: std::time::Duration) -> Self {
self.sse_keep_alive_interval = interval;
self
}
#[must_use]
pub const fn with_sse_channel_capacity(mut self, capacity: usize) -> Self {
self.sse_channel_capacity = capacity;
self
}
#[must_use]
pub const fn with_max_batch_size(mut self, size: usize) -> Self {
self.max_batch_size = size;
self
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::time::Duration;
#[test]
fn default_values() {
let config = DispatchConfig::default();
assert_eq!(config.max_request_body_size, 4 * 1024 * 1024);
assert_eq!(config.body_read_timeout, Duration::from_secs(30));
assert_eq!(config.max_query_string_length, 4096);
assert_eq!(config.sse_keep_alive_interval, Duration::from_secs(30));
assert_eq!(config.sse_channel_capacity, 64);
assert_eq!(config.max_batch_size, 100);
}
#[test]
fn with_max_request_body_size_sets_value() {
let config = DispatchConfig::default().with_max_request_body_size(8 * 1024 * 1024);
assert_eq!(config.max_request_body_size, 8 * 1024 * 1024);
}
#[test]
fn with_body_read_timeout_sets_value() {
let config = DispatchConfig::default().with_body_read_timeout(Duration::from_secs(60));
assert_eq!(config.body_read_timeout, Duration::from_secs(60));
}
#[test]
fn with_max_query_string_length_sets_value() {
let config = DispatchConfig::default().with_max_query_string_length(8192);
assert_eq!(config.max_query_string_length, 8192);
}
#[test]
fn with_sse_keep_alive_interval_sets_value() {
let config =
DispatchConfig::default().with_sse_keep_alive_interval(Duration::from_secs(15));
assert_eq!(config.sse_keep_alive_interval, Duration::from_secs(15));
}
#[test]
fn with_sse_channel_capacity_sets_value() {
let config = DispatchConfig::default().with_sse_channel_capacity(128);
assert_eq!(config.sse_channel_capacity, 128);
}
#[test]
fn with_max_batch_size_sets_value() {
let config = DispatchConfig::default().with_max_batch_size(50);
assert_eq!(config.max_batch_size, 50);
}
#[test]
fn builder_chaining() {
let config = DispatchConfig::default()
.with_max_request_body_size(1024)
.with_body_read_timeout(Duration::from_secs(10))
.with_max_query_string_length(2048)
.with_sse_keep_alive_interval(Duration::from_secs(5))
.with_sse_channel_capacity(32)
.with_max_batch_size(25);
assert_eq!(config.max_request_body_size, 1024);
assert_eq!(config.body_read_timeout, Duration::from_secs(10));
assert_eq!(config.max_query_string_length, 2048);
assert_eq!(config.sse_keep_alive_interval, Duration::from_secs(5));
assert_eq!(config.sse_channel_capacity, 32);
assert_eq!(config.max_batch_size, 25);
}
#[test]
fn debug_format() {
let config = DispatchConfig::default();
let debug = format!("{config:?}");
assert!(debug.contains("DispatchConfig"));
assert!(debug.contains("max_request_body_size"));
assert!(debug.contains("body_read_timeout"));
assert!(debug.contains("max_query_string_length"));
assert!(debug.contains("sse_keep_alive_interval"));
assert!(debug.contains("sse_channel_capacity"));
assert!(debug.contains("max_batch_size"));
}
}