Skip to main content

htsget_config/config/
data_server.rs

1//! Data server configuration.
2//!
3
4use crate::config::advanced::auth::AuthConfig;
5use crate::config::advanced::cors::CorsConfig;
6use crate::error::{Error::ParseError, Result};
7use crate::http::TlsServerConfig;
8use crate::storage::file::default_localstorage_addr;
9use serde::{Deserialize, Serialize};
10use std::net::SocketAddr;
11use std::path::{Path, PathBuf};
12
13/// Tagged allow headers for cors config, either Mirror or Any.
14#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
15#[serde(deny_unknown_fields)]
16pub enum DataServerTagged {
17  #[serde(alias = "none", alias = "NONE", alias = "null")]
18  None,
19}
20
21/// Whether the data server is enabled or not.
22#[derive(Serialize, Deserialize, Debug, Clone)]
23#[serde(untagged, deny_unknown_fields)]
24#[allow(clippy::large_enum_variant)]
25pub enum DataServerEnabled {
26  None(DataServerTagged),
27  Some(DataServerConfig),
28}
29
30impl DataServerEnabled {
31  /// Get the data server config, or an error if `None`.
32  pub fn as_data_server_config(&self) -> Result<&DataServerConfig> {
33    if let Self::Some(config) = self {
34      Ok(config)
35    } else {
36      Err(ParseError("expected `None` variant".to_string()))
37    }
38  }
39}
40
41/// Configuration for the htsget server.
42#[derive(Serialize, Deserialize, Debug, Clone)]
43#[serde(default, deny_unknown_fields)]
44pub struct DataServerConfig {
45  addr: SocketAddr,
46  local_path: Option<PathBuf>,
47  #[serde(skip_serializing)]
48  tls: Option<TlsServerConfig>,
49  cors: CorsConfig,
50  #[serde(skip_serializing)]
51  pub(crate) auth: Option<AuthConfig>,
52  ticket_origin: Option<String>,
53}
54
55impl DataServerConfig {
56  /// Create the ticket server config.
57  pub fn new(
58    addr: SocketAddr,
59    local_path: PathBuf,
60    tls: Option<TlsServerConfig>,
61    cors: CorsConfig,
62    auth: Option<AuthConfig>,
63    ticket_origin: Option<String>,
64  ) -> Self {
65    Self {
66      addr,
67      local_path: Some(local_path),
68      tls,
69      cors,
70      auth,
71      ticket_origin,
72    }
73  }
74
75  /// Get the socket address.
76  pub fn addr(&self) -> SocketAddr {
77    self.addr
78  }
79
80  /// Get the local path.
81  pub fn local_path(&self) -> Option<&Path> {
82    self.local_path.as_deref()
83  }
84
85  /// Set the local path.
86  pub fn set_local_path(&mut self, local_path: Option<PathBuf>) {
87    self.local_path = local_path;
88  }
89
90  /// Get the TLS config.
91  pub fn tls(&self) -> Option<&TlsServerConfig> {
92    self.tls.as_ref()
93  }
94
95  /// Get the CORS config.
96  pub fn cors(&self) -> &CorsConfig {
97    &self.cors
98  }
99
100  /// Get the auth config.
101  pub fn auth(&self) -> Option<&AuthConfig> {
102    self.auth.as_ref()
103  }
104
105  /// Get the ticket origin.
106  pub fn ticket_origin(&self) -> Option<String> {
107    self.ticket_origin.clone()
108  }
109
110  /// Set the auth config.
111  pub fn set_auth(&mut self, auth: Option<AuthConfig>) {
112    self.auth = auth;
113  }
114
115  /// Get the owned TLS config.
116  pub fn into_tls(self) -> Option<TlsServerConfig> {
117    self.tls
118  }
119}
120
121impl Default for DataServerConfig {
122  fn default() -> Self {
123    Self {
124      addr: default_localstorage_addr()
125        .parse()
126        .expect("expected valid address"),
127      local_path: None,
128      tls: Default::default(),
129      cors: Default::default(),
130      auth: None,
131      ticket_origin: None,
132    }
133  }
134}
135
136#[cfg(test)]
137mod tests {
138  use super::*;
139  use crate::config::tests::test_serialize_and_deserialize;
140
141  #[test]
142  fn data_server() {
143    test_serialize_and_deserialize(
144      r#"
145      addr = "127.0.0.1:8083"
146      local_path = "path"
147      cors.max_age = 1
148      ticket_origin = "http://example.com"
149      "#,
150      (
151        "127.0.0.1:8083".to_string(),
152        "path".to_string(),
153        1,
154        "http://example.com".to_string(),
155      ),
156      |result: DataServerConfig| {
157        (
158          result.addr().to_string(),
159          result.local_path().unwrap().to_string_lossy().to_string(),
160          result.cors.max_age(),
161          result.ticket_origin.unwrap(),
162        )
163      },
164    );
165  }
166}