1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
use std::str::FromStr;

use http::uri::Authority;
use serde::{Deserialize, Serialize};

use crate::config::{
  default_localstorage_addr, default_path, default_serve_at, DataServerConfig, KeyPairScheme,
};
use crate::types::Scheme;

fn default_authority() -> Authority {
  Authority::from_static(default_localstorage_addr())
}

fn default_local_path() -> String {
  default_path().into()
}

fn default_path_prefix() -> String {
  default_serve_at().into()
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct LocalStorage {
  #[serde(default)]
  scheme: Scheme,
  #[serde(with = "http_serde::authority", default = "default_authority")]
  authority: Authority,
  #[serde(default = "default_local_path")]
  local_path: String,
  #[serde(default = "default_path_prefix")]
  path_prefix: String,
}

impl LocalStorage {
  /// Create a new local storage.
  pub fn new(
    scheme: Scheme,
    authority: Authority,
    local_path: String,
    path_prefix: String,
  ) -> Self {
    Self {
      scheme,
      authority,
      local_path,
      path_prefix,
    }
  }

  /// Get the scheme.
  pub fn scheme(&self) -> Scheme {
    self.scheme
  }

  /// Get the authority.
  pub fn authority(&self) -> &Authority {
    &self.authority
  }

  /// Get the local path.
  pub fn local_path(&self) -> &str {
    &self.local_path
  }

  /// Get the path prefix.
  pub fn path_prefix(&self) -> &str {
    &self.path_prefix
  }
}

impl From<&DataServerConfig> for Option<LocalStorage> {
  fn from(config: &DataServerConfig) -> Self {
    Some(LocalStorage::new(
      config.tls().get_scheme(),
      Authority::from_str(&config.addr().to_string()).ok()?,
      config.local_path().to_str()?.to_string(),
      config.serve_at().to_string(),
    ))
  }
}

#[cfg(test)]
mod tests {
  use std::net::SocketAddr;
  use std::path::PathBuf;

  use crate::config::cors::CorsConfig;
  use crate::config::tests::test_config_from_file;
  use crate::storage::Storage;
  use crate::types::Scheme::Http;

  use super::*;

  #[test]
  fn config_storage_local_file() {
    test_config_from_file(
      r#"
        [[resolvers]]
        regex = "regex"

        [resolvers.storage]
        local_path = "path"
        scheme = "HTTPS"
        path_prefix = "path"
        "#,
      |config| {
        println!("{:?}", config.resolvers().first().unwrap().storage());
        assert!(matches!(
            config.resolvers().first().unwrap().storage(),
            Storage::Local { local_storage } if local_storage.local_path() == "path" && local_storage.scheme() == Scheme::Https && local_storage.path_prefix() == "path"
        ));
      },
    );
  }

  #[test]
  fn local_storage_from_data_server_config() {
    let data_server_config = DataServerConfig::new(
      true,
      SocketAddr::from_str("127.0.0.1:8080").unwrap(),
      PathBuf::from("data"),
      "/data".to_string(),
      None,
      CorsConfig::default(),
    );
    let result: Option<LocalStorage> = (&data_server_config).into();
    let expected = LocalStorage::new(
      Http,
      Authority::from_static("127.0.0.1:8080"),
      "data".to_string(),
      "/data".to_string(),
    );

    assert_eq!(result.unwrap(), expected);
  }
}