scouter_settings/
storage.rs

1use crate::ScouterServerConfig;
2use scouter_types::StorageType;
3use serde::Serialize;
4use std::path::PathBuf;
5
6#[derive(Debug, Clone, Serialize)]
7pub struct ObjectStorageSettings {
8    pub storage_uri: String,
9    pub storage_type: StorageType,
10    pub region: String, // this is aws specific
11}
12
13impl Default for ObjectStorageSettings {
14    fn default() -> Self {
15        let storage_uri = std::env::var("SCOUTER_STORAGE_URI")
16            .unwrap_or_else(|_| "./scouter_storage".to_string());
17
18        let storage_type = ScouterServerConfig::get_storage_type(&storage_uri);
19
20        // need to set this for aws objectstore
21        let region = std::env::var("AWS_REGION").unwrap_or_else(|_| "us-east-1".to_string());
22
23        Self {
24            storage_uri,
25            storage_type,
26            region,
27        }
28    }
29}
30
31impl ObjectStorageSettings {
32    pub fn storage_root(&self) -> String {
33        match self.storage_type {
34            StorageType::Google | StorageType::Aws | StorageType::Azure => {
35                if let Some(stripped) = self.storage_uri.strip_prefix("gs://") {
36                    stripped.to_string()
37                } else if let Some(stripped) = self.storage_uri.strip_prefix("s3://") {
38                    stripped.to_string()
39                } else if let Some(stripped) = self.storage_uri.strip_prefix("az://") {
40                    stripped.to_string()
41                } else {
42                    self.storage_uri.clone()
43                }
44            }
45            StorageType::Local => {
46                // For local storage, just return the path directly
47                self.storage_uri.clone()
48            }
49        }
50    }
51
52    pub fn canonicalized_path(&self) -> String {
53        // if registry is local canonicalize the path
54        if self.storage_type == StorageType::Local {
55            let path = PathBuf::from(&self.storage_uri);
56            if path.exists() {
57                path.canonicalize()
58                    .unwrap_or_else(|_| path.clone())
59                    .to_str()
60                    .unwrap()
61                    .to_string()
62            } else {
63                self.storage_uri.clone()
64            }
65        } else {
66            self.storage_uri.clone()
67        }
68    }
69}