spacegate_model/
lib.rs

1pub mod plugin;
2use std::{collections::BTreeMap, fmt::Debug};
3
4pub use plugin::*;
5
6pub mod gateway;
7pub use gateway::*;
8
9pub mod http_route;
10pub use http_route::*;
11use serde::{Deserialize, Serialize};
12
13pub mod route_match;
14
15pub mod constants;
16pub mod ext;
17
18pub type BoxError = Box<dyn std::error::Error + Send + Sync>;
19pub type BoxResult<T> = Result<T, BoxError>;
20
21#[derive(Serialize, Deserialize, Clone)]
22#[cfg_attr(feature = "typegen", derive(ts_rs::TS), ts(export))]
23#[serde(default)]
24pub struct ConfigItem<P = PluginInstanceId> {
25    pub gateway: SgGateway<P>,
26    #[serde(skip_serializing_if = "BTreeMap::is_empty")]
27    pub routes: BTreeMap<String, SgHttpRoute<P>>,
28}
29
30impl<P> ConfigItem<P> {
31    pub fn map_plugins<F, T>(self, mut f: F) -> ConfigItem<T>
32    where
33        F: FnMut(P) -> T,
34    {
35        ConfigItem {
36            gateway: self.gateway.map_plugins(&mut f),
37            routes: self.routes.into_iter().map(|(k, r)| (k, r.map_plugins(&mut f))).collect(),
38        }
39    }
40}
41
42impl<P> Default for ConfigItem<P> {
43    fn default() -> Self {
44        Self {
45            gateway: Default::default(),
46            routes: Default::default(),
47        }
48    }
49}
50
51impl<P: Debug> std::fmt::Debug for ConfigItem<P> {
52    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
53        f.debug_struct("ConfigItem").field("gateway", &self.gateway).field("routes", &self.routes).finish()
54    }
55}
56
57#[derive(Debug, Serialize, Deserialize, Clone)]
58#[cfg_attr(feature = "typegen", derive(ts_rs::TS), ts(export))]
59#[serde(default)]
60pub struct Config {
61    pub gateways: BTreeMap<String, ConfigItem<PluginInstanceId>>,
62    #[cfg_attr(feature = "typegen", ts(as = "crate::plugin::PluginInstanceMapTs"))]
63    pub plugins: PluginInstanceMap,
64    pub api_port: Option<u16>,
65}
66
67#[allow(clippy::derivable_impls)]
68impl Default for Config {
69    fn default() -> Self {
70        Self {
71            gateways: Default::default(),
72            plugins: Default::default(),
73            #[cfg(feature = "ext-axum")]
74            api_port: Some(crate::constants::DEFAULT_API_PORT),
75            #[cfg(not(feature = "ext-axum"))]
76            api_port: None,
77        }
78    }
79}