hoprd_api/
config.rs

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
use std::str::FromStr;

use hopr_lib::HostConfig;
use serde::{Deserialize, Serialize};
use validator::{Validate, ValidationError};

pub const DEFAULT_API_HOST: &str = "127.0.0.1";
pub const DEFAULT_API_PORT: u16 = 3001;
pub const MINIMAL_API_TOKEN_LENGTH: usize = 8;

fn validate_api_auth(token: &Auth) -> Result<(), ValidationError> {
    match &token {
        Auth::None => Ok(()),
        Auth::Token(token) => {
            if token.len() >= MINIMAL_API_TOKEN_LENGTH {
                Ok(())
            } else {
                Err(ValidationError::new("The API token is too short"))
            }
        }
    }
}

#[derive(Debug, Clone, PartialEq, smart_default::SmartDefault, Serialize, Deserialize)]
pub enum Auth {
    #[default]
    None,
    Token(String),
}

#[derive(Debug, Clone, PartialEq, smart_default::SmartDefault, Validate, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Api {
    /// Selects whether the REST API is enabled
    #[serde(default)]
    pub enable: bool,
    /// Auth enum holding the API auth configuration
    #[validate(custom(function = "validate_api_auth"))]
    #[serde(default)]
    pub auth: Auth,
    /// Host and port combination where the REST API should be located
    #[validate(nested)]
    #[serde(default = "default_api_host")]
    #[default(default_api_host())]
    pub host: HostConfig,
}

#[inline]
fn default_api_host() -> HostConfig {
    HostConfig::from_str(format!("{DEFAULT_API_HOST}:{DEFAULT_API_PORT}").as_str())
        .expect("default credentials should always work")
}