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
138
139
140
141
142
143
144
145
146
147
148
//! Configuration parsing.

use std::path::Path;
use std::fs;
use std::default::Default;
use std::str::FromStr;

use toml;
use serde::de::{Deserialize, Deserializer, Error};
use human_size;

use crate::models::Duration;

// TODO: additional config
// * next job delay
// * shutdown_timeout
// * keepalive
// https://actix.rs/docs/server/

/// Main application config, typically read from a `.toml` file.
#[derive(Clone, Debug, Default, Deserialize)]
pub struct Config {
    /// Configuration for the application's HTTP server.
    pub server: ServerConfig,

    /// Configuration for connecting to Redis.
    pub redis: RedisConfig,
}

impl Config {
    /// Read configuration from a file into a new Config struct.
    pub fn from_file<P: AsRef<Path>>(path: P) -> Result<Self, String> {
        let path = path.as_ref();
        debug!("Reading configuration from {}", path.display());

        let data = match fs::read_to_string(path) {
            Ok(data) => data,
            Err(err) => return Err(err.to_string()),
        };

        let conf: Config = match toml::from_str(&data) {
            Ok(conf) => conf,
            Err(err) => return Err(err.to_string()),
        };

        Ok(conf)
    }

    /// Get the address for the HTTP server to listen on.
    pub fn server_addr(&self) -> String {
        format!("{}:{}", self.server.host, self.server.port)
    }

    pub fn redis_url(&self) -> &str {
        &self.redis.url
    }
}

/// Configuration for the application's HTTP server.
#[derive(Clone, Debug, Deserialize)]
#[serde(default)]
pub struct ServerConfig {
    /// Host address to listen on. Defaults to "127.0.0.1" if not specified.
    pub host: String,

    /// Port to listen on. Defaults to 8023 if not specified.
    pub port: u16,

    /// Number of HTTP worker threads. Defaults to number of CPUs if not specified.
    pub threads: Option<usize>,

    /// Maximum size in bytes for HTTP POST requests. Defaults to "256kB" if not specified.
    #[serde(deserialize_with = "deserialize_human_size")]
    pub max_body_size: Option<usize>,

    /// Determines how often running tasks are checked for timeouts. Defaults to "30s" if not specified.
    pub timeout_check_interval: Duration,

    /// Determines how often failed tasks are checked for retrying. Defaults to "60s" if not specified.
    pub retry_check_interval: Duration,

    /// Determines how often ended tasks are checked for expiry. Defaults to "5m" if not specified.
    pub expiry_check_interval: Duration,

    pub next_job_delay: Option<Duration>,

    #[serde(deserialize_with = "deserialize_log_level")]
    pub log_level: log::Level,
}

fn deserialize_human_size<'de, D: Deserializer<'de>>(deserializer: D) -> Result<Option<usize>, D::Error> {
    let s: Option<&str> = Deserialize::deserialize(deserializer)?;
    Ok(match s {
        Some(s) => {
            let size: human_size::SpecificSize<human_size::Byte> = match s.parse() {
                Ok(size) => size,
                Err(_) => return Err(Error::custom(format!("Unable to parse size '{}'", s))),
            };
            Some(size.value() as usize)
        },
        None => None,
    })
}

fn deserialize_log_level<'de, D: Deserializer<'de>>(deserializer: D) -> Result<log::Level, D::Error> {
    let s: &str = Deserialize::deserialize(deserializer)?;
    match log::Level::from_str(s) {
        Ok(level) => Ok(level),
        Err(_) => Err(Error::custom(format!("Invalid log level: {}", s))),
    }
}

impl Default for ServerConfig {
    fn default() -> Self {
        ServerConfig {
            host: "127.0.0.1".to_owned(),
            port: 8023,
            threads: None,
            max_body_size: None,
            timeout_check_interval: Duration::from_secs(30),
            retry_check_interval: Duration::from_secs(60),
            expiry_check_interval: Duration::from_secs(300),
            next_job_delay: None,
            log_level: log::Level::Info,
        }
    }
}

/// Configuration for connecting to Redis.
#[derive(Clone, Debug, Deserialize)]
#[serde(default)]
pub struct RedisConfig {
    /// Redis URL to connect to. Defaults to "redis://127.0.0.1".
    pub url: String,

    // TODO: determine sensible default
    /// Number of connections to Redis that will be spawned.
    pub threads: Option<usize>,
}

impl Default for RedisConfig {
    fn default() -> Self {
        RedisConfig {
            url: "redis://127.0.0.1".to_owned(),
            threads: None,
        }
    }
}