2021-07-23 19:52:00 +00:00
|
|
|
use byte_unit::Byte;
|
|
|
|
use config::{self, ConfigError};
|
|
|
|
use std::path::PathBuf;
|
|
|
|
|
|
|
|
/// Configuration values.
|
2021-07-24 10:47:24 +00:00
|
|
|
#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
|
2021-07-23 19:52:00 +00:00
|
|
|
pub struct Config {
|
|
|
|
/// Server configuration.
|
|
|
|
pub server: ServerConfig,
|
|
|
|
/// Paste configuration.
|
|
|
|
pub paste: PasteConfig,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Server configuration.
|
2021-07-24 10:47:24 +00:00
|
|
|
#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
|
2021-07-23 19:52:00 +00:00
|
|
|
pub struct ServerConfig {
|
|
|
|
/// The socket address to bind.
|
|
|
|
pub address: String,
|
|
|
|
/// Number of workers to start.
|
|
|
|
pub workers: Option<usize>,
|
|
|
|
/// Maximum content length.
|
|
|
|
pub max_content_length: Byte,
|
|
|
|
/// Storage path.
|
|
|
|
pub upload_path: PathBuf,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Paste configuration.
|
2021-07-24 10:47:24 +00:00
|
|
|
#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
|
2021-07-23 19:52:00 +00:00
|
|
|
pub struct PasteConfig {
|
2021-07-23 21:01:16 +00:00
|
|
|
/// Pet names configuration.
|
|
|
|
pub pet_names: PetNamesConfig,
|
2021-07-23 20:53:41 +00:00
|
|
|
/// Random string configuration.
|
|
|
|
pub random: RandomConfig,
|
2021-07-23 19:52:00 +00:00
|
|
|
/// Default file extension.
|
|
|
|
pub default_extension: String,
|
|
|
|
}
|
|
|
|
|
2021-07-23 21:01:16 +00:00
|
|
|
/// Pet names configuration.
|
2021-07-24 10:47:24 +00:00
|
|
|
#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
|
2021-07-23 21:01:16 +00:00
|
|
|
pub struct PetNamesConfig {
|
|
|
|
/// Use pet names instead of original file names.
|
|
|
|
pub enabled: bool,
|
|
|
|
/// Count of words that pet name will include.
|
|
|
|
pub words: u8,
|
|
|
|
/// Separator between the words.
|
|
|
|
pub separator: String,
|
|
|
|
}
|
|
|
|
|
2021-07-23 20:53:41 +00:00
|
|
|
/// Random string configuration.
|
2021-07-24 10:47:24 +00:00
|
|
|
#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
|
2021-07-23 20:53:41 +00:00
|
|
|
pub struct RandomConfig {
|
|
|
|
/// Use random strings instead of original file names.
|
|
|
|
pub enabled: bool,
|
|
|
|
/// Length of the random string to generate.
|
2021-07-23 21:01:16 +00:00
|
|
|
pub length: usize,
|
2021-07-23 20:53:41 +00:00
|
|
|
}
|
|
|
|
|
2021-07-23 19:52:00 +00:00
|
|
|
impl Config {
|
|
|
|
/// Parses the config file and returns the values.
|
|
|
|
pub fn parse(file_name: &str) -> Result<Config, ConfigError> {
|
|
|
|
let mut config = config::Config::default();
|
|
|
|
config
|
|
|
|
.merge(config::File::with_name(file_name))?
|
|
|
|
.merge(config::Environment::with_prefix(env!("CARGO_PKG_NAME")).separator("__"))?;
|
|
|
|
config.try_into()
|
|
|
|
}
|
|
|
|
}
|
2021-07-24 10:47:24 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::*;
|
|
|
|
use std::env;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_parse_config() -> Result<(), ConfigError> {
|
|
|
|
let file_name = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
|
|
|
|
.join("config.toml")
|
|
|
|
.to_str()
|
|
|
|
.unwrap()
|
|
|
|
.to_string();
|
|
|
|
env::set_var(
|
|
|
|
format!("{}_SERVER__ADDRESS", env!("CARGO_PKG_NAME")),
|
|
|
|
"0.0.1.1",
|
|
|
|
);
|
|
|
|
let config = Config::parse(&file_name)?;
|
|
|
|
assert_eq!("0.0.1.1", config.server.address);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|