Skip to content

Commit

Permalink
feature: validate presets before starting a server
Browse files Browse the repository at this point in the history
  • Loading branch information
shakyShane committed Nov 25, 2018
1 parent abf631d commit b6ce59d
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/lib/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ pub enum ProgramStartError {
ConfigCliError(ConfigError),
InvalidArgs(Error),
FromFile(FromFileError),
PresetOptions {
name: String,
error: String
},
PresetNotSupported {
name: String,
},
Presets(Vec<ProgramStartError>),
Ip,
BindHttp(std::io::Error),
BindHttps(std::io::Error),
Expand Down Expand Up @@ -93,6 +101,24 @@ impl std::fmt::Display for ProgramStartError {
ProgramStartError::Ip => {
write!(f, "could not retrieve the address for the config-server")
}
ProgramStartError::PresetOptions {
name,
error
} => {
write!(f, "preset {} could not be parsed\nerror: {}", name, error)
}
ProgramStartError::PresetNotSupported {
name,
} => {
write!(f, "preset {} is not currently supported", name)
}
ProgramStartError::Presets(errors) => {
let res = errors.iter()
.map(|x| x.to_string())
.collect::<Vec<String>>()
.join("\n");
write!(f, "{}", res)
}
}
}
}
36 changes: 36 additions & 0 deletions src/lib/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use serde_json;
use std::collections::HashMap;
use std::sync::Arc;
use std::sync::Mutex;
use config::ProgramStartError;

pub type PresetsMap = HashMap<usize, Box<Preset<AppState>>>;

Expand All @@ -36,6 +37,41 @@ pub fn apply_presets(
app.default_resource(|r| r.f(proxy_transform))
}

pub fn validate_presets(presets: Vec<&str>, program_config: &ProgramConfig) -> Result<(), ProgramStartError> {
let mut errors = vec![];
program_config.presets.iter().enumerate().for_each(|(i, preset)| {
match preset.name.as_str() {
"m2" => {
let preset_opts: Result<M2PresetOptions, serde_json::Error>
= serde_json::from_value(preset.options.clone());

match preset_opts {
Err(e) => {
errors.push(ProgramStartError::PresetOptions {
error: e.to_string(),
name: "m2".to_string()
})
},
_ => {}
}
}
_ => {
errors.push(
ProgramStartError::PresetNotSupported {
name: "m2".to_string()
}
);
},
}
});

if errors.len() > 0 {
Err(ProgramStartError::Presets(errors))
} else {
Ok(())
}
}

pub fn state_and_presets(
opts: &ProgramOptions,
program_config: &ProgramConfig,
Expand Down
6 changes: 6 additions & 0 deletions src/lib/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use setup::state_and_presets;
use ssl;
use std::net::SocketAddr;
use std::net::SocketAddrV4;
use setup::validate_presets;

pub fn create(opts: ProgramOptions) -> Result<(actix::SystemRunner, String), ProgramStartError> {
//
Expand Down Expand Up @@ -41,6 +42,11 @@ pub fn create(opts: ProgramOptions) -> Result<(actix::SystemRunner, String), Pro
//
let maybe_seed = server_opts.seed_file.clone();

//
// Exit early if any presets fail validation
//
let _validated_presets = validate_presets(vec!["m2"], &program_config)?;

//
// Now start the server
//
Expand Down

0 comments on commit b6ce59d

Please sign in to comment.