spacegate_config/service/fs/
discovery.rs

1use spacegate_model::BackendHost;
2
3use crate::service::{config_format::ConfigFormat, Discovery, Instance};
4
5use super::Fs;
6pub struct LocalGateway {
7    uri: String,
8}
9
10impl LocalGateway {
11    pub fn new(port: u16) -> Self {
12        Self {
13            uri: format!("localhost:{}", port),
14        }
15    }
16}
17
18impl Instance for LocalGateway {
19    fn api_url(&self) -> &str {
20        &self.uri
21    }
22    fn id(&self) -> &str {
23        "local"
24    }
25}
26impl<F: ConfigFormat + Send + Sync + 'static> Discovery for Fs<F> {
27    async fn instances(&self) -> Result<Vec<impl Instance>, spacegate_model::BoxError> {
28        self.retrieve_cached(|c| c.api_port.map(LocalGateway::new).into_iter().collect()).await
29    }
30    #[cfg(target_os = "linux")]
31    async fn backends(&self) -> Result<Vec<BackendHost>, spacegate_model::BoxError> {
32        // read /var/www
33        let mut dir = tokio::fs::read_dir("/var/www").await?;
34        let mut collector = vec![];
35        while let Ok(Some(entry)) = dir.next_entry().await {
36            if entry.path().is_dir() {
37                if let Some(path) = entry.path().to_str() {
38                    collector.push(BackendHost::File { path: path.to_string() })
39                }
40            }
41        }
42        Ok(collector)
43    }
44}