spacegate_config/service/memory/
mod.rs1use std::sync::Arc;
2
3use spacegate_model::PluginConfig;
4
5use crate::Config;
6
7use super::Retrieve;
8
9#[derive(Debug, Clone)]
11pub struct Memory {
12 pub config: Arc<Config>,
13}
14
15impl Memory {
16 pub fn new(config: Config) -> Self {
17 Self { config: Arc::new(config) }
18 }
19}
20
21impl Retrieve for Memory {
22 async fn retrieve_config_item_gateway(&self, gateway_name: &str) -> Result<Option<spacegate_model::SgGateway>, spacegate_model::BoxError> {
23 Ok(self.config.gateways.get(gateway_name).map(|item| item.gateway.clone()))
24 }
25
26 async fn retrieve_config_item_route(&self, gateway_name: &str, route_name: &str) -> Result<Option<spacegate_model::SgHttpRoute>, spacegate_model::BoxError> {
27 Ok(self.config.gateways.get(gateway_name).and_then(|item| item.routes.get(route_name).cloned()))
28 }
29
30 async fn retrieve_config_item_route_names(&self, name: &str) -> Result<Vec<String>, spacegate_model::BoxError> {
31 Ok(self.config.gateways.get(name).map(|item| item.routes.keys().cloned().collect::<Vec<_>>()).unwrap_or_default())
32 }
33
34 async fn retrieve_config_names(&self) -> Result<Vec<String>, spacegate_model::BoxError> {
35 Ok(self.config.gateways.keys().cloned().collect())
36 }
37
38 async fn retrieve_all_plugins(&self) -> Result<Vec<PluginConfig>, spacegate_model::BoxError> {
39 Ok(self.config.plugins.clone().into_config_vec())
40 }
41
42 async fn retrieve_plugin(&self, id: &spacegate_model::PluginInstanceId) -> Result<Option<spacegate_model::PluginConfig>, spacegate_model::BoxError> {
43 Ok(self.config.plugins.get(id).map(|spec| spacegate_model::PluginConfig {
44 spec: spec.clone(),
45 id: id.clone(),
46 }))
47 }
48
49 async fn retrieve_plugins_by_code(&self, code: &str) -> Result<Vec<PluginConfig>, spacegate_model::BoxError> {
50 Ok(self
51 .config
52 .plugins
53 .iter()
54 .filter_map(|(id, spec)| {
55 (id.code == code).then_some(PluginConfig {
56 spec: spec.clone(),
57 id: id.clone(),
58 })
59 })
60 .collect())
61 }
62}
63
64use super::{CreateListener, Listen};
65#[derive(Debug, Clone, Default)]
66pub struct Static;
67
68impl Listen for Static {
69 fn poll_next(&mut self, _cx: &mut std::task::Context<'_>) -> std::task::Poll<Result<super::ListenEvent, crate::BoxError>> {
70 std::task::Poll::Pending
71 }
72}
73
74impl CreateListener for Memory {
75 const CONFIG_LISTENER_NAME: &'static str = "memory";
76 type Listener = Static;
77 async fn create_listener(&self) -> Result<(Config, Self::Listener), Box<dyn std::error::Error + Sync + Send + 'static>> {
78 Ok((self.config.as_ref().clone(), Static))
79 }
80}