Skip to main content

reifydb_sub_server_http/
factory.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2025 ReifyDB
3
4//! Factory for creating HTTP subsystem instances.
5
6use std::time::Duration;
7
8use reifydb_core::util::ioc::IocContainer;
9use reifydb_engine::engine::StandardEngine;
10use reifydb_runtime::SharedRuntime;
11use reifydb_sub_api::subsystem::{Subsystem, SubsystemFactory};
12use reifydb_sub_server::state::{AppState, StateConfig};
13use reifydb_type::Result;
14
15use crate::subsystem::HttpSubsystem;
16
17/// Configuration for the HTTP server subsystem.
18#[derive(Clone, Debug)]
19pub struct HttpConfig {
20	/// Address to bind the HTTP server to (e.g., "0.0.0.0:8091").
21	pub bind_addr: String,
22	/// Maximum number of concurrent connections.
23	pub max_connections: usize,
24	/// Timeout for query execution.
25	pub query_timeout: Duration,
26	/// Timeout for entire request lifecycle.
27	pub request_timeout: Duration,
28	/// Optional shared runtime .
29	pub runtime: Option<SharedRuntime>,
30}
31
32impl Default for HttpConfig {
33	fn default() -> Self {
34		Self {
35			bind_addr: "0.0.0.0:8091".to_string(),
36			max_connections: 10_000,
37			query_timeout: Duration::from_secs(30),
38			request_timeout: Duration::from_secs(60),
39			runtime: None,
40		}
41	}
42}
43
44impl HttpConfig {
45	/// Create a new HTTP config with default values.
46	pub fn new() -> Self {
47		Self::default()
48	}
49
50	/// Set the bind address.
51	pub fn bind_addr(mut self, addr: impl Into<String>) -> Self {
52		self.bind_addr = addr.into();
53		self
54	}
55
56	/// Set the maximum number of connections.
57	pub fn max_connections(mut self, max: usize) -> Self {
58		self.max_connections = max;
59		self
60	}
61
62	/// Set the query timeout.
63	pub fn query_timeout(mut self, timeout: Duration) -> Self {
64		self.query_timeout = timeout;
65		self
66	}
67
68	/// Set the request timeout.
69	pub fn request_timeout(mut self, timeout: Duration) -> Self {
70		self.request_timeout = timeout;
71		self
72	}
73
74	/// Set the shared runtime.
75	pub fn runtime(mut self, runtime: SharedRuntime) -> Self {
76		self.runtime = Some(runtime);
77		self
78	}
79}
80
81/// Factory for creating HTTP subsystem instances.
82pub struct HttpSubsystemFactory {
83	config: HttpConfig,
84}
85
86impl HttpSubsystemFactory {
87	/// Create a new HTTP subsystem factory with the given configuration.
88	pub fn new(config: HttpConfig) -> Self {
89		Self {
90			config,
91		}
92	}
93}
94
95impl SubsystemFactory for HttpSubsystemFactory {
96	fn create(self: Box<Self>, ioc: &IocContainer) -> Result<Box<dyn Subsystem>> {
97		let engine = ioc.resolve::<StandardEngine>()?;
98		let ioc_runtime = ioc.resolve::<SharedRuntime>()?;
99
100		let query_config = StateConfig::new()
101			.query_timeout(self.config.query_timeout)
102			.request_timeout(self.config.request_timeout)
103			.max_connections(self.config.max_connections);
104
105		let runtime = self.config.runtime.unwrap_or(ioc_runtime);
106
107		let state = AppState::new(runtime.actor_system(), engine, query_config);
108		let subsystem = HttpSubsystem::new(self.config.bind_addr.clone(), state, runtime);
109
110		Ok(Box::new(subsystem))
111	}
112}