Skip to main content

reifydb_sub_admin/subsystem/
admin.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 std::any::Any;
5
6use reifydb_core::interface::version::{ComponentType, HasVersion, SystemVersion};
7use reifydb_engine::StandardEngine;
8use reifydb_sub_api::{HealthStatus, Subsystem};
9
10use crate::{config::AdminConfig, server::AdminServer};
11
12pub struct AdminSubsystem {
13	config: AdminConfig,
14	server: Option<AdminServer>,
15	engine: StandardEngine,
16}
17
18impl AdminSubsystem {
19	pub fn new(config: AdminConfig, engine: StandardEngine) -> Self {
20		Self {
21			config,
22			server: None,
23			engine,
24		}
25	}
26
27	pub fn port(&self) -> u16 {
28		self.config.port
29	}
30}
31
32impl Subsystem for AdminSubsystem {
33	fn name(&self) -> &'static str {
34		"sub-admin"
35	}
36
37	fn start(&mut self) -> reifydb_type::Result<()> {
38		if !self.config.enabled {
39			return Ok(());
40		}
41
42		if self.server.is_some() {
43			return Ok(());
44		}
45
46		let mut server = AdminServer::new(self.config.clone(), self.engine.clone());
47
48		server.start().map_err(|e| {
49			reifydb_type::error!(reifydb_type::diagnostic::internal::internal(format!(
50				"Failed to start admin server: {:?}",
51				e
52			)))
53		})?;
54
55		self.server = Some(server);
56		Ok(())
57	}
58
59	fn shutdown(&mut self) -> reifydb_type::Result<()> {
60		if let Some(mut server) = self.server.take() {
61			server.stop();
62		}
63		Ok(())
64	}
65
66	fn is_running(&self) -> bool {
67		self.server.as_ref().map_or(false, |s| s.is_running())
68	}
69
70	fn health_status(&self) -> HealthStatus {
71		if !self.config.enabled {
72			return HealthStatus::Healthy;
73		}
74
75		if self.is_running() {
76			HealthStatus::Healthy
77		} else {
78			HealthStatus::Failed {
79				description: "Admin server is not running".to_string(),
80			}
81		}
82	}
83
84	fn as_any(&self) -> &dyn Any {
85		self
86	}
87
88	fn as_any_mut(&mut self) -> &mut dyn Any {
89		self
90	}
91}
92
93impl HasVersion for AdminSubsystem {
94	fn version(&self) -> SystemVersion {
95		SystemVersion {
96			name: "sub-admin".to_string(),
97			version: env!("CARGO_PKG_VERSION").to_string(),
98			description: "Web administration interface subsystem".to_string(),
99			r#type: ComponentType::Subsystem,
100		}
101	}
102}