diff --git a/examples/content_types/src/main.rs b/examples/content_types/src/main.rs index 7dd0b473..aab6d371 100644 --- a/examples/content_types/src/main.rs +++ b/examples/content_types/src/main.rs @@ -5,7 +5,7 @@ extern crate rocket; extern crate serde_json; #[macro_use] extern crate serde_derive; -use rocket::{Rocket, Request, Error}; +use rocket::{Request, Error}; use rocket::http::ContentType; use rocket::response::data; @@ -39,7 +39,7 @@ fn not_found<'r>(_: Error, request: &'r Request<'r>) -> String { } fn main() { - let mut rocket = Rocket::new("0.0.0.0", 8000); + let mut rocket = rocket::ignite(); rocket.mount("/hello", routes![hello]).catch(errors![not_found]); rocket.launch(); } diff --git a/examples/cookies/src/main.rs b/examples/cookies/src/main.rs index 93d88171..d8129b41 100644 --- a/examples/cookies/src/main.rs +++ b/examples/cookies/src/main.rs @@ -6,7 +6,6 @@ extern crate lazy_static; extern crate rocket; extern crate tera; -use rocket::Rocket; use rocket::response::Redirect; use rocket::http::{Cookie, Cookies}; @@ -37,7 +36,7 @@ fn index(cookies: &Cookies) -> tera::TeraResult { } fn main() { - let mut rocket = Rocket::new("127.0.0.1", 8000); + let mut rocket = rocket::ignite(); rocket.mount("/", routes![submit, index]); rocket.launch(); } diff --git a/examples/errors/src/main.rs b/examples/errors/src/main.rs index 3f6d743f..7cf95183 100644 --- a/examples/errors/src/main.rs +++ b/examples/errors/src/main.rs @@ -2,7 +2,7 @@ #![plugin(rocket_codegen)] extern crate rocket; -use rocket::{Rocket, Error, Request}; +use rocket::{Error, Request}; #[get("/hello//")] fn hello(name: &str, age: i8) -> String { @@ -17,7 +17,7 @@ fn not_found<'r>(_error: Error, request: &'r Request<'r>) -> String { } fn main() { - let mut rocket = Rocket::new("localhost", 8000); + let mut rocket = rocket::ignite(); rocket.mount("/", routes![hello]); rocket.catch(errors![not_found]); rocket.launch(); diff --git a/examples/extended_validation/src/main.rs b/examples/extended_validation/src/main.rs index b2f1079c..9b676f24 100644 --- a/examples/extended_validation/src/main.rs +++ b/examples/extended_validation/src/main.rs @@ -5,7 +5,6 @@ extern crate rocket; mod files; -use rocket::Rocket; use rocket::response::Redirect; use rocket::form::FromFormValue; @@ -77,7 +76,7 @@ fn user_page(username: &str) -> String { } fn main() { - let mut rocket = Rocket::new("localhost", 8000); + let mut rocket = rocket::ignite(); rocket.mount("/", routes![files::index, files::files, user_page, login]); rocket.launch(); } diff --git a/examples/form_kitchen_sink/src/main.rs b/examples/form_kitchen_sink/src/main.rs index d0a7ce9f..8daeef83 100644 --- a/examples/form_kitchen_sink/src/main.rs +++ b/examples/form_kitchen_sink/src/main.rs @@ -3,7 +3,7 @@ extern crate rocket; -use rocket::{Rocket, Request}; +use rocket::Request; use rocket::response::NamedFile; use rocket::form::FromFormValue; use std::io; @@ -56,7 +56,7 @@ fn index() -> io::Result { } fn main() { - let mut rocket = Rocket::new("localhost", 8000); + let mut rocket = rocket::ignite(); rocket.mount("/", routes![index, sink, sink2]); rocket.launch(); } diff --git a/examples/forms/src/main.rs b/examples/forms/src/main.rs index 616f615d..48d08027 100644 --- a/examples/forms/src/main.rs +++ b/examples/forms/src/main.rs @@ -5,7 +5,6 @@ extern crate rocket; mod files; -use rocket::Rocket; use rocket::response::Redirect; #[derive(FromForm)] @@ -42,7 +41,7 @@ fn user_page(username: &str) -> String { } fn main() { - let mut rocket = Rocket::new("localhost", 8000); + let mut rocket = rocket::ignite(); rocket.mount("/", routes![files::index, files::files, user_page, login]); rocket.launch(); } diff --git a/examples/from_request/src/main.rs b/examples/from_request/src/main.rs index 2ad7b50f..e768c592 100644 --- a/examples/from_request/src/main.rs +++ b/examples/from_request/src/main.rs @@ -4,7 +4,6 @@ extern crate rocket; use std::fmt; -use rocket::Rocket; use rocket::request::{Request, FromRequest}; #[derive(Debug)] @@ -29,5 +28,5 @@ fn header_count(header_count: HeaderCount) -> String { } fn main() { - Rocket::new("localhost", 8000).mount_and_launch("/", routes![header_count]); + rocket::ignite().mount_and_launch("/", routes![header_count]); } diff --git a/examples/handlebars_templates/src/main.rs b/examples/handlebars_templates/src/main.rs index a75296da..e884f0bc 100644 --- a/examples/handlebars_templates/src/main.rs +++ b/examples/handlebars_templates/src/main.rs @@ -6,7 +6,7 @@ extern crate rocket; extern crate serde_json; #[macro_use] extern crate serde_derive; -use rocket::{Rocket, Request, Error}; +use rocket::{Request, Error}; use rocket::response::Redirect; use rocket_contrib::Template; @@ -39,7 +39,7 @@ fn not_found<'r>(_: Error, req: &'r Request<'r>) -> Template { } fn main() { - let mut rocket = Rocket::new("localhost", 8000); + let mut rocket = rocket::ignite(); rocket.catch(errors![not_found]); rocket.mount_and_launch("/", routes![index, get]); } diff --git a/examples/hello_person/src/main.rs b/examples/hello_person/src/main.rs index 597d6475..b29da845 100644 --- a/examples/hello_person/src/main.rs +++ b/examples/hello_person/src/main.rs @@ -2,7 +2,6 @@ #![plugin(rocket_codegen)] extern crate rocket; -use rocket::Rocket; #[get("/hello//")] fn hello(name: &str, age: i8) -> String { @@ -15,5 +14,5 @@ fn hi<'r>(name: &'r str) -> &'r str { } fn main() { - Rocket::new("localhost", 8000).mount_and_launch("/", routes![hello, hi]); + rocket::ignite().mount_and_launch("/", routes![hello, hi]); } diff --git a/examples/hello_ranks/src/main.rs b/examples/hello_ranks/src/main.rs index 4ec893d2..5192e7f3 100644 --- a/examples/hello_ranks/src/main.rs +++ b/examples/hello_ranks/src/main.rs @@ -2,7 +2,6 @@ #![plugin(rocket_codegen)] extern crate rocket; -use rocket::Rocket; #[get("/hello//")] fn hello(name: &str, age: i8) -> String { @@ -15,5 +14,5 @@ fn hi(name: &str, age: &str) -> String { } fn main() { - Rocket::new("localhost", 8000).mount_and_launch("/", routes![hi, hello]); + rocket::ignite().mount_and_launch("/", routes![hi, hello]); } diff --git a/examples/json/src/main.rs b/examples/json/src/main.rs index e2fb565e..09509b46 100644 --- a/examples/json/src/main.rs +++ b/examples/json/src/main.rs @@ -7,7 +7,7 @@ extern crate serde_json; #[macro_use] extern crate rocket_contrib; #[macro_use] extern crate serde_derive; -use rocket::{Rocket, Request, Error}; +use rocket::{Request, Error}; use rocket_contrib::JSON; use std::collections::HashMap; use std::sync::Mutex; @@ -86,7 +86,7 @@ fn not_found<'r>(_: Error, _: &'r Request<'r>) -> JSON { } fn main() { - let mut rocket = Rocket::new("localhost", 8000); + let mut rocket = rocket::ignite(); rocket.mount("/message", routes![new, update, get]); rocket.catch(errors![not_found]); rocket.launch(); diff --git a/examples/manual_routes/src/main.rs b/examples/manual_routes/src/main.rs index d0a60bad..1fe223b3 100644 --- a/examples/manual_routes/src/main.rs +++ b/examples/manual_routes/src/main.rs @@ -1,6 +1,6 @@ extern crate rocket; -use rocket::{Rocket, Request, Response, Route}; +use rocket::{Request, Response, Route}; use rocket::http::Method::*; fn root<'r>(req: &'r Request<'r>) -> Response<'r> { @@ -14,7 +14,7 @@ fn echo_url<'a>(req: &'a Request<'a>) -> Response<'a> { } fn main() { - let mut rocket = Rocket::new("localhost", 8000); + let mut rocket = rocket::ignite(); let first = Route::new(Get, "/hello", root); let second = Route::new(Get, "/hello/", root); diff --git a/examples/optional_redirect/src/main.rs b/examples/optional_redirect/src/main.rs index 17ec3ba3..e2c94e57 100644 --- a/examples/optional_redirect/src/main.rs +++ b/examples/optional_redirect/src/main.rs @@ -2,7 +2,6 @@ #![plugin(rocket_codegen)] extern crate rocket; -use rocket::Rocket; use rocket::response::Redirect; #[get("/")] @@ -24,6 +23,5 @@ fn login() -> &'static str { } fn main() { - let rocket = Rocket::new("localhost", 8000); - rocket.mount_and_launch("/", routes![root, user, login]); + rocket::ignite().mount_and_launch("/", routes![root, user, login]) } diff --git a/examples/optional_result/src/main.rs b/examples/optional_result/src/main.rs index b23adccf..680011f9 100644 --- a/examples/optional_result/src/main.rs +++ b/examples/optional_result/src/main.rs @@ -2,7 +2,6 @@ #![plugin(rocket_codegen)] extern crate rocket; -use rocket::Rocket; #[get("/users/")] fn user(name: &str) -> Option<&'static str> { @@ -14,5 +13,5 @@ fn user(name: &str) -> Option<&'static str> { } fn main() { - Rocket::new("localhost", 8000).mount_and_launch("/", routes![user]); + rocket::ignite().mount_and_launch("/", routes![user]); } diff --git a/examples/query_params/src/main.rs b/examples/query_params/src/main.rs index 85a0a42c..783fa42a 100644 --- a/examples/query_params/src/main.rs +++ b/examples/query_params/src/main.rs @@ -3,8 +3,6 @@ extern crate rocket; -use rocket::Rocket; - #[derive(FromForm)] struct Person<'r> { name: &'r str, @@ -21,5 +19,5 @@ fn hello(person: Person) -> String { } fn main() { - Rocket::new("localhost", 8000).mount_and_launch("/", routes![hello]); + rocket::ignite().mount_and_launch("/", routes![hello]); } diff --git a/examples/redirect/src/main.rs b/examples/redirect/src/main.rs index b6e0fd28..4b827e74 100644 --- a/examples/redirect/src/main.rs +++ b/examples/redirect/src/main.rs @@ -2,7 +2,6 @@ #![plugin(rocket_codegen)] extern crate rocket; -use rocket::Rocket; use rocket::response::Redirect; #[get("/")] @@ -16,5 +15,5 @@ fn login() -> &'static str { } fn main() { - Rocket::new("localhost", 8000).mount_and_launch("/", routes![root, login]); + rocket::ignite().mount_and_launch("/", routes![root, login]); } diff --git a/examples/static_files/src/main.rs b/examples/static_files/src/main.rs index f66cd249..184da75d 100644 --- a/examples/static_files/src/main.rs +++ b/examples/static_files/src/main.rs @@ -2,7 +2,6 @@ #![plugin(rocket_codegen)] extern crate rocket; -use rocket::Rocket; use std::io; use rocket::response::NamedFile; @@ -19,5 +18,5 @@ fn files(file: PathBuf) -> io::Result { } fn main() { - Rocket::new("localhost", 8000).mount_and_launch("/", routes![index, files]); + rocket::ignite().mount_and_launch("/", routes![index, files]); } diff --git a/examples/stream/src/main.rs b/examples/stream/src/main.rs index 9347f8ba..3825e256 100644 --- a/examples/stream/src/main.rs +++ b/examples/stream/src/main.rs @@ -3,7 +3,6 @@ extern crate rocket; -use rocket::Rocket; use rocket::response::{data, Stream}; use std::io::{self, repeat, Repeat, Read, Take}; @@ -24,5 +23,5 @@ fn file() -> io::Result> { } fn main() { - Rocket::new("localhost", 8000).mount_and_launch("/", routes![root, file]); + rocket::ignite().mount_and_launch("/", routes![root, file]); } diff --git a/examples/testing/src/main.rs b/examples/testing/src/main.rs index f9388b5e..806dec38 100644 --- a/examples/testing/src/main.rs +++ b/examples/testing/src/main.rs @@ -2,7 +2,6 @@ #![plugin(rocket_codegen)] extern crate rocket; -use rocket::Rocket; #[get("/")] fn hello() -> &'static str { @@ -10,7 +9,7 @@ fn hello() -> &'static str { } fn main() { - Rocket::new("localhost", 8000).mount_and_launch("/", routes![hello]); + rocket::ignite().mount_and_launch("/", routes![hello]); } #[cfg(test)] @@ -19,7 +18,7 @@ mod test { use super::rocket::http::Method; fn run_test(f: F) where F: Fn(Rocket) { - let mut rocket = Rocket::new("_", 0); + let mut rocket = Rocket::ignite(); rocket.mount("/", routes![super::hello]); f(rocket); } diff --git a/examples/todo/src/main.rs b/examples/todo/src/main.rs index f7f1836a..2ba1d664 100644 --- a/examples/todo/src/main.rs +++ b/examples/todo/src/main.rs @@ -11,7 +11,6 @@ extern crate serde_json; mod static_files; mod task; -use rocket::Rocket; use rocket::response::{Flash, Redirect}; use rocket_contrib::Template; use task::Task; @@ -69,7 +68,7 @@ fn index(msg: Option>) -> Template { } fn main() { - let mut rocket = Rocket::new("127.0.0.1", 8000); + let mut rocket = rocket::ignite(); rocket.mount("/", routes![index, static_files::all]) .mount("/todo/", routes![new, toggle, delete]); rocket.launch(); diff --git a/lib/src/config/mod.rs b/lib/src/config/mod.rs index fa8bdf39..4fc60483 100644 --- a/lib/src/config/mod.rs +++ b/lib/src/config/mod.rs @@ -134,6 +134,11 @@ impl RocketConfig { } } +/// 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 +/// the rocket config env variable. If there is a problem doing any of this, +/// print a nice error message and bail. pub fn read_or_default() -> RocketConfig { let bail = |e: ConfigError| -> ! { logger::init(LoggingLevel::Debug); diff --git a/lib/src/rocket.rs b/lib/src/rocket.rs index 3c48c7e9..35d77efc 100644 --- a/lib/src/rocket.rs +++ b/lib/src/rocket.rs @@ -7,7 +7,7 @@ use term_painter::Color::*; use term_painter::ToStyle; use config; -use logger::{self, LoggingLevel}; +use logger; use request::Request; use router::{Router, Route}; use catcher::{self, Catcher}; @@ -24,7 +24,6 @@ pub struct Rocket { port: usize, router: Router, catchers: HashMap, - log_set: bool, } impl HyperHandler for Rocket { @@ -125,18 +124,7 @@ impl Rocket { catcher.handle(Error::NoRoute, request).respond(response); } - pub fn new(address: S, port: usize) -> Rocket { - Rocket { - address: address.to_string(), - port: port, - router: Router::new(), - catchers: catcher::defaults::get(), - log_set: false, - } - } - pub fn mount(&mut self, base: &'static str, routes: Vec) -> &mut Self { - self.enable_normal_logging_if_disabled(); info!("🛰 {} '{}':", Magenta.paint("Mounting"), base); for mut route in routes { let path = format!("{}/{}", base, route.path.as_str()); @@ -150,7 +138,6 @@ impl Rocket { } pub fn catch(&mut self, catchers: Vec) -> &mut Self { - self.enable_normal_logging_if_disabled(); info!("👾 {}:", Magenta.paint("Catchers")); for c in catchers { if self.catchers.contains_key(&c.code) && @@ -167,32 +154,7 @@ impl Rocket { self } - fn enable_normal_logging_if_disabled(&mut self) { - if !self.log_set { - logger::init(LoggingLevel::Normal); - self.log_set = true; - } - } - - pub fn log(&mut self, level: LoggingLevel) { - if self.log_set { - warn!("Log level already set! Not overriding."); - } else { - logger::init(level); - self.log_set = true; - } - } - - /// Retrieves the configuration parameter named `name` for the current - /// environment. Returns Some(value) if the paremeter exists. Otherwise, - /// returns None. - pub fn config>(_name: S) -> Option<&'static str> { - // TODO: Implement me. - None - } - - pub fn launch(mut self) { - self.enable_normal_logging_if_disabled(); + pub fn launch(self) { if self.router.has_collisions() { warn!("Route collisions detected!"); } @@ -219,6 +181,14 @@ impl Rocket { self.launch(); } + /// Retrieves the configuration parameter named `name` for the current + /// environment. Returns Some(value) if the paremeter exists. Otherwise, + /// returns None. + pub fn config>(_name: S) -> Option<&'static str> { + // TODO: Implement me. + None + } + pub fn ignite() -> Rocket { // Note: read_or_default will exit the process under errors. let config = config::read_or_default(); @@ -237,7 +207,6 @@ impl Rocket { port: config.active().port, router: Router::new(), catchers: catcher::defaults::get(), - log_set: true, } } }