[−][src]Struct rocket::Rocket
The main Rocket
type: used to mount routes and catchers and launch the
application.
Methods
impl Rocket
[src]
impl Rocket
pub fn ignite() -> Rocket
[src]
pub fn ignite() -> Rocket
Create a new Rocket
application using the configuration information in
Rocket.toml
. If the file does not exist or if there is an I/O error
reading the file, the defaults are used. See the
config documentation for more information
on defaults.
This method is typically called through the rocket::ignite
alias.
Panics
If there is an error parsing the Rocket.toml
file, this functions
prints a nice error message and then exits the process.
Examples
rocket::ignite()
pub fn custom(config: Config, log: bool) -> Rocket
[src]
pub fn custom(config: Config, log: bool) -> Rocket
Creates a new Rocket
application using the supplied custom
configuration information. The Rocket.toml
file, if present, is
ignored. Any environment variables setting config parameters are
ignored. If log
is true
, logging is enabled.
This method is typically called through the rocket::custom
alias.
Examples
use rocket::config::{Config, Environment}; let config = Config::build(Environment::Staging) .address("1.2.3.4") .port(9234) .finalize()?; let app = rocket::custom(config, false);
pub fn mount(self, base: &str, routes: Vec<Route>) -> Self
[src]
pub fn mount(self, base: &str, routes: Vec<Route>) -> Self
Mounts all of the routes in the supplied vector at the given base
path. Mounting a route with path path
at path base
makes the route
available at base/path
.
Panics
The base
mount point must be a static path. That is, the mount point
must not contain dynamic path parameters: <param>
.
Examples
Use the routes!
macro to mount routes created using the code
generation facilities. Requests to the /hello/world
URI will be
dispatched to the hi
route.
#[get("/world")] fn hi() -> &'static str { "Hello!" } fn main() { rocket::ignite().mount("/hello", routes![hi]) }
Manually create a route named hi
at path "/world"
mounted at base
"/hello"
. Requests to the /hello/world
URI will be dispatched to the
hi
route.
use rocket::{Request, Route, Data}; use rocket::handler::Outcome; use rocket::http::Method::*; fn hi(req: &Request, _: Data) -> Outcome<'static> { Outcome::from(req, "Hello!") } rocket::ignite().mount("/hello", vec![Route::new(Get, "/world", hi)])
pub fn catch(self, catchers: Vec<Catcher>) -> Self
[src]
pub fn catch(self, catchers: Vec<Catcher>) -> Self
Registers all of the catchers in the supplied vector.
Examples
#![feature(plugin)] #![plugin(rocket_codegen)] extern crate rocket; use rocket::Request; #[catch(500)] fn internal_error() -> &'static str { "Whoops! Looks like we messed up." } #[catch(400)] fn not_found(req: &Request) -> String { format!("I couldn't find '{}'. Try something else?", req.uri()) } fn main() { rocket::ignite().catch(catchers![internal_error, not_found]) }
pub fn manage<T: Send + Sync + 'static>(self, state: T) -> Self
[src]
pub fn manage<T: Send + Sync + 'static>(self, state: T) -> Self
Add state
to the state managed by this instance of Rocket.
This method can be called any number of times as long as each call
refers to a different T
.
Managed state can be retrieved by any request handler via the
State request guard. In particular, if a
value of type T
is managed by Rocket, adding State<T>
to the list of
arguments in a request handler instructs Rocket to retrieve the managed
value.
Panics
Panics if state of type T
is already being managed.
Example
use rocket::State; struct MyValue(usize); #[get("/")] fn index(state: State<MyValue>) -> String { format!("The stateful value is: {}", state.0) } fn main() { rocket::ignite() .mount("/", routes![index]) .manage(MyValue(10)) .launch(); }
pub fn attach<F: Fairing>(self, fairing: F) -> Self
[src]
pub fn attach<F: Fairing>(self, fairing: F) -> Self
Attaches a fairing to this instance of Rocket.
Example
use rocket::Rocket; use rocket::fairing::AdHoc; fn main() { rocket::ignite() .attach(AdHoc::on_launch(|_| println!("Rocket is launching!"))) .launch(); }
pub fn launch(self) -> LaunchError
[src]
pub fn launch(self) -> LaunchError
Starts the application server and begins listening for and dispatching requests to mounted routes and catchers. Unless there is an error, this function does not return and blocks until program termination.
Error
If there is a problem starting the application, a LaunchError
is
returned. Note that a value of type LaunchError
panics if dropped
without first being inspected. See the LaunchError
documentation for
more information.
Example
rocket::ignite().launch();
pub fn routes<'a>(
&'a self
) -> impl Iterator<Item = &'a Route> + 'a
[src]
pub fn routes<'a>(
&'a self
) -> impl Iterator<Item = &'a Route> + 'a
Returns an iterator over all of the routes mounted on this instance of Rocket.
Example
use rocket::Rocket; use rocket::fairing::AdHoc; #[get("/hello")] fn hello() -> &'static str { "Hello, world!" } fn main() { let rocket = rocket::ignite() .mount("/", routes![hello]) .mount("/hi", routes![hello]); for route in rocket.routes() { match route.base() { "/" => assert_eq!(route.uri.path(), "/hello"), "/hi" => assert_eq!(route.uri.path(), "/hi/hello"), _ => unreachable!("only /hello, /hi/hello are expected") } } assert_eq!(rocket.routes().count(), 2); }
pub fn state<T: Send + Sync + 'static>(&self) -> Option<&T>
[src]
pub fn state<T: Send + Sync + 'static>(&self) -> Option<&T>
Returns Some
of the managed state value for the type T
if it is
being managed by self
. Otherwise, returns None
.
Example
#[derive(PartialEq, Debug)] struct MyState(&'static str); let rocket = rocket::ignite().manage(MyState("hello!")); assert_eq!(rocket.state::<MyState>(), Some(&MyState("hello!"))); let client = rocket::local::Client::new(rocket).expect("valid rocket"); assert_eq!(client.rocket().state::<MyState>(), Some(&MyState("hello!")));
pub fn config(&self) -> &Config
[src]
pub fn config(&self) -> &Config
Returns the active configuration.
Example
use rocket::Rocket; use rocket::fairing::AdHoc; fn main() { rocket::ignite() .attach(AdHoc::on_launch(|rocket| { println!("Rocket launch config: {:?}", rocket.config()); })) .launch(); }
Auto Trait Implementations
Blanket Implementations
impl<T, U> Into for T where
U: From<T>,
[src]
impl<T, U> Into for T where
U: From<T>,
impl<T> From for T
[src]
impl<T> From for T
impl<T, U> TryFrom for T where
T: From<U>,
[src]
impl<T, U> TryFrom for T where
T: From<U>,
type Error = !
try_from
)The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
try_from
)Performs the conversion.
impl<T> Borrow for T where
T: ?Sized,
[src]
impl<T> Borrow for T where
T: ?Sized,
impl<T> BorrowMut for T where
T: ?Sized,
[src]
impl<T> BorrowMut for T where
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
impl<T, U> TryInto for T where
U: TryFrom<T>,
[src]
impl<T, U> TryInto for T where
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
try_from
)The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
try_from
)Performs the conversion.
impl<T> Any for T where
T: 'static + ?Sized,
[src]
impl<T> Any for T where
T: 'static + ?Sized,
fn get_type_id(&self) -> TypeId
[src]
fn get_type_id(&self) -> TypeId
🔬 This is a nightly-only experimental API. (get_type_id
)
this method will likely be replaced by an associated static
Gets the TypeId
of self
. Read more
impl<T> Typeable for T where
T: Any,
impl<T> Typeable for T where
T: Any,
impl<F> Handler for F where
F: Fn(Request, Response<Fresh>) + Sync + Send,
[src]
impl<F> Handler for F where
F: Fn(Request, Response<Fresh>) + Sync + Send,
fn handle(&'a self, req: Request<'a, 'k>, res: Response<'a, Fresh>)
[src]
fn handle(&'a self, req: Request<'a, 'k>, res: Response<'a, Fresh>)
Receives a Request
/Response
pair, and should perform some action on them. Read more
fn check_continue(&self, (&Method, &RequestUri, &Headers)) -> StatusCode
[src]
fn check_continue(&self, (&Method, &RequestUri, &Headers)) -> StatusCode
Called when a Request includes a Expect: 100-continue
header. Read more
fn on_connection_start(&self)
[src]
fn on_connection_start(&self)
This is run after a connection is received, on a per-connection basis (not a per-request basis, as a connection with keep-alive may handle multiple requests) Read more
fn on_connection_end(&self)
[src]
fn on_connection_end(&self)
This is run before a connection is closed, on a per-connection basis (not a per-request basis, as a connection with keep-alive may handle multiple requests) Read more