spacegate_config/service/fs/
update.rs

1use spacegate_model::{Config, ConfigItem, SgGateway, SgHttpRoute};
2
3use crate::{
4    service::{config_format::ConfigFormat, Update},
5    BoxError,
6};
7
8use super::Fs;
9
10impl<F> Update for Fs<F>
11where
12    F: ConfigFormat + Send + Sync,
13{
14    async fn update_plugin(&self, id: &spacegate_model::PluginInstanceId, value: serde_json::Value) -> Result<(), BoxError> {
15        self.modify_cached(|config| {
16            if let Some(prev_spec) = config.plugins.get_mut(id) {
17                *prev_spec = value;
18                Ok(())
19            } else {
20                Err("plugin not exists".into())
21            }
22        })
23        .await
24    }
25    async fn update_config_item_gateway(&self, gateway_name: &str, gateway: SgGateway) -> Result<(), BoxError> {
26        self.modify_cached(|config| {
27            if let Some(prev_item) = config.gateways.get_mut(gateway_name) {
28                prev_item.gateway = gateway;
29                Ok(())
30            } else {
31                Err("item not exists".into())
32            }
33        })
34        .await
35    }
36    async fn update_config_item_route(&self, gateway_name: &str, route_name: &str, route: SgHttpRoute) -> Result<(), BoxError> {
37        self.modify_cached(|config| {
38            if let Some(prev_item) = config.gateways.get_mut(gateway_name) {
39                if let Some(prev_route) = prev_item.routes.get_mut(route_name) {
40                    *prev_route = route;
41                    Ok(())
42                } else {
43                    Err("route not exists".into())
44                }
45            } else {
46                Err("item not exists".into())
47            }
48        })
49        .await
50    }
51
52    async fn update_config_item(&self, gateway_name: &str, item: ConfigItem) -> Result<(), BoxError> {
53        self.modify_cached(|config| {
54            if let Some(prev_item) = config.gateways.get_mut(gateway_name) {
55                *prev_item = item;
56                Ok(())
57            } else {
58                Err("item not exists".into())
59            }
60        })
61        .await
62    }
63    async fn update_config(&self, config: Config) -> Result<(), BoxError> {
64        self.modify_cached(|prev_config| {
65            *prev_config = config;
66            Ok(())
67        })
68        .await
69    }
70}