You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
59 lines
1.3 KiB
59 lines
1.3 KiB
pub mod loglevel;
|
|
|
|
use std::default::Default;
|
|
use std::fs::File;
|
|
use std::io::Read;
|
|
use std::path::{Path, PathBuf};
|
|
|
|
use gotham_derive::StateData;
|
|
use serde_derive::Deserialize;
|
|
use toml;
|
|
|
|
pub use crate::config::loglevel::LogLevel;
|
|
use crate::result::Result;
|
|
|
|
#[derive(Clone, Debug, Deserialize, StateData)]
|
|
pub struct Config {
|
|
#[serde(default)]
|
|
pub address: String,
|
|
#[serde(default)]
|
|
pub url: String,
|
|
#[serde(default)]
|
|
pub log_level: LogLevel,
|
|
#[serde(default)]
|
|
pub log_file: Option<String>,
|
|
#[serde(default)]
|
|
pub data_directory: PathBuf,
|
|
#[serde(default)]
|
|
pub template_directory: PathBuf,
|
|
#[serde(default)]
|
|
pub static_directory: PathBuf,
|
|
}
|
|
|
|
impl Default for Config {
|
|
fn default() -> Self {
|
|
Self {
|
|
address: "127.0.0.1:9214".into(),
|
|
url: "http://127.0.0.1:9214".into(),
|
|
log_level: LogLevel::info,
|
|
log_file: None,
|
|
data_directory: PathBuf::from("data"),
|
|
template_directory: PathBuf::from("templates"),
|
|
static_directory: PathBuf::from("static"),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Config {
|
|
pub fn load<T: AsRef<Path>>(path: T) -> Result<Config> {
|
|
if let Ok(mut file) = File::open(path.as_ref()) {
|
|
let mut buf = String::new();
|
|
file.read_to_string(&mut buf)?;
|
|
let config: Config = toml::from_str(&buf)?;
|
|
Ok(config)
|
|
} else {
|
|
Ok(Config::default())
|
|
}
|
|
}
|
|
}
|