Skip to main content

reifydb_sub_server_http/
factory.rs

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