spacegate_config/service/fs/
create.rs

1use spacegate_model::ConfigItem;
2
3use crate::{
4    model::{SgGateway, SgHttpRoute},
5    service::config_format::ConfigFormat,
6    BoxError,
7};
8
9use crate::service::Create;
10
11use super::Fs;
12
13impl<F> Create for Fs<F>
14where
15    F: ConfigFormat + Send + Sync,
16{
17    async fn create_plugin(&self, id: &spacegate_model::PluginInstanceId, value: serde_json::Value) -> Result<(), BoxError> {
18        self.modify_cached(|config| {
19            if config.plugins.get(id).is_some() {
20                return Err("plugin existed".into());
21            }
22            config.plugins.insert(id.clone(), value);
23            Ok(())
24        })
25        .await
26    }
27    async fn create_config_item(&self, gateway_name: &str, item: ConfigItem) -> Result<(), BoxError> {
28        self.modify_cached(|config| {
29            if config.gateways.contains_key(gateway_name) {
30                return Err("item existed".into());
31            }
32            config.gateways.insert(gateway_name.into(), item);
33            Ok(())
34        })
35        .await
36    }
37    async fn create_config_item_gateway(&self, gateway_name: &str, gateway: SgGateway) -> Result<(), BoxError> {
38        self.create_config_item(
39            gateway_name,
40            ConfigItem {
41                gateway,
42                routes: Default::default(),
43            },
44        )
45        .await
46    }
47    async fn create_config_item_route(&self, gateway_name: &str, route_name: &str, route: SgHttpRoute) -> Result<(), BoxError> {
48        self.modify_cached(|config| {
49            if let Some(item) = config.gateways.get_mut(gateway_name) {
50                if item.routes.contains_key(gateway_name) {
51                    Err("route existed".into())
52                } else {
53                    item.routes.insert(route_name.to_string(), route);
54                    Ok(())
55                }
56            } else {
57                Err("gateway not exists".into())
58            }
59        })
60        .await
61    }
62}