Remove Rocket::new(). Use 'ignite' everywhere.

This commit is contained in:
Sergio Benitez 2016-10-03 19:37:49 -07:00
parent d631dfd300
commit 7b1dc5a1a4
22 changed files with 42 additions and 84 deletions

View File

@ -5,7 +5,7 @@ extern crate rocket;
extern crate serde_json; extern crate serde_json;
#[macro_use] extern crate serde_derive; #[macro_use] extern crate serde_derive;
use rocket::{Rocket, Request, Error}; use rocket::{Request, Error};
use rocket::http::ContentType; use rocket::http::ContentType;
use rocket::response::data; use rocket::response::data;
@ -39,7 +39,7 @@ fn not_found<'r>(_: Error, request: &'r Request<'r>) -> String {
} }
fn main() { 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.mount("/hello", routes![hello]).catch(errors![not_found]);
rocket.launch(); rocket.launch();
} }

View File

@ -6,7 +6,6 @@ extern crate lazy_static;
extern crate rocket; extern crate rocket;
extern crate tera; extern crate tera;
use rocket::Rocket;
use rocket::response::Redirect; use rocket::response::Redirect;
use rocket::http::{Cookie, Cookies}; use rocket::http::{Cookie, Cookies};
@ -37,7 +36,7 @@ fn index(cookies: &Cookies) -> tera::TeraResult<String> {
} }
fn main() { fn main() {
let mut rocket = Rocket::new("127.0.0.1", 8000); let mut rocket = rocket::ignite();
rocket.mount("/", routes![submit, index]); rocket.mount("/", routes![submit, index]);
rocket.launch(); rocket.launch();
} }

View File

@ -2,7 +2,7 @@
#![plugin(rocket_codegen)] #![plugin(rocket_codegen)]
extern crate rocket; extern crate rocket;
use rocket::{Rocket, Error, Request}; use rocket::{Error, Request};
#[get("/hello/<name>/<age>")] #[get("/hello/<name>/<age>")]
fn hello(name: &str, age: i8) -> String { fn hello(name: &str, age: i8) -> String {
@ -17,7 +17,7 @@ fn not_found<'r>(_error: Error, request: &'r Request<'r>) -> String {
} }
fn main() { fn main() {
let mut rocket = Rocket::new("localhost", 8000); let mut rocket = rocket::ignite();
rocket.mount("/", routes![hello]); rocket.mount("/", routes![hello]);
rocket.catch(errors![not_found]); rocket.catch(errors![not_found]);
rocket.launch(); rocket.launch();

View File

@ -5,7 +5,6 @@ extern crate rocket;
mod files; mod files;
use rocket::Rocket;
use rocket::response::Redirect; use rocket::response::Redirect;
use rocket::form::FromFormValue; use rocket::form::FromFormValue;
@ -77,7 +76,7 @@ fn user_page(username: &str) -> String {
} }
fn main() { 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.mount("/", routes![files::index, files::files, user_page, login]);
rocket.launch(); rocket.launch();
} }

View File

@ -3,7 +3,7 @@
extern crate rocket; extern crate rocket;
use rocket::{Rocket, Request}; use rocket::Request;
use rocket::response::NamedFile; use rocket::response::NamedFile;
use rocket::form::FromFormValue; use rocket::form::FromFormValue;
use std::io; use std::io;
@ -56,7 +56,7 @@ fn index() -> io::Result<NamedFile> {
} }
fn main() { fn main() {
let mut rocket = Rocket::new("localhost", 8000); let mut rocket = rocket::ignite();
rocket.mount("/", routes![index, sink, sink2]); rocket.mount("/", routes![index, sink, sink2]);
rocket.launch(); rocket.launch();
} }

View File

@ -5,7 +5,6 @@ extern crate rocket;
mod files; mod files;
use rocket::Rocket;
use rocket::response::Redirect; use rocket::response::Redirect;
#[derive(FromForm)] #[derive(FromForm)]
@ -42,7 +41,7 @@ fn user_page(username: &str) -> String {
} }
fn main() { 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.mount("/", routes![files::index, files::files, user_page, login]);
rocket.launch(); rocket.launch();
} }

View File

@ -4,7 +4,6 @@
extern crate rocket; extern crate rocket;
use std::fmt; use std::fmt;
use rocket::Rocket;
use rocket::request::{Request, FromRequest}; use rocket::request::{Request, FromRequest};
#[derive(Debug)] #[derive(Debug)]
@ -29,5 +28,5 @@ fn header_count(header_count: HeaderCount) -> String {
} }
fn main() { fn main() {
Rocket::new("localhost", 8000).mount_and_launch("/", routes![header_count]); rocket::ignite().mount_and_launch("/", routes![header_count]);
} }

View File

@ -6,7 +6,7 @@ extern crate rocket;
extern crate serde_json; extern crate serde_json;
#[macro_use] extern crate serde_derive; #[macro_use] extern crate serde_derive;
use rocket::{Rocket, Request, Error}; use rocket::{Request, Error};
use rocket::response::Redirect; use rocket::response::Redirect;
use rocket_contrib::Template; use rocket_contrib::Template;
@ -39,7 +39,7 @@ fn not_found<'r>(_: Error, req: &'r Request<'r>) -> Template {
} }
fn main() { fn main() {
let mut rocket = Rocket::new("localhost", 8000); let mut rocket = rocket::ignite();
rocket.catch(errors![not_found]); rocket.catch(errors![not_found]);
rocket.mount_and_launch("/", routes![index, get]); rocket.mount_and_launch("/", routes![index, get]);
} }

View File

@ -2,7 +2,6 @@
#![plugin(rocket_codegen)] #![plugin(rocket_codegen)]
extern crate rocket; extern crate rocket;
use rocket::Rocket;
#[get("/hello/<name>/<age>")] #[get("/hello/<name>/<age>")]
fn hello(name: &str, age: i8) -> String { fn hello(name: &str, age: i8) -> String {
@ -15,5 +14,5 @@ fn hi<'r>(name: &'r str) -> &'r str {
} }
fn main() { fn main() {
Rocket::new("localhost", 8000).mount_and_launch("/", routes![hello, hi]); rocket::ignite().mount_and_launch("/", routes![hello, hi]);
} }

View File

@ -2,7 +2,6 @@
#![plugin(rocket_codegen)] #![plugin(rocket_codegen)]
extern crate rocket; extern crate rocket;
use rocket::Rocket;
#[get("/hello/<name>/<age>")] #[get("/hello/<name>/<age>")]
fn hello(name: &str, age: i8) -> String { fn hello(name: &str, age: i8) -> String {
@ -15,5 +14,5 @@ fn hi(name: &str, age: &str) -> String {
} }
fn main() { fn main() {
Rocket::new("localhost", 8000).mount_and_launch("/", routes![hi, hello]); rocket::ignite().mount_and_launch("/", routes![hi, hello]);
} }

View File

@ -7,7 +7,7 @@ extern crate serde_json;
#[macro_use] extern crate rocket_contrib; #[macro_use] extern crate rocket_contrib;
#[macro_use] extern crate serde_derive; #[macro_use] extern crate serde_derive;
use rocket::{Rocket, Request, Error}; use rocket::{Request, Error};
use rocket_contrib::JSON; use rocket_contrib::JSON;
use std::collections::HashMap; use std::collections::HashMap;
use std::sync::Mutex; use std::sync::Mutex;
@ -86,7 +86,7 @@ fn not_found<'r>(_: Error, _: &'r Request<'r>) -> JSON<SimpleMap> {
} }
fn main() { fn main() {
let mut rocket = Rocket::new("localhost", 8000); let mut rocket = rocket::ignite();
rocket.mount("/message", routes![new, update, get]); rocket.mount("/message", routes![new, update, get]);
rocket.catch(errors![not_found]); rocket.catch(errors![not_found]);
rocket.launch(); rocket.launch();

View File

@ -1,6 +1,6 @@
extern crate rocket; extern crate rocket;
use rocket::{Rocket, Request, Response, Route}; use rocket::{Request, Response, Route};
use rocket::http::Method::*; use rocket::http::Method::*;
fn root<'r>(req: &'r Request<'r>) -> Response<'r> { 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() { fn main() {
let mut rocket = Rocket::new("localhost", 8000); let mut rocket = rocket::ignite();
let first = Route::new(Get, "/hello", root); let first = Route::new(Get, "/hello", root);
let second = Route::new(Get, "/hello/<any>", root); let second = Route::new(Get, "/hello/<any>", root);

View File

@ -2,7 +2,6 @@
#![plugin(rocket_codegen)] #![plugin(rocket_codegen)]
extern crate rocket; extern crate rocket;
use rocket::Rocket;
use rocket::response::Redirect; use rocket::response::Redirect;
#[get("/")] #[get("/")]
@ -24,6 +23,5 @@ fn login() -> &'static str {
} }
fn main() { fn main() {
let rocket = Rocket::new("localhost", 8000); rocket::ignite().mount_and_launch("/", routes![root, user, login])
rocket.mount_and_launch("/", routes![root, user, login]);
} }

View File

@ -2,7 +2,6 @@
#![plugin(rocket_codegen)] #![plugin(rocket_codegen)]
extern crate rocket; extern crate rocket;
use rocket::Rocket;
#[get("/users/<name>")] #[get("/users/<name>")]
fn user(name: &str) -> Option<&'static str> { fn user(name: &str) -> Option<&'static str> {
@ -14,5 +13,5 @@ fn user(name: &str) -> Option<&'static str> {
} }
fn main() { fn main() {
Rocket::new("localhost", 8000).mount_and_launch("/", routes![user]); rocket::ignite().mount_and_launch("/", routes![user]);
} }

View File

@ -3,8 +3,6 @@
extern crate rocket; extern crate rocket;
use rocket::Rocket;
#[derive(FromForm)] #[derive(FromForm)]
struct Person<'r> { struct Person<'r> {
name: &'r str, name: &'r str,
@ -21,5 +19,5 @@ fn hello(person: Person) -> String {
} }
fn main() { fn main() {
Rocket::new("localhost", 8000).mount_and_launch("/", routes![hello]); rocket::ignite().mount_and_launch("/", routes![hello]);
} }

View File

@ -2,7 +2,6 @@
#![plugin(rocket_codegen)] #![plugin(rocket_codegen)]
extern crate rocket; extern crate rocket;
use rocket::Rocket;
use rocket::response::Redirect; use rocket::response::Redirect;
#[get("/")] #[get("/")]
@ -16,5 +15,5 @@ fn login() -> &'static str {
} }
fn main() { fn main() {
Rocket::new("localhost", 8000).mount_and_launch("/", routes![root, login]); rocket::ignite().mount_and_launch("/", routes![root, login]);
} }

View File

@ -2,7 +2,6 @@
#![plugin(rocket_codegen)] #![plugin(rocket_codegen)]
extern crate rocket; extern crate rocket;
use rocket::Rocket;
use std::io; use std::io;
use rocket::response::NamedFile; use rocket::response::NamedFile;
@ -19,5 +18,5 @@ fn files(file: PathBuf) -> io::Result<NamedFile> {
} }
fn main() { fn main() {
Rocket::new("localhost", 8000).mount_and_launch("/", routes![index, files]); rocket::ignite().mount_and_launch("/", routes![index, files]);
} }

View File

@ -3,7 +3,6 @@
extern crate rocket; extern crate rocket;
use rocket::Rocket;
use rocket::response::{data, Stream}; use rocket::response::{data, Stream};
use std::io::{self, repeat, Repeat, Read, Take}; use std::io::{self, repeat, Repeat, Read, Take};
@ -24,5 +23,5 @@ fn file() -> io::Result<Stream<File>> {
} }
fn main() { fn main() {
Rocket::new("localhost", 8000).mount_and_launch("/", routes![root, file]); rocket::ignite().mount_and_launch("/", routes![root, file]);
} }

View File

@ -2,7 +2,6 @@
#![plugin(rocket_codegen)] #![plugin(rocket_codegen)]
extern crate rocket; extern crate rocket;
use rocket::Rocket;
#[get("/")] #[get("/")]
fn hello() -> &'static str { fn hello() -> &'static str {
@ -10,7 +9,7 @@ fn hello() -> &'static str {
} }
fn main() { fn main() {
Rocket::new("localhost", 8000).mount_and_launch("/", routes![hello]); rocket::ignite().mount_and_launch("/", routes![hello]);
} }
#[cfg(test)] #[cfg(test)]
@ -19,7 +18,7 @@ mod test {
use super::rocket::http::Method; use super::rocket::http::Method;
fn run_test<F>(f: F) where F: Fn(Rocket) { fn run_test<F>(f: F) where F: Fn(Rocket) {
let mut rocket = Rocket::new("_", 0); let mut rocket = Rocket::ignite();
rocket.mount("/", routes![super::hello]); rocket.mount("/", routes![super::hello]);
f(rocket); f(rocket);
} }

View File

@ -11,7 +11,6 @@ extern crate serde_json;
mod static_files; mod static_files;
mod task; mod task;
use rocket::Rocket;
use rocket::response::{Flash, Redirect}; use rocket::response::{Flash, Redirect};
use rocket_contrib::Template; use rocket_contrib::Template;
use task::Task; use task::Task;
@ -69,7 +68,7 @@ fn index(msg: Option<Flash<()>>) -> Template {
} }
fn main() { fn main() {
let mut rocket = Rocket::new("127.0.0.1", 8000); let mut rocket = rocket::ignite();
rocket.mount("/", routes![index, static_files::all]) rocket.mount("/", routes![index, static_files::all])
.mount("/todo/", routes![new, toggle, delete]); .mount("/todo/", routes![new, toggle, delete]);
rocket.launch(); rocket.launch();

View File

@ -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 { pub fn read_or_default() -> RocketConfig {
let bail = |e: ConfigError| -> ! { let bail = |e: ConfigError| -> ! {
logger::init(LoggingLevel::Debug); logger::init(LoggingLevel::Debug);

View File

@ -7,7 +7,7 @@ use term_painter::Color::*;
use term_painter::ToStyle; use term_painter::ToStyle;
use config; use config;
use logger::{self, LoggingLevel}; use logger;
use request::Request; use request::Request;
use router::{Router, Route}; use router::{Router, Route};
use catcher::{self, Catcher}; use catcher::{self, Catcher};
@ -24,7 +24,6 @@ pub struct Rocket {
port: usize, port: usize,
router: Router, router: Router,
catchers: HashMap<u16, Catcher>, catchers: HashMap<u16, Catcher>,
log_set: bool,
} }
impl HyperHandler for Rocket { impl HyperHandler for Rocket {
@ -125,18 +124,7 @@ impl Rocket {
catcher.handle(Error::NoRoute, request).respond(response); catcher.handle(Error::NoRoute, request).respond(response);
} }
pub fn new<S: ToString>(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<Route>) -> &mut Self { pub fn mount(&mut self, base: &'static str, routes: Vec<Route>) -> &mut Self {
self.enable_normal_logging_if_disabled();
info!("🛰 {} '{}':", Magenta.paint("Mounting"), base); info!("🛰 {} '{}':", Magenta.paint("Mounting"), base);
for mut route in routes { for mut route in routes {
let path = format!("{}/{}", base, route.path.as_str()); let path = format!("{}/{}", base, route.path.as_str());
@ -150,7 +138,6 @@ impl Rocket {
} }
pub fn catch(&mut self, catchers: Vec<Catcher>) -> &mut Self { pub fn catch(&mut self, catchers: Vec<Catcher>) -> &mut Self {
self.enable_normal_logging_if_disabled();
info!("👾 {}:", Magenta.paint("Catchers")); info!("👾 {}:", Magenta.paint("Catchers"));
for c in catchers { for c in catchers {
if self.catchers.contains_key(&c.code) && if self.catchers.contains_key(&c.code) &&
@ -167,32 +154,7 @@ impl Rocket {
self self
} }
fn enable_normal_logging_if_disabled(&mut self) { pub fn launch(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<S: AsRef<str>>(_name: S) -> Option<&'static str> {
// TODO: Implement me.
None
}
pub fn launch(mut self) {
self.enable_normal_logging_if_disabled();
if self.router.has_collisions() { if self.router.has_collisions() {
warn!("Route collisions detected!"); warn!("Route collisions detected!");
} }
@ -219,6 +181,14 @@ impl Rocket {
self.launch(); 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<S: AsRef<str>>(_name: S) -> Option<&'static str> {
// TODO: Implement me.
None
}
pub fn ignite() -> Rocket { pub fn ignite() -> Rocket {
// Note: read_or_default will exit the process under errors. // Note: read_or_default will exit the process under errors.
let config = config::read_or_default(); let config = config::read_or_default();
@ -237,7 +207,6 @@ impl Rocket {
port: config.active().port, port: config.active().port,
router: Router::new(), router: Router::new(),
catchers: catcher::defaults::get(), catchers: catcher::defaults::get(),
log_set: true,
} }
} }
} }