2017-05-19 10:29:08 +00:00
|
|
|
use rocket::{self, State};
|
|
|
|
use rocket::fairing::AdHoc;
|
2017-06-19 04:06:41 +00:00
|
|
|
use rocket::config::{self, Config, Environment, LoggingLevel};
|
2017-06-06 20:41:04 +00:00
|
|
|
use rocket::http::Status;
|
|
|
|
use rocket::local::Client;
|
2016-12-27 22:35:14 +00:00
|
|
|
|
2017-05-19 10:29:08 +00:00
|
|
|
struct LocalConfig(Config);
|
|
|
|
|
|
|
|
#[get("/check_config")]
|
2019-06-13 02:41:29 +00:00
|
|
|
fn check_config(config: State<'_, LocalConfig>) -> Option<()> {
|
|
|
|
let environment = match std::env::var("ROCKET_ENV") {
|
2017-05-19 10:29:08 +00:00
|
|
|
Ok(name) => name,
|
|
|
|
Err(_) => return None
|
|
|
|
};
|
2016-12-27 22:35:14 +00:00
|
|
|
|
2017-05-19 10:29:08 +00:00
|
|
|
let config = &config.0;
|
|
|
|
match &*environment {
|
|
|
|
"development" => {
|
2016-12-27 22:35:14 +00:00
|
|
|
assert_eq!(config.address, "localhost".to_string());
|
|
|
|
assert_eq!(config.port, 8000);
|
2017-01-12 10:47:25 +00:00
|
|
|
assert_eq!(config.workers, 1);
|
2016-12-27 22:35:14 +00:00
|
|
|
assert_eq!(config.log_level, LoggingLevel::Normal);
|
2017-01-12 10:47:25 +00:00
|
|
|
assert_eq!(config.environment, config::Environment::Development);
|
2016-12-27 22:35:14 +00:00
|
|
|
assert_eq!(config.extras().count(), 2);
|
|
|
|
assert_eq!(config.get_str("hi"), Ok("Hello!"));
|
|
|
|
assert_eq!(config.get_bool("is_extra"), Ok(true));
|
|
|
|
}
|
2017-05-19 10:29:08 +00:00
|
|
|
"staging" => {
|
2016-12-27 22:35:14 +00:00
|
|
|
assert_eq!(config.address, "0.0.0.0".to_string());
|
2017-10-05 19:59:06 +00:00
|
|
|
assert_eq!(config.port, 8000);
|
2017-01-12 10:47:25 +00:00
|
|
|
assert_eq!(config.workers, 8);
|
2016-12-27 22:35:14 +00:00
|
|
|
assert_eq!(config.log_level, LoggingLevel::Normal);
|
2017-01-12 10:47:25 +00:00
|
|
|
assert_eq!(config.environment, config::Environment::Staging);
|
2016-12-27 22:35:14 +00:00
|
|
|
assert_eq!(config.extras().count(), 0);
|
|
|
|
}
|
2017-05-19 10:29:08 +00:00
|
|
|
"production" => {
|
2016-12-27 22:35:14 +00:00
|
|
|
assert_eq!(config.address, "0.0.0.0".to_string());
|
2017-10-05 19:59:06 +00:00
|
|
|
assert_eq!(config.port, 8000);
|
2017-01-12 10:47:25 +00:00
|
|
|
assert_eq!(config.workers, 12);
|
2016-12-27 22:35:14 +00:00
|
|
|
assert_eq!(config.log_level, LoggingLevel::Critical);
|
2017-01-12 10:47:25 +00:00
|
|
|
assert_eq!(config.environment, config::Environment::Production);
|
2016-12-27 22:35:14 +00:00
|
|
|
assert_eq!(config.extras().count(), 0);
|
|
|
|
}
|
2017-05-19 10:29:08 +00:00
|
|
|
_ => {
|
|
|
|
panic!("Unknown environment in envvar: {}", environment);
|
|
|
|
}
|
2016-12-27 22:35:14 +00:00
|
|
|
}
|
2017-05-19 10:29:08 +00:00
|
|
|
|
|
|
|
Some(())
|
2016-12-27 22:35:14 +00:00
|
|
|
}
|
|
|
|
|
2017-05-19 10:29:08 +00:00
|
|
|
pub fn test_config(environment: Environment) {
|
|
|
|
// Manually set the config environment variable. Rocket will initialize the
|
|
|
|
// environment in `ignite()`. We'll read this back in the handler to config.
|
2019-06-13 02:41:29 +00:00
|
|
|
std::env::set_var("ROCKET_ENV", environment.to_string());
|
2017-05-19 10:29:08 +00:00
|
|
|
|
|
|
|
let rocket = rocket::ignite()
|
2018-08-14 16:14:06 +00:00
|
|
|
.attach(AdHoc::on_attach("Local Config", |rocket| {
|
2017-05-19 10:29:08 +00:00
|
|
|
println!("Attaching local config.");
|
|
|
|
let config = rocket.config().clone();
|
|
|
|
Ok(rocket.manage(LocalConfig(config)))
|
|
|
|
}))
|
|
|
|
.mount("/", routes![check_config]);
|
2016-12-27 22:35:14 +00:00
|
|
|
|
2017-06-06 20:41:04 +00:00
|
|
|
let client = Client::new(rocket).unwrap();
|
|
|
|
let response = client.get("/check_config").dispatch();
|
2017-05-19 10:29:08 +00:00
|
|
|
assert_eq!(response.status(), Status::Ok);
|
2016-12-27 22:35:14 +00:00
|
|
|
}
|