reifydb_sub_server/config/
server.rs

1// Copyright (c) reifydb.com 2025
2// This file is licensed under the AGPL-3.0-or-later, see license.md file
3
4use super::{NetworkConfig, ProtocolConfigs};
5
6/// Main server configuration supporting multiple protocols
7#[derive(Debug, Clone)]
8pub struct ServerConfig {
9	/// Bind address and port
10	pub bind_addr: String,
11	/// Network and performance configuration
12	pub network: NetworkConfig,
13	/// Protocol-specific configurations
14	pub protocols: ProtocolConfigs,
15}
16
17impl ServerConfig {
18	/// Create a new ServerConfig with defaults
19	pub fn new() -> Self {
20		Self::default()
21	}
22
23	/// Set the bind address
24	pub fn bind_addr<S: Into<String>>(mut self, addr: S) -> Self {
25		self.bind_addr = addr.into();
26		self
27	}
28
29	/// Configure network settings
30	pub fn network(mut self, config: NetworkConfig) -> Self {
31		self.network = config;
32		self
33	}
34
35	/// Configure protocols
36	pub fn protocols(mut self, config: ProtocolConfigs) -> Self {
37		self.protocols = config;
38		self
39	}
40
41	/// Enable WebSocket protocol with configuration
42	pub fn enable_websocket(mut self, config: Option<super::WebSocketConfig>) -> Self {
43		self.protocols.ws = config.or_else(|| Some(Default::default()));
44		self
45	}
46
47	/// Enable HTTP protocol with configuration
48	pub fn enable_http(mut self, config: Option<super::HttpConfig>) -> Self {
49		self.protocols.http = config.or_else(|| Some(Default::default()));
50		self
51	}
52
53	/// Get the effective number of listeners
54	pub fn effective_listeners(&self) -> usize {
55		self.network.listeners.unwrap_or(1)
56	}
57}
58
59impl Default for ServerConfig {
60	fn default() -> Self {
61		Self {
62			bind_addr: "0.0.0.0:8090".to_string(),
63			network: NetworkConfig::default(),
64			protocols: ProtocolConfigs::default(),
65		}
66	}
67}