Skip to main content

nado_sdk/utils/
client_mode.rs

1use crate::utils::deployment::Deployment;
2use include_dir::{include_dir, Dir};
3use std::fs;
4use std::path::Path;
5
6pub static CONFIGS: Dir = include_dir!("$CARGO_MANIFEST_DIR/src/nado_utils/configs");
7#[derive(Clone, PartialEq)]
8pub enum ClientMode {
9    Test,
10    Prod,
11    Local,
12    LocalAlt,
13}
14
15impl ClientMode {
16    fn get_domain() -> &'static str {
17        let use_backup = std::env::var("USE_BACKUP_PUBLIC_BACKEND")
18            .unwrap_or_else(|_| "false".to_string())
19            .to_lowercase()
20            == "true";
21        if use_backup {
22            "nado-backend.xyz"
23        } else {
24            "nado.xyz"
25        }
26    }
27
28    pub fn default_gateway_url(&self) -> String {
29        let envtag = self.nado_envtag();
30        let domain = Self::get_domain();
31        match self {
32            Self::Local | Self::LocalAlt => {
33                format!("http://gateway.{envtag}.{domain}:80/v1")
34            }
35            _ => {
36                format!("https://gateway.{envtag}.{domain}/v1")
37            }
38        }
39    }
40
41    pub fn default_trigger_url(&self) -> String {
42        let envtag = self.nado_envtag();
43        let domain = Self::get_domain();
44        match self {
45            Self::Local | Self::LocalAlt => {
46                format!("http://trigger.{envtag}.{domain}:8080/v1")
47            }
48            _ => {
49                format!("https://trigger.{envtag}.{domain}/v1")
50            }
51        }
52    }
53
54    pub fn default_archive_url(&self) -> String {
55        let envtag = self.nado_envtag();
56        let domain = Self::get_domain();
57        match self {
58            Self::Local | Self::LocalAlt => {
59                format!("http://archive.{envtag}.{domain}:8000/v1")
60            }
61            _ => {
62                format!("https://archive.{envtag}.{domain}/v1")
63            }
64        }
65    }
66
67    pub fn default_gateway_ws_url(&self) -> String {
68        let envtag = self.nado_envtag();
69        let domain = Self::get_domain();
70        match self {
71            Self::Local | Self::LocalAlt => {
72                format!("ws://gateway.{envtag}.{domain}:80/ws")
73            }
74            _ => {
75                format!("wss://gateway.{envtag}.{domain}/ws")
76            }
77        }
78    }
79
80    pub fn private_gateway_ws_url(&self) -> String {
81        let envtag = self.nado_envtag();
82        match self {
83            Self::Local | Self::LocalAlt => {
84                format!("ws://{envtag}-mm.nado-backend.xyz:80/ws")
85            }
86            _ => {
87                format!("wss://{envtag}-mm.nado-backend.xyz/ws")
88            }
89        }
90    }
91
92    pub fn private_gateway_subscription_url(&self) -> String {
93        let envtag = self.nado_envtag();
94        match self {
95            Self::Local | Self::LocalAlt => {
96                format!("ws://{envtag}-mm.nado-backend.xyz:80/subscribe")
97            }
98            _ => {
99                format!("wss://{envtag}-mm.nado-backend.xyz/subscribe")
100            }
101        }
102    }
103
104    pub fn private_gateway_url(&self) -> String {
105        let envtag = self.nado_envtag();
106        match self {
107            Self::Local | Self::LocalAlt => {
108                format!("http://{envtag}-mm.nado-backend.xyz:80")
109            }
110            _ => {
111                format!("https://{envtag}-mm.nado-backend.xyz")
112            }
113        }
114    }
115
116    pub fn nado_envtag(&self) -> String {
117        match self {
118            Self::Test => "test",
119            Self::Prod => "prod",
120            Self::Local => "local",
121            Self::LocalAlt => "local-alt",
122        }
123        .to_string()
124    }
125
126    pub fn from_envtag(envtag: &str) -> Self {
127        match envtag {
128            "test" => Self::Test,
129            "prod" => Self::Prod,
130            "local" => Self::Local,
131            "local-alt" => Self::LocalAlt,
132            _ => panic!("Unknown envtag: {envtag}"),
133        }
134    }
135
136    pub fn deployment(&self) -> Deployment {
137        let network = self.nado_envtag();
138        let deployment_path = Path::new(&network).join("deployment.json");
139
140        // Order of loading
141        // 1. try ENVVAR override
142        // 2. try locally configured directory
143        // 3. use deployment.json included in binary
144        let deployment_str = match std::env::var("NADO_CONFIGS_DIR") {
145            Ok(path) => {
146                let deployment_path = Path::new(&path).join(&network).join("deployment.json");
147                fs::read_to_string(&deployment_path).unwrap_or_else(|_| {
148                    panic!("Failed to read {}", &deployment_path.to_string_lossy())
149                })
150            }
151            Err(_) => {
152                let package_dir = env!("CARGO_MANIFEST_DIR");
153                let default_configs_path = Path::new(package_dir)
154                    .join("src/nado_utils/configs")
155                    .join(&network)
156                    .join("deployment.json");
157
158                fs::read_to_string(&default_configs_path).unwrap_or_else(|_| {
159                    CONFIGS
160                        .get_file(deployment_path.to_str().unwrap())
161                        .expect("Deployment file not found")
162                        .contents_utf8()
163                        .expect("Failed to read deployment file")
164                        .to_string()
165                })
166            }
167        };
168
169        serde_json::from_str(&deployment_str).unwrap()
170    }
171}