spacegate_config/service/redis/
retrieve.rs1use std::collections::HashMap;
2
3use super::{Redis, CONF_GATEWAY_KEY, CONF_HTTP_ROUTE_KEY, CONF_PLUGIN_KEY};
4use crate::{
5 model::{SgGateway, SgHttpRoute},
6 service::config_format::ConfigFormat,
7 BoxResult,
8};
9use redis::AsyncCommands as _;
10use spacegate_model::{PluginConfig, PluginInstanceId};
11
12use crate::service::Retrieve;
13
14impl<F> Retrieve for Redis<F>
15where
16 F: ConfigFormat + Send + Sync,
17{
18 async fn retrieve_config_item_gateway(&self, gateway_name: &str) -> BoxResult<Option<SgGateway>> {
19 let gateway_config: Option<String> = self.get_con().await?.hget(CONF_GATEWAY_KEY, gateway_name).await?;
20 gateway_config.map(|config| self.format.de::<SgGateway>(config.as_bytes()).map_err(|e| format!("[SG.Config] Gateway Config parse error {}", e).into())).transpose()
21 }
22
23 async fn retrieve_config_item_route(&self, gateway_name: &str, route_name: &str) -> BoxResult<Option<crate::model::SgHttpRoute>> {
24 let http_route_config: Option<String> = self.get_con().await?.hget(format!("{CONF_HTTP_ROUTE_KEY}{}", gateway_name), route_name).await?;
25 http_route_config.map(|config| self.format.de::<SgHttpRoute>(config.as_bytes()).map_err(|e| format!("[SG.Config] Route Config parse error {}", e).into())).transpose()
26 }
27
28 async fn retrieve_config_item_route_names(&self, name: &str) -> BoxResult<Vec<String>> {
29 let http_route_configs: HashMap<String, String> = self.get_con().await?.hgetall(format!("{CONF_HTTP_ROUTE_KEY}{}", name)).await?;
30
31 Ok(http_route_configs.into_keys().collect())
32 }
33
34 async fn retrieve_config_names(&self) -> BoxResult<Vec<String>> {
35 let gateway_configs: HashMap<String, String> = self.get_con().await?.hgetall(CONF_GATEWAY_KEY).await?;
36
37 let gateway_configs = gateway_configs
38 .into_values()
39 .map(|v| self.format.de(v.as_bytes()).map_err(|e| format!("[SG.Config] Gateway Config parse error {}", e).into()))
40 .collect::<BoxResult<Vec<SgGateway>>>()?;
41
42 let gateway_names = gateway_configs.into_iter().map(|g| g.name).collect();
43 Ok(gateway_names)
44 }
45
46 async fn retrieve_all_plugins(&self) -> BoxResult<Vec<PluginConfig>> {
47 let plugin_configs: HashMap<String, String> = self.get_con().await?.hgetall(CONF_PLUGIN_KEY).await?;
48
49 let plugin_configs = plugin_configs
50 .into_values()
51 .map(|v| self.format.de(v.as_bytes()).map_err(|e| format!("[SG.Config] Plugin Config parse error {}", e).into()))
52 .collect::<BoxResult<Vec<PluginConfig>>>()?;
53 Ok(plugin_configs)
54 }
55
56 async fn retrieve_plugin(&self, id: &PluginInstanceId) -> BoxResult<Option<PluginConfig>> {
57 let plugin_config: Option<String> = self.get_con().await?.hget(CONF_PLUGIN_KEY, id.to_string()).await?;
58 plugin_config.map(|config| self.format.de::<PluginConfig>(config.as_bytes()).map_err(|e| format!("[SG.Config] Plugin Config parse error {}", e).into())).transpose()
59 }
60
61 async fn retrieve_plugins_by_code(&self, code: &str) -> Result<Vec<PluginConfig>, spacegate_model::BoxError> {
62 let plugin_configs: HashMap<String, String> = self.get_con().await?.hgetall(CONF_PLUGIN_KEY).await?;
63
64 let plugin_configs = plugin_configs
65 .into_values()
66 .filter(|key| key.starts_with(code))
67 .filter_map(|v| self.format.de(v.as_bytes()).ok().filter(|c: &PluginConfig| c.id.code == code))
68 .collect::<Vec<PluginConfig>>();
69 Ok(plugin_configs)
70 }
71}