mirror of https://github.com/rwf2/Rocket.git
Use ignite in README example. Check ROCKET_ENV without Rocket.toml being present.
This commit is contained in:
parent
920bd35d46
commit
d631dfd300
|
@ -9,7 +9,6 @@ application:
|
|||
#![plugin(rocket_codegen)]
|
||||
|
||||
extern crate rocket;
|
||||
use rocket::Rocket;
|
||||
|
||||
#[get("/<name>/<age>")]
|
||||
fn hello(name: &str, age: u8) -> String {
|
||||
|
@ -17,7 +16,7 @@ fn hello(name: &str, age: u8) -> String {
|
|||
}
|
||||
|
||||
fn main() {
|
||||
Rocket::new("localhost", 8000).mount_and_launch("/hello", routes![hello]);
|
||||
rocket::ignite().mount_and_launch("/hello", routes![hello]);
|
||||
}
|
||||
```
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
# None of these are actually needed as Rocket has sane defaults for each. We
|
||||
# show all of them here explicitly for demonstrative purposes.
|
||||
# Except for the session key, none of these are actually needed; Rocket has sane
|
||||
# defaults. We show all of them here explicitly for demonstrative purposes.
|
||||
|
||||
[development]
|
||||
address = "localhost"
|
||||
|
|
|
@ -2,7 +2,6 @@
|
|||
#![plugin(rocket_codegen)]
|
||||
|
||||
extern crate rocket;
|
||||
use rocket::Rocket;
|
||||
|
||||
#[get("/")]
|
||||
fn hello() -> &'static str {
|
||||
|
@ -10,5 +9,5 @@ fn hello() -> &'static str {
|
|||
}
|
||||
|
||||
fn main() {
|
||||
Rocket::new("localhost", 8000).mount_and_launch("/hello", routes![hello]);
|
||||
rocket::ignite().mount_and_launch("/", routes![hello]);
|
||||
}
|
||||
|
|
|
@ -41,7 +41,7 @@ impl ConfigError {
|
|||
info_!("valid environments are: {}", White.paint(valid_envs));
|
||||
}
|
||||
BadEnv(ref name) => {
|
||||
error!("'{}' is not a valid ROCKET_ENV", name);
|
||||
error!("'{}' is not a valid ROCKET_ENV value", name);
|
||||
info_!("valid environments are: {}", White.paint(valid_envs));
|
||||
}
|
||||
BadType(ref name, ref expected, ref actual, ref filename) => {
|
||||
|
|
|
@ -2,20 +2,21 @@ mod error;
|
|||
mod environment;
|
||||
mod config;
|
||||
|
||||
pub use self::error::{ConfigError, ParsingError};
|
||||
pub use self::environment::Environment;
|
||||
|
||||
use toml::{self, Table};
|
||||
|
||||
use self::Environment::*;
|
||||
use self::config::Config;
|
||||
|
||||
use std::fs::{self, File};
|
||||
use std::collections::HashMap;
|
||||
use std::io::Read;
|
||||
use std::path::PathBuf;
|
||||
use std::process;
|
||||
use std::env;
|
||||
|
||||
pub use self::error::{ConfigError, ParsingError};
|
||||
pub use self::environment::Environment;
|
||||
use self::Environment::*;
|
||||
use self::config::Config;
|
||||
|
||||
use toml::{self, Table};
|
||||
use logger::{self, LoggingLevel};
|
||||
|
||||
const CONFIG_FILENAME: &'static str = "Rocket.toml";
|
||||
|
||||
#[derive(Debug)]
|
||||
|
@ -87,8 +88,7 @@ impl RocketConfig {
|
|||
))?;
|
||||
|
||||
// Create a config with the defaults, but the set the env to the active
|
||||
let mut config = RocketConfig::default();
|
||||
config.active_env = Environment::active()?;
|
||||
let mut config = RocketConfig::active_default()?;
|
||||
|
||||
// Parse the values from the TOML file.
|
||||
for (entry, value) in toml {
|
||||
|
@ -126,6 +126,31 @@ impl RocketConfig {
|
|||
// Parse the contents from the file.
|
||||
RocketConfig::parse(contents, &file.to_string_lossy())
|
||||
}
|
||||
|
||||
pub fn active_default() -> Result<RocketConfig, ConfigError> {
|
||||
let mut default = RocketConfig::default();
|
||||
default.active_env = Environment::active()?;
|
||||
Ok(default)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn read_or_default() -> RocketConfig {
|
||||
let bail = |e: ConfigError| -> ! {
|
||||
logger::init(LoggingLevel::Debug);
|
||||
e.pretty_print();
|
||||
process::exit(1)
|
||||
};
|
||||
|
||||
use self::ConfigError::*;
|
||||
RocketConfig::read().unwrap_or_else(|e| {
|
||||
match e {
|
||||
ParseError(..) | BadEntry(..) | BadEnv(..) | BadType(..) => bail(e),
|
||||
IOError | BadCWD => warn!("failed reading Rocket.toml. using defaults"),
|
||||
NotFound => { /* try using the default below */ }
|
||||
}
|
||||
|
||||
RocketConfig::active_default().unwrap_or_else(|e| bail(e))
|
||||
})
|
||||
}
|
||||
|
||||
impl Default for RocketConfig {
|
||||
|
|
|
@ -6,12 +6,12 @@ use std::process;
|
|||
use term_painter::Color::*;
|
||||
use term_painter::ToStyle;
|
||||
|
||||
use config;
|
||||
use logger::{self, LoggingLevel};
|
||||
use request::Request;
|
||||
use router::{Router, Route};
|
||||
use catcher::{self, Catcher};
|
||||
use response::Outcome;
|
||||
use config::RocketConfig;
|
||||
use form::FormItems;
|
||||
use error::Error;
|
||||
|
||||
|
@ -220,21 +220,8 @@ impl Rocket {
|
|||
}
|
||||
|
||||
pub fn ignite() -> Rocket {
|
||||
use config::ConfigError::*;
|
||||
let config = match RocketConfig::read() {
|
||||
Ok(config) => config,
|
||||
Err(e@ParseError(..)) | Err(e@BadEntry(..)) |
|
||||
Err(e@BadEnv(..)) | Err(e@BadType(..)) => {
|
||||
logger::init(LoggingLevel::Debug);
|
||||
e.pretty_print();
|
||||
process::exit(1)
|
||||
}
|
||||
Err(IOError) | Err(BadCWD) => {
|
||||
warn!("error reading Rocket config file; using defaults.");
|
||||
RocketConfig::default()
|
||||
}
|
||||
Err(NotFound) => RocketConfig::default()
|
||||
};
|
||||
// Note: read_or_default will exit the process under errors.
|
||||
let config = config::read_or_default();
|
||||
|
||||
logger::init(config.active().log_level);
|
||||
info!("🔧 Configured for {}.", config.active_env);
|
||||
|
|
Loading…
Reference in New Issue