Skip to main content

dfx_core/config/model/
canister_http_adapter.rs

1use schemars::JsonSchema;
2use serde::{Deserialize, Serialize};
3use std::{path::PathBuf, str::FromStr};
4
5// These definitions come from https://gitlab.com/dfinity-lab/public/ic/-/blob/master/rs/canister_http/adapter/src/config.rs
6#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq, Default)]
7/// The source of the unix domain socket to be used for inter-process
8/// communication.
9pub enum IncomingSource {
10    /// We use systemd's created socket.
11    #[default]
12    Systemd,
13    /// We use the corresponing path as socket.
14    Path(PathBuf),
15}
16
17/// Represents the log level of the HTTP adapter.
18#[derive(Clone, Debug, Serialize, Deserialize, Copy, PartialEq, Eq, JsonSchema, Default)]
19#[serde(rename_all = "snake_case")]
20pub enum HttpAdapterLogLevel {
21    Critical,
22    #[default]
23    Error,
24    Warning,
25    Info,
26    Debug,
27    Trace,
28}
29
30impl FromStr for HttpAdapterLogLevel {
31    type Err = String;
32
33    fn from_str(input: &str) -> Result<HttpAdapterLogLevel, Self::Err> {
34        match input {
35            "critical" => Ok(HttpAdapterLogLevel::Critical),
36            "error" => Ok(HttpAdapterLogLevel::Error),
37            "warning" => Ok(HttpAdapterLogLevel::Warning),
38            "info" => Ok(HttpAdapterLogLevel::Info),
39            "debug" => Ok(HttpAdapterLogLevel::Debug),
40            "trace" => Ok(HttpAdapterLogLevel::Trace),
41            other => Err(format!("Unknown log level: {other}")),
42        }
43    }
44}
45
46#[derive(Clone, Debug, Deserialize, Serialize)]
47pub struct LoggerConfig {
48    pub level: HttpAdapterLogLevel,
49}
50
51/// This struct contains configuration options for the Canister HTTP Adapter.
52#[derive(Clone, Debug, Deserialize, Serialize)]
53pub struct Config {
54    /// Specifies which unix domain socket should be used for serving incoming requests.
55    #[serde(default)]
56    pub incoming_source: IncomingSource,
57
58    pub logger: LoggerConfig,
59}
60
61impl Config {
62    pub fn new(uds_path: PathBuf, log_level: HttpAdapterLogLevel) -> Config {
63        Config {
64            incoming_source: IncomingSource::Path(uds_path),
65            logger: LoggerConfig { level: log_level },
66        }
67    }
68
69    pub fn get_socket_path(&self) -> Option<PathBuf> {
70        match &self.incoming_source {
71            IncomingSource::Systemd => None,
72            IncomingSource::Path(path) => Some(path.clone()),
73        }
74    }
75}