dfx_core/config/model/
canister_http_adapter.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use std::{path::PathBuf, str::FromStr};

// These definitions come from https://gitlab.com/dfinity-lab/public/ic/-/blob/master/rs/canister_http/adapter/src/config.rs
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq, Default)]
/// The source of the unix domain socket to be used for inter-process
/// communication.
pub enum IncomingSource {
    /// We use systemd's created socket.
    #[default]
    Systemd,
    /// We use the corresponing path as socket.
    Path(PathBuf),
}

/// Represents the log level of the HTTP adapter.
#[derive(Clone, Debug, Serialize, Deserialize, Copy, PartialEq, Eq, JsonSchema, Default)]
#[serde(rename_all = "snake_case")]
pub enum HttpAdapterLogLevel {
    Critical,
    #[default]
    Error,
    Warning,
    Info,
    Debug,
    Trace,
}

impl FromStr for HttpAdapterLogLevel {
    type Err = String;

    fn from_str(input: &str) -> Result<HttpAdapterLogLevel, Self::Err> {
        match input {
            "critical" => Ok(HttpAdapterLogLevel::Critical),
            "error" => Ok(HttpAdapterLogLevel::Error),
            "warning" => Ok(HttpAdapterLogLevel::Warning),
            "info" => Ok(HttpAdapterLogLevel::Info),
            "debug" => Ok(HttpAdapterLogLevel::Debug),
            "trace" => Ok(HttpAdapterLogLevel::Trace),
            other => Err(format!("Unknown log level: {}", other)),
        }
    }
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct LoggerConfig {
    pub level: HttpAdapterLogLevel,
}

/// This struct contains configuration options for the Canister HTTP Adapter.
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Config {
    /// Specifies which unix domain socket should be used for serving incoming requests.
    #[serde(default)]
    pub incoming_source: IncomingSource,

    pub logger: LoggerConfig,
}

impl Config {
    pub fn new(uds_path: PathBuf, log_level: HttpAdapterLogLevel) -> Config {
        Config {
            incoming_source: IncomingSource::Path(uds_path),
            logger: LoggerConfig { level: log_level },
        }
    }

    pub fn get_socket_path(&self) -> Option<PathBuf> {
        match &self.incoming_source {
            IncomingSource::Systemd => None,
            IncomingSource::Path(path) => Some(path.clone()),
        }
    }
}