Rocket/core/lib/src/lib.rs

159 lines
4.8 KiB
Rust
Raw Normal View History

2018-10-06 04:56:46 +00:00
#![feature(proc_macro_hygiene)]
#![feature(async_await)]
2018-06-04 16:06:08 +00:00
#![recursion_limit="256"]
2019-05-13 23:18:48 +00:00
#![doc(html_root_url = "https://api.rocket.rs/v0.5")]
2020-06-11 09:30:14 +00:00
#![doc(html_favicon_url = "https://rocket.rs/images/favicon.ico")]
#![doc(html_logo_url = "https://rocket.rs/images/logo-boxed.png")]
2019-06-13 01:48:02 +00:00
#![warn(rust_2018_idioms)]
//! # Rocket - Core API Documentation
2016-09-30 04:44:27 +00:00
//!
//! Hello, and welcome to the core Rocket API documentation!
//!
//! This API documentation is highly technical and is purely a reference.
//! There's an [overview] of Rocket on the main site as well as a [full,
//! detailed guide]. If you'd like pointers on getting started, see the
//! [quickstart] or [getting started] chapters of the guide.
2016-09-30 04:44:27 +00:00
//!
2018-10-07 04:16:02 +00:00
//! You may also be interested in looking at the
//! [`rocket_contrib`](../rocket_contrib) documentation, which contains
//! automatic JSON (de)serialiazation, templating support, static file serving,
//! and other useful features.
//!
2019-05-13 23:18:48 +00:00
//! [overview]: https://rocket.rs/v0.5/overview
//! [full, detailed guide]: https://rocket.rs/v0.5/guide
//! [quickstart]: https://rocket.rs/v0.5/guide/quickstart
//! [getting started]: https://rocket.rs/v0.5/guide/getting-started
//!
//! ## Libraries
//!
//! Rocket's functionality is split into two crates:
//!
//! 1. Core - This core library. Needed by every Rocket application.
2018-10-07 04:16:02 +00:00
//! 2. [Contrib](../rocket_contrib) - Provides useful functionality for many
//! Rocket applications. Completely optional.
//!
//! ## Usage
//!
//! First, depend on `rocket` in `Cargo.toml`:
//!
//! ```toml
//! [dependencies]
2019-05-13 23:18:48 +00:00
//! rocket = "0.5.0-dev"
//! ```
//!
//! Then, add the following to the top of your `main.rs` file:
//!
2016-10-18 02:29:58 +00:00
//! ```rust
//! #![feature(proc_macro_hygiene, async_await)]
//!
//! #[macro_use] extern crate rocket;
//! # #[get("/")] fn hello() { }
//! # fn main() { rocket::ignite().mount("/", routes![hello]); }
//! ```
//!
2019-05-13 23:18:48 +00:00
//! See the [guide](https://rocket.rs/v0.5/guide) for more information on how to
2016-10-18 02:29:58 +00:00
//! write Rocket applications. Here's a simple example to get you started:
//!
//! ```rust
//! #![feature(proc_macro_hygiene, async_await)]
2016-10-18 02:29:58 +00:00
//!
//! #[macro_use] extern crate rocket;
2016-10-18 02:29:58 +00:00
//!
//! #[get("/")]
//! fn hello() -> &'static str {
//! "Hello, world!"
//! }
//!
//! fn main() {
//! # if false { // We don't actually want to launch the server in an example.
//! rocket::ignite().mount("/", routes![hello]).launch();
2016-10-18 02:29:58 +00:00
//! # }
//! }
//! ```
//!
//! ## Configuration
//!
//! Rocket and Rocket libraries are configured via the `Rocket.toml` file and/or
//! `ROCKET_{PARAM}` environment variables. For more information on how to
//! configure Rocket, see the [configuration section] of the guide as well as
//! the [`config`] module documentation.
//!
2019-05-13 23:18:48 +00:00
//! [configuration section]: https://rocket.rs/v0.5/guide/configuration/
2016-10-18 02:29:58 +00:00
//!
//! ## Testing
//!
//! The [`local`] module contains structures that facilitate unit and
//! integration testing of a Rocket application. The top-level [`local`] module
//! documentation and the [testing chapter of the guide] include detailed
//! examples.
//!
2019-05-13 23:18:48 +00:00
//! [testing chapter of the guide]: https://rocket.rs/v0.5/guide/testing/#testing
2018-10-22 06:53:09 +00:00
#[allow(unused_imports)] #[macro_use] extern crate rocket_codegen;
2020-02-26 00:56:59 +00:00
pub use rocket_codegen::*;
#[macro_use] extern crate log;
#[macro_use] extern crate pear;
#[doc(hidden)] #[macro_use] pub mod logger;
#[macro_use] pub mod outcome;
Remove Session in favor of private cookies. New testing API. Sessions -------- This commit removes the `Session` type in favor of methods on the `Cookies` types that allow for adding, removing, and getting private (signed and encrypted) cookies. These methods provide a superset of the functionality of `Session` while also being a minimal addition to the existing API. They can be used to implement the previous `Session` type as well as other forms of session storage. The new methods are: * Cookie::add_private(&mut self, Cookie) * Cookie::remove_private(&mut self, Cookie) * Cookie::get_private(&self, &str) Resolves #20 Testing ------- This commit removes the `rocket::testing` module. It adds the `rocket::local` module which provides a `Client` type for local dispatching of requests against a `Rocket` instance. This `local` package subsumes the previous `testing` package. Rocket Examples --------------- The `forms`, `optional_result`, and `hello_alt_methods` examples have been removed. The following example have been renamed: * extended_validation -> form_validation * hello_ranks -> ranking * from_request -> request_guard * hello_tls -> tls Other Changes ------------- This commit also includes the following smaller changes: * Config::{development, staging, production} constructors have been added for easier creation of default `Config` structures. * The `Config` type is exported from the root. * `Request` implements `Clone` and `Debug`. * `Request::new` is no longer exported. * A `Response::body_bytes` method was added to easily retrieve a response's body as a `Vec<u8>`.
2017-06-06 20:41:04 +00:00
pub mod local;
pub mod request;
pub mod response;
pub mod config;
pub mod data;
pub mod handler;
pub mod fairing;
pub mod error;
// Reexport of HTTP everything.
pub mod http {
//! Types that map to concepts in HTTP.
//!
//! This module exports types that map to HTTP concepts or to the underlying
//! HTTP library when needed.
#[doc(inline)]
pub use rocket_http::*;
}
mod router;
mod rocket;
mod codegen;
mod catcher;
2018-10-23 20:22:26 +00:00
mod ext;
2019-06-13 01:48:02 +00:00
#[doc(inline)] pub use crate::response::Response;
#[doc(inline)] pub use crate::handler::{Handler, ErrorHandler};
#[doc(hidden)] pub use crate::codegen::{StaticRouteInfo, StaticCatchInfo};
#[doc(inline)] pub use crate::outcome::Outcome;
#[doc(inline)] pub use crate::data::Data;
#[doc(inline)] pub use crate::config::Config;
pub use crate::router::Route;
pub use crate::request::{Request, State};
pub use crate::catcher::Catcher;
pub use crate::rocket::Rocket;
pub use ext::AsyncReadExt;
/// Alias to [`Rocket::ignite()`] Creates a new instance of `Rocket`.
pub fn ignite() -> Rocket {
Rocket::ignite()
}
/// Alias to [`Rocket::custom()`]. Creates a new instance of `Rocket` with a
/// custom configuration.
pub fn custom(config: config::Config) -> Rocket {
Rocket::custom(config)
}
// TODO.async: More thoughtful plan for async tests
/// WARNING: This is unstable! Do not use this method outside of Rocket!
#[doc(hidden)]
pub fn async_test(fut: impl std::future::Future<Output = ()> + Send + 'static) {
tokio::runtime::Runtime::new().expect("create tokio runtime").block_on(fut)
}