2016-10-18 19:04:56 +00:00
|
|
|
//! Application configuration and configuration parameter retrieval.
|
|
|
|
//!
|
|
|
|
//! This module implements configuration handling for Rocket. It implements
|
|
|
|
//! the parsing and interpretation of the `Rocket.toml` config file. It also
|
|
|
|
//! allows libraries to access values that have been configured by the user.
|
|
|
|
//!
|
|
|
|
//! ## Application Configuration
|
|
|
|
//!
|
|
|
|
//! ### Environments
|
|
|
|
//!
|
|
|
|
//! Rocket applications are always running in one of three environments:
|
|
|
|
//!
|
|
|
|
//! * development _or_ dev
|
|
|
|
//! * staging _or_ stage
|
|
|
|
//! * production _or_ prod
|
|
|
|
//!
|
|
|
|
//! Each environment can contain different configuration parameters. By default,
|
|
|
|
//! Rocket applications run in the **development** environment. The environment
|
|
|
|
//! can be changed via the `ROCKET_ENV` environment variable. For example, to
|
|
|
|
//! start a Rocket application in the **production** environment:
|
|
|
|
//!
|
|
|
|
//! ```sh
|
|
|
|
//! ROCKET_ENV=production ./target/release/rocket_app
|
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! ### Configuration Parameters
|
|
|
|
//!
|
|
|
|
//! Each environments consists of several standard configuration parameters as
|
|
|
|
//! well as an arbitrary number of _extra_ configuration parameters, which are
|
|
|
|
//! not used by Rocket itself but can be used by external libraries. The
|
|
|
|
//! standard configuration parameters are:
|
|
|
|
//!
|
|
|
|
//! * **address**: _[string]_ an IP address or host the application will
|
|
|
|
//! listen on
|
|
|
|
//! * examples: `"localhost"`, `"0.0.0.0"`, `"1.2.3.4"`
|
|
|
|
//! * **port**: _[integer]_ a port number to listen on
|
|
|
|
//! * examples: `"8000"`, `"80"`, `"4242"`
|
2017-01-12 10:38:14 +00:00
|
|
|
//! * **workers**: _[integer]_ the number of concurrent workers to use
|
|
|
|
//! * examples: `"12"`, `"1"`, `"4"`
|
2016-10-18 19:04:56 +00:00
|
|
|
//! * **log**: _[string]_ how much information to log; one of `"normal"`,
|
|
|
|
//! `"debug"`, or `"critical"`
|
|
|
|
//! * **session_key**: _[string]_ a 192-bit base64 encoded string (32
|
|
|
|
//! characters) to use as the session key
|
|
|
|
//! * example: `"VheMwXIBygSmOlZAhuWl2B+zgvTN3WW5"`
|
|
|
|
//!
|
|
|
|
//! ### Rocket.toml
|
|
|
|
//!
|
|
|
|
//! The `Rocket.toml` file is used to specify the configuration parameters for
|
|
|
|
//! each environment. The file is optional. If it is not present, the default
|
|
|
|
//! configuration parameters are used.
|
|
|
|
//!
|
2016-10-31 16:00:32 +00:00
|
|
|
//! The file must be a series of tables, at most one for each environment and a
|
|
|
|
//! "global" table, where each table contains key-value pairs corresponding to
|
|
|
|
//! configuration parameters for that environment. If a configuration parameter
|
|
|
|
//! is missing, the default value is used. The following is a complete
|
|
|
|
//! `Rocket.toml` file, where every standard configuration parameter is
|
|
|
|
//! specified with the default value:
|
2016-10-18 19:04:56 +00:00
|
|
|
//!
|
|
|
|
//! ```toml
|
|
|
|
//! [development]
|
|
|
|
//! address = "localhost"
|
|
|
|
//! port = 8000
|
2017-01-12 10:38:14 +00:00
|
|
|
//! workers = max(number_of_cpus, 2)
|
2016-10-18 19:04:56 +00:00
|
|
|
//! log = "normal"
|
|
|
|
//!
|
|
|
|
//! [staging]
|
|
|
|
//! address = "0.0.0.0"
|
|
|
|
//! port = 80
|
2017-01-12 10:38:14 +00:00
|
|
|
//! workers = max(number_of_cpus, 2)
|
2016-10-18 19:04:56 +00:00
|
|
|
//! log = "normal"
|
|
|
|
//! # don't use this key! generate your own and keep it private!
|
|
|
|
//! session_key = "VheMwXIBygSmOlZAhuWl2B+zgvTN3WW5"
|
|
|
|
//!
|
|
|
|
//! [production]
|
|
|
|
//! address = "0.0.0.0"
|
|
|
|
//! port = 80
|
2017-01-12 10:38:14 +00:00
|
|
|
//! workers = max(number_of_cpus, 2)
|
2016-10-18 19:04:56 +00:00
|
|
|
//! log = "critical"
|
|
|
|
//! # don't use this key! generate your own and keep it private!
|
|
|
|
//! session_key = "adL5fFIPmZBrlyHk2YT4NLV3YCk2gFXz"
|
|
|
|
//! ```
|
|
|
|
//!
|
2017-01-12 10:38:14 +00:00
|
|
|
//! The `workers` parameter is computed by Rocket automatically; the value above
|
|
|
|
//! is not valid TOML syntax. When manually specifying the number of workers,
|
|
|
|
//! the value should be an integer: `workers = 10`.
|
|
|
|
//!
|
2016-10-31 16:00:32 +00:00
|
|
|
//! The "global" pseudo-environment can be used to set and/or override
|
|
|
|
//! configuration parameters globally. A parameter defined in a `[global]` table
|
|
|
|
//! sets, or overrides if already present, that parameter in every environment.
|
|
|
|
//! For example, given the following `Rocket.toml` file, the value of `address`
|
|
|
|
//! will be `"1.2.3.4"` in every environment:
|
|
|
|
//!
|
|
|
|
//! ```toml
|
|
|
|
//! [global]
|
|
|
|
//! address = "1.2.3.4"
|
|
|
|
//!
|
2016-11-02 15:55:56 +00:00
|
|
|
//! [development]
|
2016-10-31 16:00:32 +00:00
|
|
|
//! address = "localhost"
|
|
|
|
//!
|
|
|
|
//! [production]
|
|
|
|
//! address = "0.0.0.0"
|
|
|
|
//! ```
|
|
|
|
//!
|
2017-01-14 00:45:46 +00:00
|
|
|
//! ## Environment Variables
|
|
|
|
//!
|
|
|
|
//! All configuration parameters, including extras, can be overridden through
|
|
|
|
//! environment variables. To override the configuration parameter `param`, use
|
|
|
|
//! an environment variable named `ROCKET_PARAM`. For instance, to override the
|
|
|
|
//! "port" configuration parameter, you can run your application with:
|
|
|
|
//!
|
|
|
|
//! ```sh
|
|
|
|
//! ROCKET_PORT=3721 ./your_application
|
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! Environment variables take precedence over all other configuration methods:
|
|
|
|
//! if the variable is set, it will be used as the value for the parameter.
|
|
|
|
//!
|
2016-10-18 19:04:56 +00:00
|
|
|
//! ## Retrieving Configuration Parameters
|
|
|
|
//!
|
|
|
|
//! Configuration parameters for the currently active configuration environment
|
|
|
|
//! can be retrieved via the [active](fn.active.html) function and methods on
|
|
|
|
//! the [Config](struct.Config.html) structure. The general structure is to call
|
|
|
|
//! `active` and then one of the `get_` methods on the returned `Config`
|
|
|
|
//! structure.
|
|
|
|
//!
|
|
|
|
//! As an example, consider the following code used by the `Template` type to
|
|
|
|
//! retrieve the value of the `template_dir` configuration parameter. If the
|
|
|
|
//! value isn't present or isn't a string, a default value is used.
|
|
|
|
//!
|
|
|
|
//! ```rust
|
|
|
|
//! use rocket::config;
|
|
|
|
//!
|
|
|
|
//! const DEFAULT_TEMPLATE_DIR: &'static str = "templates";
|
|
|
|
//!
|
|
|
|
//! let template_dir = config::active().map(|config| {
|
|
|
|
//! let dir = config.get_str("template_dir")
|
|
|
|
//! .map_err(|e| if !e.is_not_found() { e.pretty_print(); })
|
|
|
|
//! .unwrap_or(DEFAULT_TEMPLATE_DIR);
|
|
|
|
//!
|
|
|
|
//! config.root().join(dir).to_string_lossy().into_owned()
|
|
|
|
//! }).unwrap_or(DEFAULT_TEMPLATE_DIR.to_string());
|
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! Libraries should always use a default if a parameter is not defined.
|
|
|
|
|
2016-10-03 10:39:56 +00:00
|
|
|
mod error;
|
|
|
|
mod environment;
|
|
|
|
mod config;
|
2017-01-12 02:31:37 +00:00
|
|
|
mod builder;
|
2017-01-14 00:45:46 +00:00
|
|
|
mod toml_ext;
|
2016-10-03 10:39:56 +00:00
|
|
|
|
2016-10-16 10:16:16 +00:00
|
|
|
use std::sync::{Once, ONCE_INIT};
|
2016-10-03 10:39:56 +00:00
|
|
|
use std::fs::{self, File};
|
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::io::Read;
|
2017-01-12 02:31:37 +00:00
|
|
|
use std::path::{Path, PathBuf};
|
2016-10-04 02:21:21 +00:00
|
|
|
use std::process;
|
2016-10-03 10:39:56 +00:00
|
|
|
use std::env;
|
|
|
|
|
2017-01-12 02:31:37 +00:00
|
|
|
use toml;
|
|
|
|
|
|
|
|
pub use toml::{Array, Table, Value};
|
2016-10-04 02:21:21 +00:00
|
|
|
pub use self::error::{ConfigError, ParsingError};
|
|
|
|
pub use self::environment::Environment;
|
2016-10-16 01:58:57 +00:00
|
|
|
pub use self::config::Config;
|
2017-01-12 02:31:37 +00:00
|
|
|
pub use self::builder::ConfigBuilder;
|
2017-01-14 00:45:46 +00:00
|
|
|
pub use self::toml_ext::IntoValue;
|
2016-12-20 00:51:59 +00:00
|
|
|
|
2017-01-12 02:31:37 +00:00
|
|
|
use self::Environment::*;
|
2017-01-14 00:45:46 +00:00
|
|
|
use self::environment::CONFIG_ENV;
|
|
|
|
use self::toml_ext::parse_simple_toml_value;
|
2016-10-04 02:21:21 +00:00
|
|
|
use logger::{self, LoggingLevel};
|
2017-01-14 00:45:46 +00:00
|
|
|
use http::ascii::uncased_eq;
|
2016-10-04 02:21:21 +00:00
|
|
|
|
2016-10-16 10:16:16 +00:00
|
|
|
static INIT: Once = ONCE_INIT;
|
2016-10-15 01:57:36 +00:00
|
|
|
static mut CONFIG: Option<RocketConfig> = None;
|
|
|
|
|
2016-10-03 10:39:56 +00:00
|
|
|
const CONFIG_FILENAME: &'static str = "Rocket.toml";
|
2016-10-31 16:00:32 +00:00
|
|
|
const GLOBAL_ENV_NAME: &'static str = "global";
|
2017-01-14 00:45:46 +00:00
|
|
|
const ENV_VAR_PREFIX: &'static str = "ROCKET_";
|
|
|
|
const PREHANDLED_VARS: [&'static str; 2] = ["ROCKET_CODEGEN_DEBUG", CONFIG_ENV];
|
2016-10-31 16:00:32 +00:00
|
|
|
|
2016-10-18 19:04:56 +00:00
|
|
|
/// Wraps `std::result` with the error type of
|
|
|
|
/// [ConfigError](enum.ConfigError.html).
|
2016-10-15 01:57:36 +00:00
|
|
|
pub type Result<T> = ::std::result::Result<T, ConfigError>;
|
|
|
|
|
2016-10-18 19:04:56 +00:00
|
|
|
#[doc(hidden)]
|
2016-10-16 01:58:57 +00:00
|
|
|
#[derive(Debug, PartialEq)]
|
2016-10-03 10:39:56 +00:00
|
|
|
pub struct RocketConfig {
|
|
|
|
pub active_env: Environment,
|
|
|
|
config: HashMap<Environment, Config>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl RocketConfig {
|
2017-01-14 00:45:46 +00:00
|
|
|
/// Create a new configuration using the passed in `config` for all
|
|
|
|
/// environments. The Rocket.toml file is ignored, as are environment
|
|
|
|
/// variables.
|
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// If the current working directory can't be retrieved, this function
|
|
|
|
/// panics.
|
|
|
|
pub fn new(config: Config) -> RocketConfig {
|
2017-01-12 02:31:37 +00:00
|
|
|
let f = config.config_path.clone();
|
|
|
|
let active_env = config.environment;
|
|
|
|
|
|
|
|
// None of these unwraps should fail since the filename is coming from
|
|
|
|
// an existing connfig.
|
|
|
|
let mut configs = HashMap::new();
|
|
|
|
configs.insert(Development, Config::default_for(Development, &f).unwrap());
|
|
|
|
configs.insert(Staging, Config::default_for(Staging, &f).unwrap());
|
|
|
|
configs.insert(Production, Config::default_for(Production, &f).unwrap());
|
|
|
|
configs.insert(active_env, config);
|
|
|
|
|
|
|
|
RocketConfig {
|
|
|
|
active_env: active_env,
|
|
|
|
config: configs
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-14 00:45:46 +00:00
|
|
|
/// Read the configuration from the `Rocket.toml` file. The file is search
|
|
|
|
/// for recursively up the tree, starting from the CWD.
|
|
|
|
pub fn read() -> Result<RocketConfig> {
|
|
|
|
// Find the config file, starting from the `cwd` and working backwords.
|
|
|
|
let file = RocketConfig::find()?;
|
|
|
|
|
|
|
|
// Try to open the config file for reading.
|
|
|
|
let mut handle = File::open(&file).map_err(|_| ConfigError::IOError)?;
|
|
|
|
|
|
|
|
// Read the configure file to a string for parsing.
|
|
|
|
let mut contents = String::new();
|
|
|
|
handle.read_to_string(&mut contents).map_err(|_| ConfigError::IOError)?;
|
|
|
|
|
|
|
|
// Parse the config and return the result.
|
|
|
|
RocketConfig::parse(contents, &file)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return the default configuration for all environments and marks the
|
|
|
|
/// active environment (via the CONFIG_ENV variable) as active.
|
|
|
|
pub fn active_default<P: AsRef<Path>>(filename: P) -> Result<RocketConfig> {
|
|
|
|
let mut defaults = HashMap::new();
|
|
|
|
defaults.insert(Development, Config::default_for(Development, &filename)?);
|
|
|
|
defaults.insert(Staging, Config::default_for(Staging, &filename)?);
|
|
|
|
defaults.insert(Production, Config::default_for(Production, &filename)?);
|
|
|
|
|
|
|
|
let mut config = RocketConfig {
|
|
|
|
active_env: Environment::active()?,
|
|
|
|
config: defaults,
|
|
|
|
};
|
|
|
|
|
|
|
|
// Override any variables from the environment.
|
|
|
|
config.override_from_env()?;
|
|
|
|
Ok(config)
|
|
|
|
}
|
|
|
|
|
2017-01-12 02:31:37 +00:00
|
|
|
/// Iteratively search for `CONFIG_FILENAME` starting at the current working
|
|
|
|
/// directory and working up through its parents. Returns the path to the
|
|
|
|
/// file or an Error::NoKey if the file couldn't be found. If the current
|
|
|
|
/// working directory can't be determined, return `BadCWD`.
|
2016-10-15 01:57:36 +00:00
|
|
|
fn find() -> Result<PathBuf> {
|
2016-10-03 10:39:56 +00:00
|
|
|
let cwd = env::current_dir().map_err(|_| ConfigError::BadCWD)?;
|
|
|
|
let mut current = cwd.as_path();
|
|
|
|
|
|
|
|
loop {
|
|
|
|
let manifest = current.join(CONFIG_FILENAME);
|
|
|
|
if fs::metadata(&manifest).is_ok() {
|
|
|
|
return Ok(manifest)
|
|
|
|
}
|
|
|
|
|
|
|
|
match current.parent() {
|
|
|
|
Some(p) => current = p,
|
|
|
|
None => break,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Err(ConfigError::NotFound)
|
|
|
|
}
|
|
|
|
|
2017-01-14 00:45:46 +00:00
|
|
|
fn get_mut(&mut self, env: Environment) -> &mut Config {
|
|
|
|
match self.config.get_mut(&env) {
|
|
|
|
Some(config) => config,
|
|
|
|
None => panic!("set(): {} config is missing.", env),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-12 02:31:37 +00:00
|
|
|
/// Set the configuration for the environment `env` to be the configuration
|
|
|
|
/// derived from the TOML table `kvs`. The environment must already exist in
|
|
|
|
/// `self`, otherwise this function panics. Any existing values are
|
|
|
|
/// overriden by those in `kvs`.
|
2017-01-14 00:45:46 +00:00
|
|
|
fn set_from_table(&mut self, env: Environment, kvs: &Table) -> Result<()> {
|
2016-10-03 10:39:56 +00:00
|
|
|
for (key, value) in kvs {
|
2017-01-14 00:45:46 +00:00
|
|
|
self.get_mut(env).set(key, value)?;
|
2016-10-03 10:39:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2017-01-12 02:31:37 +00:00
|
|
|
/// Retrieves the `Config` for the environment `env`.
|
2016-10-03 10:39:56 +00:00
|
|
|
pub fn get(&self, env: Environment) -> &Config {
|
2016-10-15 01:57:36 +00:00
|
|
|
match self.config.get(&env) {
|
|
|
|
Some(config) => config,
|
|
|
|
None => panic!("get(): {} config is missing.", env),
|
2016-10-03 10:39:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-12 02:31:37 +00:00
|
|
|
/// Retrieves the `Config` for the active environment.
|
2016-10-03 10:39:56 +00:00
|
|
|
pub fn active(&self) -> &Config {
|
|
|
|
self.get(self.active_env)
|
|
|
|
}
|
|
|
|
|
2017-01-14 00:45:46 +00:00
|
|
|
// Override all environments with values from env variables if present.
|
|
|
|
fn override_from_env(&mut self) -> Result<()> {
|
|
|
|
'outer: for (env_key, env_val) in env::vars() {
|
|
|
|
if env_key.len() < ENV_VAR_PREFIX.len() {
|
|
|
|
continue
|
|
|
|
} else if !uncased_eq(&env_key[..ENV_VAR_PREFIX.len()], ENV_VAR_PREFIX) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skip environment variables that are handled elsewhere.
|
|
|
|
for prehandled_var in PREHANDLED_VARS.iter() {
|
|
|
|
if uncased_eq(&env_key, &prehandled_var) {
|
|
|
|
continue 'outer
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse the key and value and try to set the variable for all envs.
|
|
|
|
let key = env_key[ENV_VAR_PREFIX.len()..].to_lowercase();
|
|
|
|
let val = parse_simple_toml_value(&env_val);
|
|
|
|
for env in &Environment::all() {
|
|
|
|
match self.get_mut(*env).set(&key, &val) {
|
|
|
|
Err(ConfigError::BadType(_, exp, _, _)) => {
|
|
|
|
return Err(ConfigError::BadEnvVal(env_key, env_val, exp))
|
|
|
|
}
|
|
|
|
Err(e) => return Err(e),
|
|
|
|
Ok(_) => { /* move along */ }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Parses the configuration from the Rocket.toml file. Also overrides any
|
|
|
|
/// values there with values from the environment.
|
2017-01-12 02:31:37 +00:00
|
|
|
fn parse<P: AsRef<Path>>(src: String, filename: P) -> Result<RocketConfig> {
|
|
|
|
// Get a PathBuf version of the filename.
|
|
|
|
let path = filename.as_ref().to_path_buf();
|
|
|
|
|
2016-10-03 10:39:56 +00:00
|
|
|
// Parse the source as TOML, if possible.
|
|
|
|
let mut parser = toml::Parser::new(&src);
|
2017-01-12 02:31:37 +00:00
|
|
|
let toml = parser.parse().ok_or_else(|| {
|
|
|
|
let source = src.clone();
|
|
|
|
let errors = parser.errors.iter()
|
|
|
|
.map(|error| ParsingError {
|
|
|
|
byte_range: (error.lo, error.hi),
|
|
|
|
start: parser.to_linecol(error.lo),
|
|
|
|
end: parser.to_linecol(error.hi),
|
|
|
|
desc: error.desc.clone(),
|
|
|
|
});
|
|
|
|
|
|
|
|
ConfigError::ParseError(source, path.clone(), errors.collect())
|
|
|
|
})?;
|
2016-10-03 10:39:56 +00:00
|
|
|
|
2016-10-31 16:00:32 +00:00
|
|
|
// Create a config with the defaults; set the env to the active one.
|
2016-10-15 01:57:36 +00:00
|
|
|
let mut config = RocketConfig::active_default(filename)?;
|
2016-10-03 10:39:56 +00:00
|
|
|
|
2016-10-31 16:00:32 +00:00
|
|
|
// Store all of the global overrides, if any, for later use.
|
|
|
|
let mut global = None;
|
|
|
|
|
2016-10-03 10:39:56 +00:00
|
|
|
// Parse the values from the TOML file.
|
|
|
|
for (entry, value) in toml {
|
|
|
|
// Each environment must be a table.
|
|
|
|
let kv_pairs = match value.as_table() {
|
|
|
|
Some(table) => table,
|
|
|
|
None => return Err(ConfigError::BadType(
|
2017-01-12 02:31:37 +00:00
|
|
|
entry, "a table", value.type_str(), path.clone()
|
2016-10-03 10:39:56 +00:00
|
|
|
))
|
|
|
|
};
|
|
|
|
|
2017-01-12 02:31:37 +00:00
|
|
|
// Store the global table for later use and move on.
|
2016-10-31 16:00:32 +00:00
|
|
|
if entry.as_str() == GLOBAL_ENV_NAME {
|
|
|
|
global = Some(kv_pairs.clone());
|
2017-01-12 02:31:37 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is not the global table. Parse the environment name from the
|
|
|
|
// table entry name and then set all of the key/values.
|
|
|
|
match entry.as_str().parse() {
|
2017-01-14 00:45:46 +00:00
|
|
|
Ok(env) => config.set_from_table(env, kv_pairs)?,
|
2017-01-12 02:31:37 +00:00
|
|
|
Err(_) => Err(ConfigError::BadEntry(entry.clone(), path.clone()))?
|
2016-10-31 16:00:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Override all of the environments with the global values.
|
|
|
|
if let Some(ref global_kv_pairs) = global {
|
|
|
|
for env in &Environment::all() {
|
2017-01-14 00:45:46 +00:00
|
|
|
config.set_from_table(*env, global_kv_pairs)?;
|
2016-10-31 16:00:32 +00:00
|
|
|
}
|
2016-10-03 10:39:56 +00:00
|
|
|
}
|
|
|
|
|
2017-01-14 00:45:46 +00:00
|
|
|
// Override any variables from the environment.
|
|
|
|
config.override_from_env()?;
|
2016-10-03 10:39:56 +00:00
|
|
|
|
2017-01-14 00:45:46 +00:00
|
|
|
Ok(config)
|
2016-10-04 02:21:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-16 10:16:16 +00:00
|
|
|
/// Returns the active configuration and whether this call initialized the
|
|
|
|
/// configuration. The configuration can only be initialized once.
|
|
|
|
///
|
2016-10-15 01:57:36 +00:00
|
|
|
/// Initializes the global RocketConfig by reading the Rocket config file from
|
|
|
|
/// the current directory or any of its parents. Returns the active
|
|
|
|
/// configuration, which is determined by the config env variable. If there as a
|
|
|
|
/// problem parsing the configuration, the error is printed and the progam is
|
|
|
|
/// aborted. If there an I/O issue reading the config file, a warning is printed
|
|
|
|
/// and the default configuration is used. If there is no config file, the
|
|
|
|
/// default configuration is used.
|
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
///
|
2016-10-16 10:16:16 +00:00
|
|
|
/// If there is a problem, prints a nice error message and bails.
|
2016-10-16 01:58:57 +00:00
|
|
|
#[doc(hidden)]
|
2016-10-16 10:16:16 +00:00
|
|
|
pub fn init() -> (&'static Config, bool) {
|
|
|
|
let mut this_init = false;
|
|
|
|
unsafe {
|
|
|
|
INIT.call_once(|| {
|
|
|
|
private_init();
|
|
|
|
this_init = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
(CONFIG.as_ref().unwrap().active(), this_init)
|
2016-10-04 10:54:24 +00:00
|
|
|
}
|
2016-10-16 10:16:16 +00:00
|
|
|
}
|
2016-10-04 10:54:24 +00:00
|
|
|
|
2017-01-12 02:31:37 +00:00
|
|
|
#[doc(hidden)]
|
|
|
|
pub fn custom_init(config: Config) -> (&'static Config, bool) {
|
|
|
|
let mut this_init = false;
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
INIT.call_once(|| {
|
|
|
|
CONFIG = Some(RocketConfig::new(config));
|
|
|
|
this_init = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
(CONFIG.as_ref().unwrap().active(), this_init)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-16 10:16:16 +00:00
|
|
|
unsafe fn private_init() {
|
2016-10-04 02:21:21 +00:00
|
|
|
let bail = |e: ConfigError| -> ! {
|
|
|
|
logger::init(LoggingLevel::Debug);
|
|
|
|
e.pretty_print();
|
|
|
|
process::exit(1)
|
|
|
|
};
|
|
|
|
|
|
|
|
use self::ConfigError::*;
|
2016-10-15 01:57:36 +00:00
|
|
|
let config = RocketConfig::read().unwrap_or_else(|e| {
|
2016-10-04 02:21:21 +00:00
|
|
|
match e {
|
2016-10-15 01:57:36 +00:00
|
|
|
ParseError(..) | BadEntry(..) | BadEnv(..) | BadType(..)
|
2017-01-14 00:45:46 +00:00
|
|
|
| BadFilePath(..) | BadEnvVal(..) => bail(e),
|
2016-10-15 01:57:36 +00:00
|
|
|
IOError | BadCWD => warn!("Failed reading Rocket.toml. Using defaults."),
|
2016-10-04 02:21:21 +00:00
|
|
|
NotFound => { /* try using the default below */ }
|
|
|
|
}
|
|
|
|
|
2016-10-15 01:57:36 +00:00
|
|
|
let default_path = match env::current_dir() {
|
|
|
|
Ok(path) => path.join(&format!(".{}.{}", "default", CONFIG_FILENAME)),
|
|
|
|
Err(_) => bail(ConfigError::BadCWD)
|
|
|
|
};
|
|
|
|
|
2017-01-12 02:31:37 +00:00
|
|
|
RocketConfig::active_default(&default_path).unwrap_or_else(|e| bail(e))
|
2016-10-15 01:57:36 +00:00
|
|
|
});
|
|
|
|
|
2016-10-16 10:16:16 +00:00
|
|
|
CONFIG = Some(config);
|
2016-10-15 01:57:36 +00:00
|
|
|
}
|
|
|
|
|
2016-10-16 01:58:57 +00:00
|
|
|
/// Retrieve the active configuration, if there is one.
|
2016-10-18 19:04:56 +00:00
|
|
|
///
|
|
|
|
/// This function is guaranteed to return `Some` once a Rocket application has
|
|
|
|
/// started. Before a Rocket application has started, or when there is no active
|
|
|
|
/// Rocket application (such as during testing), this function will return None.
|
2016-10-15 01:57:36 +00:00
|
|
|
pub fn active() -> Option<&'static Config> {
|
|
|
|
unsafe { CONFIG.as_ref().map(|c| c.active()) }
|
2016-10-03 10:39:56 +00:00
|
|
|
}
|
|
|
|
|
2016-10-04 10:54:24 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use std::env;
|
|
|
|
use std::sync::Mutex;
|
|
|
|
|
2017-01-14 00:45:46 +00:00
|
|
|
use super::{RocketConfig, Config, ConfigError, ConfigBuilder};
|
2017-01-12 02:31:37 +00:00
|
|
|
use super::{Environment, GLOBAL_ENV_NAME};
|
|
|
|
use super::environment::CONFIG_ENV;
|
2016-10-04 10:54:24 +00:00
|
|
|
use super::Environment::*;
|
2016-10-15 01:57:36 +00:00
|
|
|
use super::Result;
|
2016-10-04 10:54:24 +00:00
|
|
|
|
|
|
|
use ::logger::LoggingLevel;
|
|
|
|
|
2016-10-15 01:57:36 +00:00
|
|
|
const TEST_CONFIG_FILENAME: &'static str = "/tmp/testing/Rocket.toml";
|
|
|
|
|
2016-10-04 10:54:24 +00:00
|
|
|
lazy_static! {
|
|
|
|
static ref ENV_LOCK: Mutex<usize> = Mutex::new(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! check_config {
|
|
|
|
($rconfig:expr, $econfig:expr) => (
|
2017-01-12 02:31:37 +00:00
|
|
|
let expected = $econfig.finalize().unwrap();
|
2016-10-04 10:54:24 +00:00
|
|
|
match $rconfig {
|
2017-01-12 02:31:37 +00:00
|
|
|
Ok(config) => assert_eq!(config.active(), &expected),
|
2016-10-04 10:54:24 +00:00
|
|
|
Err(e) => panic!("Config {} failed: {:?}", stringify!($rconfig), e)
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
($env:expr, $rconfig:expr, $econfig:expr) => (
|
2017-01-12 02:31:37 +00:00
|
|
|
let expected = $econfig.finalize().unwrap();
|
2016-10-16 01:58:57 +00:00
|
|
|
match $rconfig {
|
2017-01-12 02:31:37 +00:00
|
|
|
Ok(ref config) => assert_eq!(config.get($env), &expected),
|
2016-10-16 01:58:57 +00:00
|
|
|
Err(ref e) => panic!("Config {} failed: {:?}", stringify!($rconfig), e)
|
2016-10-04 10:54:24 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-10-15 01:57:36 +00:00
|
|
|
fn active_default() -> Result<RocketConfig> {
|
|
|
|
RocketConfig::active_default(TEST_CONFIG_FILENAME)
|
|
|
|
}
|
|
|
|
|
2017-01-12 02:31:37 +00:00
|
|
|
fn default_config(env: Environment) -> ConfigBuilder {
|
|
|
|
ConfigBuilder::new(env)
|
2016-10-15 01:57:36 +00:00
|
|
|
}
|
|
|
|
|
2016-10-04 10:54:24 +00:00
|
|
|
#[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);
|
2016-10-15 01:57:36 +00:00
|
|
|
check_config!(active_default(), default_config(Development));
|
2016-10-04 10:54:24 +00:00
|
|
|
|
|
|
|
// Now with an explicit dev environment.
|
|
|
|
for env in &["development", "dev"] {
|
|
|
|
env::set_var(CONFIG_ENV, env);
|
2016-10-15 01:57:36 +00:00
|
|
|
check_config!(active_default(), default_config(Development));
|
2016-10-03 10:39:56 +00:00
|
|
|
}
|
2016-10-04 10:54:24 +00:00
|
|
|
|
|
|
|
// Now staging.
|
|
|
|
for env in &["stage", "staging"] {
|
|
|
|
env::set_var(CONFIG_ENV, env);
|
2016-10-15 01:57:36 +00:00
|
|
|
check_config!(active_default(), default_config(Staging));
|
2016-10-04 10:54:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, production.
|
|
|
|
for env in &["prod", "production"] {
|
|
|
|
env::set_var(CONFIG_ENV, env);
|
2016-10-15 01:57:36 +00:00
|
|
|
check_config!(active_default(), default_config(Production));
|
2016-10-04 10:54:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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());
|
2016-10-15 01:57:36 +00:00
|
|
|
assert!(active_default().err().map_or(false, |e| e == err));
|
2016-10-04 10:54:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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);
|
2016-10-15 01:57:36 +00:00
|
|
|
let e_str = env.to_string();
|
|
|
|
let err = ConfigError::BadEntry(e_str, TEST_CONFIG_FILENAME.into());
|
|
|
|
assert!(RocketConfig::parse(toml_table, TEST_CONFIG_FILENAME)
|
2016-10-04 10:54:24 +00:00
|
|
|
.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
|
2017-01-12 10:38:14 +00:00
|
|
|
workers = 21
|
2016-10-04 10:54:24 +00:00
|
|
|
log = "critical"
|
|
|
|
session_key = "01234567890123456789012345678901"
|
|
|
|
template_dir = "mine"
|
|
|
|
json = true
|
|
|
|
pi = 3.14
|
|
|
|
"#;
|
|
|
|
|
2016-10-16 01:58:57 +00:00
|
|
|
let mut expected = default_config(Development)
|
2017-01-12 02:31:37 +00:00
|
|
|
.address("1.2.3.4")
|
|
|
|
.port(7810)
|
2017-01-12 10:38:14 +00:00
|
|
|
.workers(21)
|
2017-01-12 02:31:37 +00:00
|
|
|
.log_level(LoggingLevel::Critical)
|
|
|
|
.session_key("01234567890123456789012345678901")
|
|
|
|
.extra("template_dir", "mine")
|
|
|
|
.extra("json", true)
|
|
|
|
.extra("pi", 3.14);
|
|
|
|
|
|
|
|
expected.environment = Development;
|
2016-10-04 10:54:24 +00:00
|
|
|
let dev_config = ["[dev]", config_str].join("\n");
|
2016-10-15 01:57:36 +00:00
|
|
|
let parsed = RocketConfig::parse(dev_config, TEST_CONFIG_FILENAME);
|
2017-01-12 02:31:37 +00:00
|
|
|
check_config!(Development, parsed, expected.clone());
|
2016-10-15 01:57:36 +00:00
|
|
|
check_config!(Staging, parsed, default_config(Staging));
|
|
|
|
check_config!(Production, parsed, default_config(Production));
|
2016-10-04 10:54:24 +00:00
|
|
|
|
2017-01-12 02:31:37 +00:00
|
|
|
expected.environment = Staging;
|
2016-10-04 10:54:24 +00:00
|
|
|
let stage_config = ["[stage]", config_str].join("\n");
|
2016-10-15 01:57:36 +00:00
|
|
|
let parsed = RocketConfig::parse(stage_config, TEST_CONFIG_FILENAME);
|
2017-01-12 02:31:37 +00:00
|
|
|
check_config!(Staging, parsed, expected.clone());
|
2016-10-15 01:57:36 +00:00
|
|
|
check_config!(Development, parsed, default_config(Development));
|
|
|
|
check_config!(Production, parsed, default_config(Production));
|
2016-10-04 10:54:24 +00:00
|
|
|
|
2017-01-12 02:31:37 +00:00
|
|
|
expected.environment = Production;
|
2016-10-04 10:54:24 +00:00
|
|
|
let prod_config = ["[prod]", config_str].join("\n");
|
2016-10-15 01:57:36 +00:00
|
|
|
let parsed = RocketConfig::parse(prod_config, TEST_CONFIG_FILENAME);
|
2016-10-04 10:54:24 +00:00
|
|
|
check_config!(Production, parsed, expected);
|
2016-10-15 01:57:36 +00:00
|
|
|
check_config!(Development, parsed, default_config(Development));
|
|
|
|
check_config!(Staging, parsed, default_config(Staging));
|
2016-10-04 10:54:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[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"
|
2016-10-15 01:57:36 +00:00
|
|
|
"#.to_string(), TEST_CONFIG_FILENAME), {
|
2017-01-12 02:31:37 +00:00
|
|
|
default_config(Development).address("localhost")
|
2016-10-04 10:54:24 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
check_config!(RocketConfig::parse(r#"
|
|
|
|
[dev]
|
|
|
|
address = "127.0.0.1"
|
2016-10-15 01:57:36 +00:00
|
|
|
"#.to_string(), TEST_CONFIG_FILENAME), {
|
2017-01-12 02:31:37 +00:00
|
|
|
default_config(Development).address("127.0.0.1")
|
2016-10-04 10:54:24 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
check_config!(RocketConfig::parse(r#"
|
|
|
|
[dev]
|
|
|
|
address = "0.0.0.0"
|
2016-10-15 01:57:36 +00:00
|
|
|
"#.to_string(), TEST_CONFIG_FILENAME), {
|
2017-01-12 02:31:37 +00:00
|
|
|
default_config(Development).address("0.0.0.0")
|
2016-10-04 10:54:24 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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
|
2016-10-15 01:57:36 +00:00
|
|
|
"#.to_string(), TEST_CONFIG_FILENAME).is_err());
|
2016-10-04 10:54:24 +00:00
|
|
|
|
|
|
|
assert!(RocketConfig::parse(r#"
|
|
|
|
[development]
|
|
|
|
address = true
|
2016-10-15 01:57:36 +00:00
|
|
|
"#.to_string(), TEST_CONFIG_FILENAME).is_err());
|
2016-10-04 10:54:24 +00:00
|
|
|
|
|
|
|
assert!(RocketConfig::parse(r#"
|
|
|
|
[development]
|
2016-12-21 01:27:31 +00:00
|
|
|
address = "........"
|
2016-10-15 01:57:36 +00:00
|
|
|
"#.to_string(), TEST_CONFIG_FILENAME).is_err());
|
2016-10-04 10:54:24 +00:00
|
|
|
|
|
|
|
assert!(RocketConfig::parse(r#"
|
|
|
|
[staging]
|
|
|
|
address = "1.2.3.4:100"
|
2016-10-15 01:57:36 +00:00
|
|
|
"#.to_string(), TEST_CONFIG_FILENAME).is_err());
|
2016-10-04 10:54:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[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
|
2016-10-15 01:57:36 +00:00
|
|
|
"#.to_string(), TEST_CONFIG_FILENAME), {
|
|
|
|
default_config(Staging).port(100)
|
2016-10-04 10:54:24 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
check_config!(RocketConfig::parse(r#"
|
|
|
|
[stage]
|
|
|
|
port = 6000
|
2016-10-15 01:57:36 +00:00
|
|
|
"#.to_string(), TEST_CONFIG_FILENAME), {
|
|
|
|
default_config(Staging).port(6000)
|
2016-10-04 10:54:24 +00:00
|
|
|
});
|
2017-01-09 18:15:33 +00:00
|
|
|
|
|
|
|
check_config!(RocketConfig::parse(r#"
|
|
|
|
[stage]
|
|
|
|
port = 65535
|
|
|
|
"#.to_string(), TEST_CONFIG_FILENAME), {
|
|
|
|
default_config(Staging).port(65535)
|
|
|
|
});
|
2016-10-04 10:54:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[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
|
2016-10-15 01:57:36 +00:00
|
|
|
"#.to_string(), TEST_CONFIG_FILENAME).is_err());
|
2016-10-04 10:54:24 +00:00
|
|
|
|
|
|
|
assert!(RocketConfig::parse(r#"
|
|
|
|
[production]
|
|
|
|
port = "hello"
|
2016-10-15 01:57:36 +00:00
|
|
|
"#.to_string(), TEST_CONFIG_FILENAME).is_err());
|
2016-10-04 10:54:24 +00:00
|
|
|
|
|
|
|
assert!(RocketConfig::parse(r#"
|
|
|
|
[staging]
|
|
|
|
port = -1
|
2016-10-15 01:57:36 +00:00
|
|
|
"#.to_string(), TEST_CONFIG_FILENAME).is_err());
|
2017-01-09 18:15:33 +00:00
|
|
|
|
|
|
|
assert!(RocketConfig::parse(r#"
|
|
|
|
[staging]
|
|
|
|
port = 65536
|
|
|
|
"#.to_string(), TEST_CONFIG_FILENAME).is_err());
|
|
|
|
|
|
|
|
assert!(RocketConfig::parse(r#"
|
|
|
|
[staging]
|
|
|
|
port = 105836
|
|
|
|
"#.to_string(), TEST_CONFIG_FILENAME).is_err());
|
2016-10-04 10:54:24 +00:00
|
|
|
}
|
|
|
|
|
2017-01-12 10:38:14 +00:00
|
|
|
#[test]
|
|
|
|
fn test_good_workers_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]
|
|
|
|
workers = 1
|
|
|
|
"#.to_string(), TEST_CONFIG_FILENAME), {
|
|
|
|
default_config(Staging).workers(1)
|
|
|
|
});
|
|
|
|
|
|
|
|
check_config!(RocketConfig::parse(r#"
|
|
|
|
[stage]
|
|
|
|
workers = 300
|
|
|
|
"#.to_string(), TEST_CONFIG_FILENAME), {
|
|
|
|
default_config(Staging).workers(300)
|
|
|
|
});
|
|
|
|
|
|
|
|
check_config!(RocketConfig::parse(r#"
|
|
|
|
[stage]
|
|
|
|
workers = 65535
|
|
|
|
"#.to_string(), TEST_CONFIG_FILENAME), {
|
|
|
|
default_config(Staging).workers(65535)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_bad_workers_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]
|
|
|
|
workers = true
|
|
|
|
"#.to_string(), TEST_CONFIG_FILENAME).is_err());
|
|
|
|
|
|
|
|
assert!(RocketConfig::parse(r#"
|
|
|
|
[production]
|
|
|
|
workers = "hello"
|
|
|
|
"#.to_string(), TEST_CONFIG_FILENAME).is_err());
|
|
|
|
|
|
|
|
assert!(RocketConfig::parse(r#"
|
|
|
|
[staging]
|
|
|
|
workers = -1
|
|
|
|
"#.to_string(), TEST_CONFIG_FILENAME).is_err());
|
|
|
|
|
|
|
|
assert!(RocketConfig::parse(r#"
|
|
|
|
[staging]
|
|
|
|
workers = 65536
|
|
|
|
"#.to_string(), TEST_CONFIG_FILENAME).is_err());
|
|
|
|
|
|
|
|
assert!(RocketConfig::parse(r#"
|
|
|
|
[staging]
|
|
|
|
workers = 105836
|
|
|
|
"#.to_string(), TEST_CONFIG_FILENAME).is_err());
|
|
|
|
}
|
|
|
|
|
2016-10-04 10:54:24 +00:00
|
|
|
#[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"
|
2016-10-15 01:57:36 +00:00
|
|
|
"#.to_string(), TEST_CONFIG_FILENAME), {
|
|
|
|
default_config(Staging).log_level(LoggingLevel::Normal)
|
2016-10-04 10:54:24 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
check_config!(RocketConfig::parse(r#"
|
|
|
|
[stage]
|
|
|
|
log = "debug"
|
2016-10-15 01:57:36 +00:00
|
|
|
"#.to_string(), TEST_CONFIG_FILENAME), {
|
|
|
|
default_config(Staging).log_level(LoggingLevel::Debug)
|
2016-10-04 10:54:24 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
check_config!(RocketConfig::parse(r#"
|
|
|
|
[stage]
|
|
|
|
log = "critical"
|
2016-10-15 01:57:36 +00:00
|
|
|
"#.to_string(), TEST_CONFIG_FILENAME), {
|
|
|
|
default_config(Staging).log_level(LoggingLevel::Critical)
|
2016-10-04 10:54:24 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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
|
2016-10-15 01:57:36 +00:00
|
|
|
"#.to_string(), TEST_CONFIG_FILENAME).is_err());
|
2016-10-04 10:54:24 +00:00
|
|
|
|
|
|
|
assert!(RocketConfig::parse(r#"
|
|
|
|
[development]
|
|
|
|
log = 0
|
2016-10-15 01:57:36 +00:00
|
|
|
"#.to_string(), TEST_CONFIG_FILENAME).is_err());
|
2016-10-04 10:54:24 +00:00
|
|
|
|
|
|
|
assert!(RocketConfig::parse(r#"
|
|
|
|
[prod]
|
|
|
|
log = "no"
|
2016-10-15 01:57:36 +00:00
|
|
|
"#.to_string(), TEST_CONFIG_FILENAME).is_err());
|
2016-10-04 10:54:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[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"
|
2016-10-15 01:57:36 +00:00
|
|
|
"#.to_string(), TEST_CONFIG_FILENAME), {
|
|
|
|
default_config(Staging).session_key(
|
2017-01-12 02:31:37 +00:00
|
|
|
"VheMwXIBygSmOlZAhuWl2B+zgvTN3WW5"
|
2016-10-15 01:57:36 +00:00
|
|
|
)
|
2016-10-04 10:54:24 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
check_config!(RocketConfig::parse(r#"
|
|
|
|
[stage]
|
|
|
|
session_key = "adL5fFIPmZBrlyHk2YT4NLV3YCk2gFXz"
|
2016-10-15 01:57:36 +00:00
|
|
|
"#.to_string(), TEST_CONFIG_FILENAME), {
|
|
|
|
default_config(Staging).session_key(
|
2017-01-12 02:31:37 +00:00
|
|
|
"adL5fFIPmZBrlyHk2YT4NLV3YCk2gFXz"
|
2016-10-15 01:57:36 +00:00
|
|
|
)
|
2016-10-04 10:54:24 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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
|
2016-10-15 01:57:36 +00:00
|
|
|
"#.to_string(), TEST_CONFIG_FILENAME).is_err());
|
2016-10-04 10:54:24 +00:00
|
|
|
|
|
|
|
assert!(RocketConfig::parse(r#"
|
|
|
|
[dev]
|
|
|
|
session_key = 1283724897238945234897
|
2016-10-15 01:57:36 +00:00
|
|
|
"#.to_string(), TEST_CONFIG_FILENAME).is_err());
|
2016-10-04 10:54:24 +00:00
|
|
|
|
|
|
|
assert!(RocketConfig::parse(r#"
|
|
|
|
[dev]
|
|
|
|
session_key = "abcv"
|
2016-10-15 01:57:36 +00:00
|
|
|
"#.to_string(), TEST_CONFIG_FILENAME).is_err());
|
2016-10-04 10:54:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[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
|
2016-10-15 01:57:36 +00:00
|
|
|
"#.to_string(), TEST_CONFIG_FILENAME).is_err());
|
2016-10-04 10:54:24 +00:00
|
|
|
|
|
|
|
assert!(RocketConfig::parse(r#"
|
|
|
|
[dev]
|
|
|
|
1.2.3 = 2
|
2016-10-15 01:57:36 +00:00
|
|
|
"#.to_string(), TEST_CONFIG_FILENAME).is_err());
|
2016-10-04 10:54:24 +00:00
|
|
|
|
|
|
|
assert!(RocketConfig::parse(r#"
|
|
|
|
[dev]
|
|
|
|
session_key = "abcv" = other
|
2016-10-15 01:57:36 +00:00
|
|
|
"#.to_string(), TEST_CONFIG_FILENAME).is_err());
|
2016-10-03 10:39:56 +00:00
|
|
|
}
|
2016-10-31 16:00:32 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_global_overrides() {
|
|
|
|
// Take the lock so changing the environment doesn't cause races.
|
|
|
|
let _env_lock = ENV_LOCK.lock().unwrap();
|
|
|
|
|
|
|
|
// Test first that we can override each environment.
|
|
|
|
for env in &Environment::all() {
|
|
|
|
env::set_var(CONFIG_ENV, env.to_string());
|
|
|
|
|
|
|
|
check_config!(RocketConfig::parse(format!(r#"
|
|
|
|
[{}]
|
|
|
|
address = "7.6.5.4"
|
|
|
|
"#, GLOBAL_ENV_NAME), TEST_CONFIG_FILENAME), {
|
2017-01-12 02:31:37 +00:00
|
|
|
default_config(*env).address("7.6.5.4")
|
2016-10-31 16:00:32 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
check_config!(RocketConfig::parse(format!(r#"
|
|
|
|
[{}]
|
|
|
|
database = "mysql"
|
|
|
|
"#, GLOBAL_ENV_NAME), TEST_CONFIG_FILENAME), {
|
2017-01-12 02:31:37 +00:00
|
|
|
default_config(*env).extra("database", "mysql")
|
2016-10-31 16:00:32 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
check_config!(RocketConfig::parse(format!(r#"
|
|
|
|
[{}]
|
|
|
|
port = 3980
|
|
|
|
"#, GLOBAL_ENV_NAME), TEST_CONFIG_FILENAME), {
|
2017-01-12 02:31:37 +00:00
|
|
|
default_config(*env).port(3980)
|
2016-10-31 16:00:32 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2017-01-14 00:45:46 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_env_override() {
|
|
|
|
// Take the lock so changing the environment doesn't cause races.
|
|
|
|
let _env_lock = ENV_LOCK.lock().unwrap();
|
|
|
|
|
|
|
|
let pairs = [
|
|
|
|
("log", "critical"), ("LOG", "debug"), ("PORT", "8110"),
|
|
|
|
("address", "1.2.3.4"), ("EXTRA_EXTRA", "true"), ("workers", "3")
|
|
|
|
];
|
|
|
|
|
|
|
|
let check_value = |key: &str, val: &str, config: &Config| {
|
|
|
|
match key {
|
|
|
|
"log" => assert_eq!(config.log_level, val.parse().unwrap()),
|
|
|
|
"port" => assert_eq!(config.port, val.parse().unwrap()),
|
|
|
|
"address" => assert_eq!(config.address, val),
|
|
|
|
"extra_extra" => assert_eq!(config.get_bool(key).unwrap(), true),
|
|
|
|
"workers" => assert_eq!(config.workers, val.parse().unwrap()),
|
|
|
|
_ => panic!("Unexpected key: {}", key)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Check that setting the environment variable actually changes the
|
|
|
|
// config for the default active and nonactive environments.
|
|
|
|
for &(key, val) in &pairs {
|
|
|
|
env::set_var(format!("ROCKET_{}", key), val);
|
|
|
|
|
|
|
|
let rconfig = active_default().unwrap();
|
|
|
|
// Check that it overrides the active config.
|
|
|
|
for env in &Environment::all() {
|
|
|
|
env::set_var(CONFIG_ENV, env.to_string());
|
|
|
|
let rconfig = active_default().unwrap();
|
|
|
|
check_value(&*key.to_lowercase(), val, rconfig.active());
|
|
|
|
}
|
|
|
|
|
|
|
|
// And non-active configs.
|
|
|
|
for env in &Environment::all() {
|
|
|
|
check_value(&*key.to_lowercase(), val, rconfig.get(*env));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clear the variables so they don't override for the next test.
|
|
|
|
for &(key, _) in &pairs {
|
|
|
|
env::remove_var(format!("ROCKET_{}", key))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now we build a config file to test that the environment variables
|
|
|
|
// override configurations from files as well.
|
|
|
|
let toml = r#"
|
|
|
|
[dev]
|
|
|
|
address = "1.2.3.4"
|
|
|
|
|
|
|
|
[stage]
|
|
|
|
address = "2.3.4.5"
|
|
|
|
|
|
|
|
[prod]
|
|
|
|
address = "10.1.1.1"
|
|
|
|
|
|
|
|
[global]
|
|
|
|
address = "1.2.3.4"
|
|
|
|
port = 7810
|
|
|
|
workers = 21
|
|
|
|
log = "normal"
|
|
|
|
"#.to_string();
|
|
|
|
|
|
|
|
// Check that setting the environment variable actually changes the
|
|
|
|
// config for the default active environments.
|
|
|
|
for &(key, val) in &pairs {
|
|
|
|
env::set_var(format!("ROCKET_{}", key), val);
|
|
|
|
|
|
|
|
let r = RocketConfig::parse(toml.clone(), TEST_CONFIG_FILENAME).unwrap();
|
|
|
|
check_value(&*key.to_lowercase(), val, r.active());
|
|
|
|
|
|
|
|
// And non-active configs.
|
|
|
|
for env in &Environment::all() {
|
|
|
|
check_value(&*key.to_lowercase(), val, r.get(*env));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clear the variables so they don't override for the next test.
|
|
|
|
for &(key, _) in &pairs {
|
|
|
|
env::remove_var(format!("ROCKET_{}", key))
|
|
|
|
}
|
|
|
|
}
|
2016-10-03 10:39:56 +00:00
|
|
|
}
|