mirror of https://github.com/rwf2/Rocket.git
parent
463df9d1df
commit
2ad508ed96
|
@ -10,3 +10,6 @@ hyper = { version = "^0.9", default-features = false }
|
|||
url = "^1"
|
||||
# mime = "^0.2"
|
||||
toml = "^0.2"
|
||||
|
||||
[dev-dependencies]
|
||||
lazy_static = "*"
|
||||
|
|
|
@ -1,12 +1,13 @@
|
|||
use std::collections::HashMap;
|
||||
use std::net::ToSocketAddrs;
|
||||
|
||||
use super::Environment::*;
|
||||
use super::Environment;
|
||||
|
||||
use logger::LoggingLevel;
|
||||
use toml::Value;
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
#[derive(Debug)]
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub struct Config {
|
||||
pub address: String,
|
||||
pub port: usize,
|
||||
|
@ -66,9 +67,21 @@ impl Config {
|
|||
|
||||
pub fn set(&mut self, name: &str, value: &Value) -> Result<(), &'static str> {
|
||||
if name == "address" {
|
||||
self.address = parse!(value, as_str).to_string();
|
||||
let address_str = parse!(value, as_str).to_string();
|
||||
if address_str.contains(":") {
|
||||
return Err("an IP address with no port")
|
||||
} else if format!("{}:{}", address_str, 80).to_socket_addrs().is_err() {
|
||||
return Err("a valid IP address")
|
||||
}
|
||||
|
||||
self.address = address_str;
|
||||
} else if name == "port" {
|
||||
self.port = parse!(value, as_integer) as usize;
|
||||
let port = parse!(value, as_integer);
|
||||
if port < 0 {
|
||||
return Err("an unsigned integer");
|
||||
}
|
||||
|
||||
self.port = port as usize;
|
||||
} else if name == "session_key" {
|
||||
let key = parse!(value, as_str);
|
||||
if key.len() != 32 {
|
||||
|
|
|
@ -6,7 +6,7 @@ use std::env;
|
|||
|
||||
use self::Environment::*;
|
||||
|
||||
const CONFIG_ENV: &'static str = "ROCKET_ENV";
|
||||
pub const CONFIG_ENV: &'static str = "ROCKET_ENV";
|
||||
|
||||
#[derive(Hash, PartialEq, Eq, Debug, Clone, Copy)]
|
||||
pub enum Environment {
|
||||
|
|
|
@ -3,7 +3,7 @@ use super::Environment;
|
|||
use term_painter::Color::White;
|
||||
use term_painter::ToStyle;
|
||||
|
||||
#[derive(Debug)]
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub struct ParsingError {
|
||||
pub byte_range: (usize, usize),
|
||||
pub start: (usize, usize),
|
||||
|
@ -11,7 +11,7 @@ pub struct ParsingError {
|
|||
pub desc: String,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub enum ConfigError {
|
||||
BadCWD,
|
||||
NotFound,
|
||||
|
|
|
@ -19,7 +19,7 @@ use logger::{self, LoggingLevel};
|
|||
|
||||
const CONFIG_FILENAME: &'static str = "Rocket.toml";
|
||||
|
||||
#[derive(Debug)]
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub struct RocketConfig {
|
||||
pub active_env: Environment,
|
||||
config: HashMap<Environment, Config>,
|
||||
|
@ -134,6 +134,21 @@ impl RocketConfig {
|
|||
}
|
||||
}
|
||||
|
||||
impl Default for RocketConfig {
|
||||
fn default() -> RocketConfig {
|
||||
RocketConfig {
|
||||
active_env: Environment::Development,
|
||||
config: {
|
||||
let mut default_config = HashMap::new();
|
||||
default_config.insert(Development, Config::default_for(Development));
|
||||
default_config.insert(Staging, Config::default_for(Staging));
|
||||
default_config.insert(Production, Config::default_for(Production));
|
||||
default_config
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Read the Rocket config file from the current directory or any of its
|
||||
/// parents. If there is no such file, return the default config. A returned
|
||||
/// config will have its active environment set to whatever was passed in with
|
||||
|
@ -158,17 +173,370 @@ pub fn read_or_default() -> RocketConfig {
|
|||
})
|
||||
}
|
||||
|
||||
impl Default for RocketConfig {
|
||||
fn default() -> RocketConfig {
|
||||
RocketConfig {
|
||||
active_env: Environment::Development,
|
||||
config: {
|
||||
let mut default_config = HashMap::new();
|
||||
default_config.insert(Development, Config::default_for(Development));
|
||||
default_config.insert(Staging, Config::default_for(Staging));
|
||||
default_config.insert(Production, Config::default_for(Production));
|
||||
default_config
|
||||
},
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use std::env;
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Mutex;
|
||||
|
||||
use super::{RocketConfig, CONFIG_FILENAME, ConfigError};
|
||||
use super::environment::CONFIG_ENV;
|
||||
use super::Environment::*;
|
||||
use super::config::Config;
|
||||
|
||||
use ::toml::Value;
|
||||
use ::logger::LoggingLevel;
|
||||
|
||||
lazy_static! {
|
||||
static ref ENV_LOCK: Mutex<usize> = Mutex::new(0);
|
||||
}
|
||||
|
||||
macro_rules! check_config {
|
||||
($rconfig:expr => { $($param:tt)+ }) => (
|
||||
match $rconfig {
|
||||
Ok(config) => assert_eq!(config.active(), &Config { $($param)+ }),
|
||||
Err(e) => panic!("Config {} failed: {:?}", stringify!($rconfig), e)
|
||||
}
|
||||
);
|
||||
|
||||
($rconfig:expr, $econfig:expr) => (
|
||||
match $rconfig {
|
||||
Ok(config) => assert_eq!(config.active(), &$econfig),
|
||||
Err(e) => panic!("Config {} failed: {:?}", stringify!($rconfig), e)
|
||||
}
|
||||
);
|
||||
|
||||
($env:expr, $rconfig:expr, $econfig:expr) => (
|
||||
match $rconfig.clone() {
|
||||
Ok(config) => assert_eq!(config.get($env), &$econfig),
|
||||
Err(e) => panic!("Config {} failed: {:?}", stringify!($rconfig), e)
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_defaults() {
|
||||
// Take the lock so changing the environment doesn't cause races.
|
||||
let _env_lock = ENV_LOCK.lock().unwrap();
|
||||
|
||||
// First, without an environment. Should get development defaults.
|
||||
env::remove_var(CONFIG_ENV);
|
||||
check_config!(RocketConfig::active_default(), Config::default_for(Development));
|
||||
|
||||
// Now with an explicit dev environment.
|
||||
for env in &["development", "dev"] {
|
||||
env::set_var(CONFIG_ENV, env);
|
||||
check_config!(RocketConfig::active_default(), Config::default_for(Development));
|
||||
}
|
||||
|
||||
// Now staging.
|
||||
for env in &["stage", "staging"] {
|
||||
env::set_var(CONFIG_ENV, env);
|
||||
check_config!(RocketConfig::active_default(), Config::default_for(Staging));
|
||||
}
|
||||
|
||||
// Finally, production.
|
||||
for env in &["prod", "production"] {
|
||||
env::set_var(CONFIG_ENV, env);
|
||||
check_config!(RocketConfig::active_default(), Config::default_for(Production));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bad_environment_vars() {
|
||||
// Take the lock so changing the environment doesn't cause races.
|
||||
let _env_lock = ENV_LOCK.lock().unwrap();
|
||||
|
||||
for env in &["", "p", "pr", "pro", "prodo", " prod", "dev ", "!dev!", "🚀 "] {
|
||||
env::set_var(CONFIG_ENV, env);
|
||||
let err = ConfigError::BadEnv(env.to_string());
|
||||
assert!(RocketConfig::active_default().err().map_or(false, |e| e == err));
|
||||
}
|
||||
|
||||
// Test that a bunch of invalid environment names give the right error.
|
||||
env::remove_var(CONFIG_ENV);
|
||||
for env in &["p", "pr", "pro", "prodo", "bad", "meow", "this", "that"] {
|
||||
let toml_table = format!("[{}]\n", env);
|
||||
let err = ConfigError::BadEntry(env.to_string(), CONFIG_FILENAME.into());
|
||||
assert!(RocketConfig::parse(toml_table, CONFIG_FILENAME)
|
||||
.err().map_or(false, |e| e == err));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_good_full_config_files() {
|
||||
// Take the lock so changing the environment doesn't cause races.
|
||||
let _env_lock = ENV_LOCK.lock().unwrap();
|
||||
env::remove_var(CONFIG_ENV);
|
||||
|
||||
let config_str = r#"
|
||||
address = "1.2.3.4"
|
||||
port = 7810
|
||||
log = "critical"
|
||||
session_key = "01234567890123456789012345678901"
|
||||
template_dir = "mine"
|
||||
json = true
|
||||
pi = 3.14
|
||||
"#;
|
||||
|
||||
let mut extra: HashMap<String, Value> = HashMap::new();
|
||||
extra.insert("template_dir".to_string(), Value::String("mine".into()));
|
||||
extra.insert("json".to_string(), Value::Boolean(true));
|
||||
extra.insert("pi".to_string(), Value::Float(3.14));
|
||||
|
||||
let expected = Config {
|
||||
address: "1.2.3.4".to_string(),
|
||||
port: 7810,
|
||||
log_level: LoggingLevel::Critical,
|
||||
session_key: Some("01234567890123456789012345678901".to_string()),
|
||||
extra: extra
|
||||
};
|
||||
|
||||
let dev_config = ["[dev]", config_str].join("\n");
|
||||
let parsed = RocketConfig::parse(dev_config, CONFIG_FILENAME);
|
||||
check_config!(Development, parsed, expected);
|
||||
check_config!(Staging, parsed, Config::default_for(Staging));
|
||||
check_config!(Production, parsed, Config::default_for(Production));
|
||||
|
||||
let stage_config = ["[stage]", config_str].join("\n");
|
||||
let parsed = RocketConfig::parse(stage_config, CONFIG_FILENAME);
|
||||
check_config!(Staging, parsed, expected);
|
||||
check_config!(Development, parsed, Config::default_for(Development));
|
||||
check_config!(Production, parsed, Config::default_for(Production));
|
||||
|
||||
let prod_config = ["[prod]", config_str].join("\n");
|
||||
let parsed = RocketConfig::parse(prod_config, CONFIG_FILENAME);
|
||||
check_config!(Production, parsed, expected);
|
||||
check_config!(Development, parsed, Config::default_for(Development));
|
||||
check_config!(Staging, parsed, Config::default_for(Staging));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_good_address_values() {
|
||||
// Take the lock so changing the environment doesn't cause races.
|
||||
let _env_lock = ENV_LOCK.lock().unwrap();
|
||||
env::set_var(CONFIG_ENV, "dev");
|
||||
|
||||
check_config!(RocketConfig::parse(r#"
|
||||
[development]
|
||||
address = "localhost"
|
||||
"#.to_string(), CONFIG_FILENAME) => {
|
||||
address: "localhost".to_string(),
|
||||
..Config::default_for(Development)
|
||||
});
|
||||
|
||||
check_config!(RocketConfig::parse(r#"
|
||||
[dev]
|
||||
address = "127.0.0.1"
|
||||
"#.to_string(), CONFIG_FILENAME) => {
|
||||
address: "127.0.0.1".to_string(),
|
||||
..Config::default_for(Development)
|
||||
});
|
||||
|
||||
check_config!(RocketConfig::parse(r#"
|
||||
[dev]
|
||||
address = "0.0.0.0"
|
||||
"#.to_string(), CONFIG_FILENAME) => {
|
||||
address: "0.0.0.0".to_string(),
|
||||
..Config::default_for(Development)
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
#[test]
|
||||
fn test_bad_address_values() {
|
||||
// Take the lock so changing the environment doesn't cause races.
|
||||
let _env_lock = ENV_LOCK.lock().unwrap();
|
||||
env::remove_var(CONFIG_ENV);
|
||||
|
||||
assert!(RocketConfig::parse(r#"
|
||||
[development]
|
||||
address = 0000
|
||||
"#.to_string(), CONFIG_FILENAME).is_err());
|
||||
|
||||
assert!(RocketConfig::parse(r#"
|
||||
[development]
|
||||
address = true
|
||||
"#.to_string(), CONFIG_FILENAME).is_err());
|
||||
|
||||
assert!(RocketConfig::parse(r#"
|
||||
[development]
|
||||
address = "_idk_"
|
||||
"#.to_string(), CONFIG_FILENAME).is_err());
|
||||
|
||||
assert!(RocketConfig::parse(r#"
|
||||
[staging]
|
||||
address = "1.2.3.4:100"
|
||||
"#.to_string(), CONFIG_FILENAME).is_err());
|
||||
|
||||
assert!(RocketConfig::parse(r#"
|
||||
[production]
|
||||
address = "1.2.3.4.5.6"
|
||||
"#.to_string(), CONFIG_FILENAME).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_good_port_values() {
|
||||
// Take the lock so changing the environment doesn't cause races.
|
||||
let _env_lock = ENV_LOCK.lock().unwrap();
|
||||
env::set_var(CONFIG_ENV, "stage");
|
||||
|
||||
check_config!(RocketConfig::parse(r#"
|
||||
[stage]
|
||||
port = 100
|
||||
"#.to_string(), CONFIG_FILENAME) => {
|
||||
port: 100,
|
||||
..Config::default_for(Staging)
|
||||
});
|
||||
|
||||
check_config!(RocketConfig::parse(r#"
|
||||
[stage]
|
||||
port = 6000
|
||||
"#.to_string(), CONFIG_FILENAME) => {
|
||||
port: 6000,
|
||||
..Config::default_for(Staging)
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bad_port_values() {
|
||||
// Take the lock so changing the environment doesn't cause races.
|
||||
let _env_lock = ENV_LOCK.lock().unwrap();
|
||||
env::remove_var(CONFIG_ENV);
|
||||
|
||||
assert!(RocketConfig::parse(r#"
|
||||
[development]
|
||||
port = true
|
||||
"#.to_string(), CONFIG_FILENAME).is_err());
|
||||
|
||||
assert!(RocketConfig::parse(r#"
|
||||
[production]
|
||||
port = "hello"
|
||||
"#.to_string(), CONFIG_FILENAME).is_err());
|
||||
|
||||
assert!(RocketConfig::parse(r#"
|
||||
[staging]
|
||||
port = -1
|
||||
"#.to_string(), CONFIG_FILENAME).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_good_log_levels() {
|
||||
// Take the lock so changing the environment doesn't cause races.
|
||||
let _env_lock = ENV_LOCK.lock().unwrap();
|
||||
env::set_var(CONFIG_ENV, "stage");
|
||||
|
||||
check_config!(RocketConfig::parse(r#"
|
||||
[stage]
|
||||
log = "normal"
|
||||
"#.to_string(), CONFIG_FILENAME) => {
|
||||
log_level: LoggingLevel::Normal,
|
||||
..Config::default_for(Staging)
|
||||
});
|
||||
|
||||
|
||||
check_config!(RocketConfig::parse(r#"
|
||||
[stage]
|
||||
log = "debug"
|
||||
"#.to_string(), CONFIG_FILENAME) => {
|
||||
log_level: LoggingLevel::Debug,
|
||||
..Config::default_for(Staging)
|
||||
});
|
||||
|
||||
check_config!(RocketConfig::parse(r#"
|
||||
[stage]
|
||||
log = "critical"
|
||||
"#.to_string(), CONFIG_FILENAME) => {
|
||||
log_level: LoggingLevel::Critical,
|
||||
..Config::default_for(Staging)
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bad_log_level_values() {
|
||||
// Take the lock so changing the environment doesn't cause races.
|
||||
let _env_lock = ENV_LOCK.lock().unwrap();
|
||||
env::remove_var(CONFIG_ENV);
|
||||
|
||||
assert!(RocketConfig::parse(r#"
|
||||
[dev]
|
||||
log = false
|
||||
"#.to_string(), CONFIG_FILENAME).is_err());
|
||||
|
||||
assert!(RocketConfig::parse(r#"
|
||||
[development]
|
||||
log = 0
|
||||
"#.to_string(), CONFIG_FILENAME).is_err());
|
||||
|
||||
assert!(RocketConfig::parse(r#"
|
||||
[prod]
|
||||
log = "no"
|
||||
"#.to_string(), CONFIG_FILENAME).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_good_session_key() {
|
||||
// Take the lock so changing the environment doesn't cause races.
|
||||
let _env_lock = ENV_LOCK.lock().unwrap();
|
||||
env::set_var(CONFIG_ENV, "stage");
|
||||
|
||||
check_config!(RocketConfig::parse(r#"
|
||||
[stage]
|
||||
session_key = "VheMwXIBygSmOlZAhuWl2B+zgvTN3WW5"
|
||||
"#.to_string(), CONFIG_FILENAME) => {
|
||||
session_key: Some("VheMwXIBygSmOlZAhuWl2B+zgvTN3WW5".into()),
|
||||
..Config::default_for(Staging)
|
||||
});
|
||||
|
||||
check_config!(RocketConfig::parse(r#"
|
||||
[stage]
|
||||
session_key = "adL5fFIPmZBrlyHk2YT4NLV3YCk2gFXz"
|
||||
"#.to_string(), CONFIG_FILENAME) => {
|
||||
session_key: Some("adL5fFIPmZBrlyHk2YT4NLV3YCk2gFXz".into()),
|
||||
..Config::default_for(Staging)
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bad_session_key() {
|
||||
// Take the lock so changing the environment doesn't cause races.
|
||||
let _env_lock = ENV_LOCK.lock().unwrap();
|
||||
env::remove_var(CONFIG_ENV);
|
||||
|
||||
assert!(RocketConfig::parse(r#"
|
||||
[dev]
|
||||
session_key = true
|
||||
"#.to_string(), CONFIG_FILENAME).is_err());
|
||||
|
||||
assert!(RocketConfig::parse(r#"
|
||||
[dev]
|
||||
session_key = 1283724897238945234897
|
||||
"#.to_string(), CONFIG_FILENAME).is_err());
|
||||
|
||||
assert!(RocketConfig::parse(r#"
|
||||
[dev]
|
||||
session_key = "abcv"
|
||||
"#.to_string(), CONFIG_FILENAME).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bad_toml() {
|
||||
// Take the lock so changing the environment doesn't cause races.
|
||||
let _env_lock = ENV_LOCK.lock().unwrap();
|
||||
env::remove_var(CONFIG_ENV);
|
||||
|
||||
assert!(RocketConfig::parse(r#"
|
||||
[dev
|
||||
"#.to_string(), CONFIG_FILENAME).is_err());
|
||||
|
||||
assert!(RocketConfig::parse(r#"
|
||||
[dev]
|
||||
1.2.3 = 2
|
||||
"#.to_string(), CONFIG_FILENAME).is_err());
|
||||
|
||||
assert!(RocketConfig::parse(r#"
|
||||
[dev]
|
||||
session_key = "abcv" = other
|
||||
"#.to_string(), CONFIG_FILENAME).is_err());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -66,6 +66,8 @@ extern crate url;
|
|||
// extern crate mime;
|
||||
extern crate toml;
|
||||
|
||||
#[cfg(test)] #[macro_use] extern crate lazy_static;
|
||||
|
||||
#[doc(hidden)] #[macro_use] pub mod logger;
|
||||
#[doc(hidden)] pub mod http;
|
||||
pub mod form;
|
||||
|
|
Loading…
Reference in New Issue