mirror of
https://github.com/actix/actix-extras.git
synced 2025-06-26 02:19:22 +02:00
rename Application
This commit is contained in:
@ -13,6 +13,9 @@ use pipeline::{Pipeline, PipelineHandler};
|
||||
use middleware::Middleware;
|
||||
use server::{HttpHandler, IntoHttpHandler, HttpHandlerTask, ServerSettings};
|
||||
|
||||
#[deprecated(since="0.5.0", note="please use `actix_web::App` instead")]
|
||||
pub type Application<S> = App<S>;
|
||||
|
||||
/// Application
|
||||
pub struct HttpApplication<S=()> {
|
||||
state: Rc<S>,
|
||||
@ -108,17 +111,17 @@ struct ApplicationParts<S> {
|
||||
middlewares: Vec<Box<Middleware<S>>>,
|
||||
}
|
||||
|
||||
/// Structure that follows the builder pattern for building `Application` structs.
|
||||
pub struct Application<S=()> {
|
||||
/// Structure that follows the builder pattern for building application instances.
|
||||
pub struct App<S=()> {
|
||||
parts: Option<ApplicationParts<S>>,
|
||||
}
|
||||
|
||||
impl Application<()> {
|
||||
impl App<()> {
|
||||
|
||||
/// Create application with empty state. Application can
|
||||
/// be configured with builder-like pattern.
|
||||
pub fn new() -> Application<()> {
|
||||
Application {
|
||||
pub fn new() -> App<()> {
|
||||
App {
|
||||
parts: Some(ApplicationParts {
|
||||
state: (),
|
||||
prefix: "/".to_owned(),
|
||||
@ -134,21 +137,21 @@ impl Application<()> {
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Application<()> {
|
||||
impl Default for App<()> {
|
||||
fn default() -> Self {
|
||||
Application::new()
|
||||
App::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl<S> Application<S> where S: 'static {
|
||||
impl<S> App<S> where S: 'static {
|
||||
|
||||
/// Create application with specific state. Application can be
|
||||
/// configured with builder-like pattern.
|
||||
///
|
||||
/// State is shared with all resources within same application and could be
|
||||
/// accessed with `HttpRequest::state()` method.
|
||||
pub fn with_state(state: S) -> Application<S> {
|
||||
Application {
|
||||
pub fn with_state(state: S) -> App<S> {
|
||||
App {
|
||||
parts: Some(ApplicationParts {
|
||||
state,
|
||||
prefix: "/".to_owned(),
|
||||
@ -178,10 +181,10 @@ impl<S> Application<S> where S: 'static {
|
||||
///
|
||||
/// ```rust
|
||||
/// # extern crate actix_web;
|
||||
/// use actix_web::{http, Application, HttpResponse};
|
||||
/// use actix_web::{http, App, HttpResponse};
|
||||
///
|
||||
/// fn main() {
|
||||
/// let app = Application::new()
|
||||
/// let app = App::new()
|
||||
/// .prefix("/app")
|
||||
/// .resource("/test", |r| {
|
||||
/// r.method(http::Method::GET).f(|_| HttpResponse::Ok());
|
||||
@ -190,7 +193,7 @@ impl<S> Application<S> where S: 'static {
|
||||
/// .finish();
|
||||
/// }
|
||||
/// ```
|
||||
pub fn prefix<P: Into<String>>(mut self, prefix: P) -> Application<S> {
|
||||
pub fn prefix<P: Into<String>>(mut self, prefix: P) -> App<S> {
|
||||
{
|
||||
let parts = self.parts.as_mut().expect("Use after finish");
|
||||
let mut prefix = prefix.into();
|
||||
@ -222,17 +225,17 @@ impl<S> Application<S> where S: 'static {
|
||||
///
|
||||
/// ```rust
|
||||
/// # extern crate actix_web;
|
||||
/// use actix_web::{http, Application, HttpResponse};
|
||||
/// use actix_web::{http, App, HttpResponse};
|
||||
///
|
||||
/// fn main() {
|
||||
/// let app = Application::new()
|
||||
/// let app = App::new()
|
||||
/// .resource("/test", |r| {
|
||||
/// r.method(http::Method::GET).f(|_| HttpResponse::Ok());
|
||||
/// r.method(http::Method::HEAD).f(|_| HttpResponse::MethodNotAllowed());
|
||||
/// });
|
||||
/// }
|
||||
/// ```
|
||||
pub fn resource<F>(mut self, path: &str, f: F) -> Application<S>
|
||||
pub fn resource<F>(mut self, path: &str, f: F) -> App<S>
|
||||
where F: FnOnce(&mut Resource<S>) + 'static
|
||||
{
|
||||
{
|
||||
@ -249,7 +252,7 @@ impl<S> Application<S> where S: 'static {
|
||||
}
|
||||
|
||||
/// Default resource is used if no matched route could be found.
|
||||
pub fn default_resource<F>(mut self, f: F) -> Application<S>
|
||||
pub fn default_resource<F>(mut self, f: F) -> App<S>
|
||||
where F: FnOnce(&mut Resource<S>) + 'static
|
||||
{
|
||||
{
|
||||
@ -260,7 +263,7 @@ impl<S> Application<S> where S: 'static {
|
||||
}
|
||||
|
||||
/// Set default content encoding. `ContentEncoding::Auto` is set by default.
|
||||
pub fn default_encoding<F>(mut self, encoding: ContentEncoding) -> Application<S>
|
||||
pub fn default_encoding<F>(mut self, encoding: ContentEncoding) -> App<S>
|
||||
{
|
||||
{
|
||||
let parts = self.parts.as_mut().expect("Use after finish");
|
||||
@ -277,7 +280,7 @@ impl<S> Application<S> where S: 'static {
|
||||
///
|
||||
/// ```rust
|
||||
/// # extern crate actix_web;
|
||||
/// use actix_web::{Application, HttpRequest, HttpResponse, Result};
|
||||
/// use actix_web::{App, HttpRequest, HttpResponse, Result};
|
||||
///
|
||||
/// fn index(mut req: HttpRequest) -> Result<HttpResponse> {
|
||||
/// let url = req.url_for("youtube", &["oHg5SJYRHA0"])?;
|
||||
@ -286,13 +289,13 @@ impl<S> Application<S> where S: 'static {
|
||||
/// }
|
||||
///
|
||||
/// fn main() {
|
||||
/// let app = Application::new()
|
||||
/// let app = App::new()
|
||||
/// .resource("/index.html", |r| r.f(index))
|
||||
/// .external_resource("youtube", "https://youtube.com/watch/{video_id}")
|
||||
/// .finish();
|
||||
/// }
|
||||
/// ```
|
||||
pub fn external_resource<T, U>(mut self, name: T, url: U) -> Application<S>
|
||||
pub fn external_resource<T, U>(mut self, name: T, url: U) -> App<S>
|
||||
where T: AsRef<str>, U: AsRef<str>
|
||||
{
|
||||
{
|
||||
@ -315,10 +318,10 @@ impl<S> Application<S> where S: 'static {
|
||||
///
|
||||
/// ```rust
|
||||
/// # extern crate actix_web;
|
||||
/// use actix_web::{http, Application, HttpRequest, HttpResponse};
|
||||
/// use actix_web::{http, App, HttpRequest, HttpResponse};
|
||||
///
|
||||
/// fn main() {
|
||||
/// let app = Application::new()
|
||||
/// let app = App::new()
|
||||
/// .handler("/app", |req: HttpRequest| {
|
||||
/// match *req.method() {
|
||||
/// http::Method::GET => HttpResponse::Ok(),
|
||||
@ -327,7 +330,7 @@ impl<S> Application<S> where S: 'static {
|
||||
/// }});
|
||||
/// }
|
||||
/// ```
|
||||
pub fn handler<H: Handler<S>>(mut self, path: &str, handler: H) -> Application<S>
|
||||
pub fn handler<H: Handler<S>>(mut self, path: &str, handler: H) -> App<S>
|
||||
{
|
||||
{
|
||||
let path = path.trim().trim_right_matches('/').to_owned();
|
||||
@ -338,7 +341,7 @@ impl<S> Application<S> where S: 'static {
|
||||
}
|
||||
|
||||
/// Register a middleware
|
||||
pub fn middleware<M: Middleware<S>>(mut self, mw: M) -> Application<S> {
|
||||
pub fn middleware<M: Middleware<S>>(mut self, mw: M) -> App<S> {
|
||||
self.parts.as_mut().expect("Use after finish")
|
||||
.middlewares.push(Box::new(mw));
|
||||
self
|
||||
@ -352,10 +355,10 @@ impl<S> Application<S> where S: 'static {
|
||||
///
|
||||
/// ```rust
|
||||
/// # extern crate actix_web;
|
||||
/// use actix_web::{Application, HttpResponse, http, fs, middleware};
|
||||
/// use actix_web::{App, HttpResponse, http, fs, middleware};
|
||||
///
|
||||
/// // this function could be located in different module
|
||||
/// fn config(app: Application) -> Application {
|
||||
/// fn config(app: App) -> App {
|
||||
/// app
|
||||
/// .resource("/test", |r| {
|
||||
/// r.method(http::Method::GET).f(|_| HttpResponse::Ok());
|
||||
@ -364,14 +367,14 @@ impl<S> Application<S> where S: 'static {
|
||||
/// }
|
||||
///
|
||||
/// fn main() {
|
||||
/// let app = Application::new()
|
||||
/// let app = App::new()
|
||||
/// .middleware(middleware::Logger::default())
|
||||
/// .configure(config) // <- register resources
|
||||
/// .handler("/static", fs::StaticFiles::new(".", true));
|
||||
/// }
|
||||
/// ```
|
||||
pub fn configure<F>(self, cfg: F) -> Application<S>
|
||||
where F: Fn(Application<S>) -> Application<S>
|
||||
pub fn configure<F>(self, cfg: F) -> App<S>
|
||||
where F: Fn(App<S>) -> App<S>
|
||||
{
|
||||
cfg(self)
|
||||
}
|
||||
@ -410,7 +413,7 @@ impl<S> Application<S> where S: 'static {
|
||||
|
||||
/// Convenience method for creating `Box<HttpHandler>` instance.
|
||||
///
|
||||
/// This method is useful if you need to register several application instances
|
||||
/// This method is useful if you need to register multiple application instances
|
||||
/// with different state.
|
||||
///
|
||||
/// ```rust
|
||||
@ -425,11 +428,11 @@ impl<S> Application<S> where S: 'static {
|
||||
/// fn main() {
|
||||
/// # thread::spawn(|| {
|
||||
/// HttpServer::new(|| { vec![
|
||||
/// Application::with_state(State1)
|
||||
/// App::with_state(State1)
|
||||
/// .prefix("/app1")
|
||||
/// .resource("/", |r| r.f(|r| HttpResponse::Ok()))
|
||||
/// .boxed(),
|
||||
/// Application::with_state(State2)
|
||||
/// App::with_state(State2)
|
||||
/// .prefix("/app2")
|
||||
/// .resource("/", |r| r.f(|r| HttpResponse::Ok()))
|
||||
/// .boxed() ]})
|
||||
@ -443,7 +446,7 @@ impl<S> Application<S> where S: 'static {
|
||||
}
|
||||
}
|
||||
|
||||
impl<S: 'static> IntoHttpHandler for Application<S> {
|
||||
impl<S: 'static> IntoHttpHandler for App<S> {
|
||||
type Handler = HttpApplication<S>;
|
||||
|
||||
fn into_handler(mut self, settings: ServerSettings) -> HttpApplication<S> {
|
||||
@ -455,7 +458,7 @@ impl<S: 'static> IntoHttpHandler for Application<S> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, S: 'static> IntoHttpHandler for &'a mut Application<S> {
|
||||
impl<'a, S: 'static> IntoHttpHandler for &'a mut App<S> {
|
||||
type Handler = HttpApplication<S>;
|
||||
|
||||
fn into_handler(self, settings: ServerSettings) -> HttpApplication<S> {
|
||||
@ -468,7 +471,7 @@ impl<'a, S: 'static> IntoHttpHandler for &'a mut Application<S> {
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
impl<S: 'static> Iterator for Application<S> {
|
||||
impl<S: 'static> Iterator for App<S> {
|
||||
type Item = HttpApplication<S>;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
@ -491,7 +494,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_default_resource() {
|
||||
let mut app = Application::new()
|
||||
let mut app = App::new()
|
||||
.resource("/test", |r| r.f(|_| HttpResponse::Ok()))
|
||||
.finish();
|
||||
|
||||
@ -503,7 +506,7 @@ mod tests {
|
||||
let resp = app.run(req);
|
||||
assert_eq!(resp.as_response().unwrap().status(), StatusCode::NOT_FOUND);
|
||||
|
||||
let mut app = Application::new()
|
||||
let mut app = App::new()
|
||||
.default_resource(|r| r.f(|_| HttpResponse::MethodNotAllowed()))
|
||||
.finish();
|
||||
let req = TestRequest::with_uri("/blah").finish();
|
||||
@ -513,7 +516,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_unhandled_prefix() {
|
||||
let mut app = Application::new()
|
||||
let mut app = App::new()
|
||||
.prefix("/test")
|
||||
.resource("/test", |r| r.f(|_| HttpResponse::Ok()))
|
||||
.finish();
|
||||
@ -522,7 +525,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_state() {
|
||||
let mut app = Application::with_state(10)
|
||||
let mut app = App::with_state(10)
|
||||
.resource("/", |r| r.f(|_| HttpResponse::Ok()))
|
||||
.finish();
|
||||
let req = HttpRequest::default().with_state(Rc::clone(&app.state), app.router.clone());
|
||||
@ -532,7 +535,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_prefix() {
|
||||
let mut app = Application::new()
|
||||
let mut app = App::new()
|
||||
.prefix("/test")
|
||||
.resource("/blah", |r| r.f(|_| HttpResponse::Ok()))
|
||||
.finish();
|
||||
@ -555,7 +558,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_handler() {
|
||||
let mut app = Application::new()
|
||||
let mut app = App::new()
|
||||
.handler("/test", |_| HttpResponse::Ok())
|
||||
.finish();
|
||||
|
||||
@ -582,7 +585,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_handler_prefix() {
|
||||
let mut app = Application::new()
|
||||
let mut app = App::new()
|
||||
.prefix("/app")
|
||||
.handler("/test", |_| HttpResponse::Ok())
|
||||
.finish();
|
||||
|
12
src/de.rs
12
src/de.rs
@ -19,7 +19,7 @@ use httprequest::HttpRequest;
|
||||
/// # extern crate actix_web;
|
||||
/// # extern crate futures;
|
||||
/// #[macro_use] extern crate serde_derive;
|
||||
/// use actix_web::{Application, Path, Result, http};
|
||||
/// use actix_web::{App, Path, Result, http};
|
||||
///
|
||||
/// /// extract path info from "/{username}/{count}/?index.html" url
|
||||
/// /// {username} - deserializes to a String
|
||||
@ -29,7 +29,7 @@ use httprequest::HttpRequest;
|
||||
/// }
|
||||
///
|
||||
/// fn main() {
|
||||
/// let app = Application::new().resource(
|
||||
/// let app = App::new().resource(
|
||||
/// "/{username}/{count}/?index.html", // <- define path parameters
|
||||
/// |r| r.method(http::Method::GET).with(index)); // <- use `with` extractor
|
||||
/// }
|
||||
@ -43,7 +43,7 @@ use httprequest::HttpRequest;
|
||||
/// # extern crate actix_web;
|
||||
/// # extern crate futures;
|
||||
/// #[macro_use] extern crate serde_derive;
|
||||
/// use actix_web::{Application, Path, Result, http};
|
||||
/// use actix_web::{App, Path, Result, http};
|
||||
///
|
||||
/// #[derive(Deserialize)]
|
||||
/// struct Info {
|
||||
@ -56,7 +56,7 @@ use httprequest::HttpRequest;
|
||||
/// }
|
||||
///
|
||||
/// fn main() {
|
||||
/// let app = Application::new().resource(
|
||||
/// let app = App::new().resource(
|
||||
/// "/{username}/index.html", // <- define path parameters
|
||||
/// |r| r.method(http::Method::GET).with(index)); // <- use `with` extractor
|
||||
/// }
|
||||
@ -109,7 +109,7 @@ impl<T, S> FromRequest<S> for Path<T>
|
||||
/// # extern crate actix_web;
|
||||
/// # extern crate futures;
|
||||
/// #[macro_use] extern crate serde_derive;
|
||||
/// use actix_web::{Application, Query, http};
|
||||
/// use actix_web::{App, Query, http};
|
||||
///
|
||||
/// #[derive(Deserialize)]
|
||||
/// struct Info {
|
||||
@ -123,7 +123,7 @@ impl<T, S> FromRequest<S> for Path<T>
|
||||
/// }
|
||||
///
|
||||
/// fn main() {
|
||||
/// let app = Application::new().resource(
|
||||
/// let app = App::new().resource(
|
||||
/// "/index.html",
|
||||
/// |r| r.method(http::Method::GET).with(index)); // <- use `with` extractor
|
||||
/// }
|
||||
|
@ -363,15 +363,15 @@ impl Responder for Directory {
|
||||
|
||||
/// Static files handling
|
||||
///
|
||||
/// `StaticFile` handler must be registered with `Application::handler()` method,
|
||||
/// `StaticFile` handler must be registered with `App::handler()` method,
|
||||
/// because `StaticFile` handler requires access sub-path information.
|
||||
///
|
||||
/// ```rust
|
||||
/// # extern crate actix_web;
|
||||
/// use actix_web::{fs, Application};
|
||||
/// use actix_web::{fs, App};
|
||||
///
|
||||
/// fn main() {
|
||||
/// let app = Application::new()
|
||||
/// let app = App::new()
|
||||
/// .handler("/static", fs::StaticFiles::new(".", true))
|
||||
/// .finish();
|
||||
/// }
|
||||
|
@ -96,7 +96,7 @@ impl<A, B> Responder for Either<A, B>
|
||||
}
|
||||
}
|
||||
|
||||
/// Convenience trait that convert `Future` object into `Boxed` future
|
||||
/// Convenience trait that converts `Future` object to a `Boxed` future
|
||||
///
|
||||
/// For example loading json from request's body is async operation.
|
||||
///
|
||||
@ -106,7 +106,7 @@ impl<A, B> Responder for Either<A, B>
|
||||
/// # #[macro_use] extern crate serde_derive;
|
||||
/// use futures::future::Future;
|
||||
/// use actix_web::{
|
||||
/// Application, HttpRequest, HttpResponse, HttpMessage, Error, AsyncResponder};
|
||||
/// App, HttpRequest, HttpResponse, HttpMessage, Error, AsyncResponder};
|
||||
///
|
||||
/// #[derive(Deserialize, Debug)]
|
||||
/// struct MyObj {
|
||||
@ -384,10 +384,10 @@ impl<S, H, F, R, E> RouteHandler<S> for AsyncHandler<S, H, F, R, E>
|
||||
/// # extern crate actix_web;
|
||||
/// # extern crate futures;
|
||||
/// #[macro_use] extern crate serde_derive;
|
||||
/// use actix_web::{Application, Path, State, http};
|
||||
/// use actix_web::{App, Path, State, http};
|
||||
///
|
||||
/// /// Application state
|
||||
/// struct App {msg: &'static str}
|
||||
/// struct MyApp {msg: &'static str}
|
||||
///
|
||||
/// #[derive(Deserialize)]
|
||||
/// struct Info {
|
||||
@ -395,12 +395,12 @@ impl<S, H, F, R, E> RouteHandler<S> for AsyncHandler<S, H, F, R, E>
|
||||
/// }
|
||||
///
|
||||
/// /// extract path info using serde
|
||||
/// fn index(state: State<App>, info: Path<Info>) -> String {
|
||||
/// fn index(state: State<MyApp>, info: Path<Info>) -> String {
|
||||
/// format!("{} {}!", state.msg, info.username)
|
||||
/// }
|
||||
///
|
||||
/// fn main() {
|
||||
/// let app = Application::with_state(App{msg: "Welcome"}).resource(
|
||||
/// let app = App::with_state(MyApp{msg: "Welcome"}).resource(
|
||||
/// "/{username}/index.html", // <- define path parameters
|
||||
/// |r| r.method(http::Method::GET).with2(index)); // <- use `with` extractor
|
||||
/// }
|
||||
|
@ -38,7 +38,7 @@ use httpresponse::HttpResponse;
|
||||
/// # HttpResponse::Ok().into()
|
||||
/// # }
|
||||
/// fn main() {
|
||||
/// let app = Application::new()
|
||||
/// let app = App::new()
|
||||
/// .resource("/test/", |r| r.f(index))
|
||||
/// .default_resource(|r| r.h(NormalizePath::default()))
|
||||
/// .finish();
|
||||
@ -155,7 +155,7 @@ mod tests {
|
||||
use super::*;
|
||||
use http::{header, Method};
|
||||
use test::TestRequest;
|
||||
use application::Application;
|
||||
use application::App;
|
||||
|
||||
fn index(_req: HttpRequest) -> HttpResponse {
|
||||
HttpResponse::new(StatusCode::OK)
|
||||
@ -163,7 +163,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_normalize_path_trailing_slashes() {
|
||||
let mut app = Application::new()
|
||||
let mut app = App::new()
|
||||
.resource("/resource1", |r| r.method(Method::GET).f(index))
|
||||
.resource("/resource2/", |r| r.method(Method::GET).f(index))
|
||||
.default_resource(|r| r.h(NormalizePath::default()))
|
||||
@ -196,7 +196,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_normalize_path_trailing_slashes_disabled() {
|
||||
let mut app = Application::new()
|
||||
let mut app = App::new()
|
||||
.resource("/resource1", |r| r.method(Method::GET).f(index))
|
||||
.resource("/resource2/", |r| r.method(Method::GET).f(index))
|
||||
.default_resource(|r| r.h(
|
||||
@ -223,7 +223,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_normalize_path_merge_slashes() {
|
||||
let mut app = Application::new()
|
||||
let mut app = App::new()
|
||||
.resource("/resource1", |r| r.method(Method::GET).f(index))
|
||||
.resource("/resource1/a/b", |r| r.method(Method::GET).f(index))
|
||||
.default_resource(|r| r.h(NormalizePath::default()))
|
||||
@ -263,7 +263,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_normalize_path_merge_and_append_slashes() {
|
||||
let mut app = Application::new()
|
||||
let mut app = App::new()
|
||||
.resource("/resource1", |r| r.method(Method::GET).f(index))
|
||||
.resource("/resource2/", |r| r.method(Method::GET).f(index))
|
||||
.resource("/resource1/a/b", |r| r.method(Method::GET).f(index))
|
||||
|
@ -279,7 +279,7 @@ impl<S> HttpRequest<S> {
|
||||
///
|
||||
/// ```rust
|
||||
/// # extern crate actix_web;
|
||||
/// # use actix_web::{Application, HttpRequest, HttpResponse, http};
|
||||
/// # use actix_web::{App, HttpRequest, HttpResponse, http};
|
||||
/// #
|
||||
/// fn index(req: HttpRequest) -> HttpResponse {
|
||||
/// let url = req.url_for("foo", &["1", "2", "3"]); // <- generate url for "foo" resource
|
||||
@ -287,7 +287,7 @@ impl<S> HttpRequest<S> {
|
||||
/// }
|
||||
///
|
||||
/// fn main() {
|
||||
/// let app = Application::new()
|
||||
/// let app = App::new()
|
||||
/// .resource("/test/{one}/{two}/{three}", |r| {
|
||||
/// r.name("foo"); // <- set resource name, then it could be used in `url_for`
|
||||
/// r.method(http::Method::GET).f(|_| HttpResponse::Ok());
|
||||
|
@ -314,7 +314,7 @@ impl HttpResponseBuilder {
|
||||
///
|
||||
/// ```rust
|
||||
/// # extern crate actix_web;
|
||||
/// use actix_web::{http, Application, HttpRequest, HttpResponse};
|
||||
/// use actix_web::{http, HttpRequest, HttpResponse};
|
||||
///
|
||||
/// fn index(req: HttpRequest) -> HttpResponse {
|
||||
/// HttpResponse::Ok()
|
||||
|
@ -51,7 +51,7 @@ use httpresponse::HttpResponse;
|
||||
/// # extern crate actix_web;
|
||||
/// # extern crate futures;
|
||||
/// #[macro_use] extern crate serde_derive;
|
||||
/// use actix_web::{Application, Json, Result, http};
|
||||
/// use actix_web::{App, Json, Result, http};
|
||||
///
|
||||
/// #[derive(Deserialize)]
|
||||
/// struct Info {
|
||||
@ -64,7 +64,7 @@ use httpresponse::HttpResponse;
|
||||
/// }
|
||||
///
|
||||
/// fn main() {
|
||||
/// let app = Application::new().resource(
|
||||
/// let app = App::new().resource(
|
||||
/// "/index.html",
|
||||
/// |r| r.method(http::Method::POST).with(index)); // <- use `with` extractor
|
||||
/// }
|
||||
@ -139,8 +139,7 @@ impl<T, S> FromRequest<S> for Json<T>
|
||||
/// # extern crate futures;
|
||||
/// # #[macro_use] extern crate serde_derive;
|
||||
/// use futures::future::Future;
|
||||
/// use actix_web::{Application, AsyncResponder,
|
||||
/// HttpRequest, HttpResponse, HttpMessage, Error};
|
||||
/// use actix_web::{AsyncResponder, HttpRequest, HttpResponse, HttpMessage, Error};
|
||||
///
|
||||
/// #[derive(Deserialize, Debug)]
|
||||
/// struct MyObj {
|
||||
|
10
src/lib.rs
10
src/lib.rs
@ -1,7 +1,7 @@
|
||||
//! Actix web is a small, pragmatic, extremely fast, web framework for Rust.
|
||||
//!
|
||||
//! ```rust
|
||||
//! use actix_web::{Application, HttpServer, Path};
|
||||
//! use actix_web::{App, HttpServer, Path};
|
||||
//! # use std::thread;
|
||||
//!
|
||||
//! fn index(info: Path<(String, u32)>) -> String {
|
||||
@ -11,7 +11,7 @@
|
||||
//! fn main() {
|
||||
//! # thread::spawn(|| {
|
||||
//! HttpServer::new(
|
||||
//! || Application::new()
|
||||
//! || App::new()
|
||||
//! .resource("/{name}/{id}/index.html", |r| r.with(index)))
|
||||
//! .bind("127.0.0.1:8080").unwrap()
|
||||
//! .run();
|
||||
@ -136,7 +136,7 @@ pub use error::{Error, Result, ResponseError};
|
||||
pub use body::{Body, Binary};
|
||||
pub use json::Json;
|
||||
pub use de::{Path, Query};
|
||||
pub use application::Application;
|
||||
pub use application::App;
|
||||
pub use httpmessage::HttpMessage;
|
||||
pub use httprequest::HttpRequest;
|
||||
pub use httpresponse::HttpResponse;
|
||||
@ -147,6 +147,10 @@ pub use server::HttpServer;
|
||||
#[doc(hidden)]
|
||||
pub mod httpcodes;
|
||||
|
||||
#[doc(hidden)]
|
||||
#[allow(deprecated)]
|
||||
pub use application::Application;
|
||||
|
||||
#[cfg(feature="openssl")]
|
||||
pub(crate) const HAS_OPENSSL: bool = true;
|
||||
#[cfg(not(feature="openssl"))]
|
||||
|
@ -9,7 +9,7 @@
|
||||
//! 2. Use any of the builder methods to set fields in the backend.
|
||||
//! 3. Call [finish](struct.Cors.html#method.finish) to retrieve the constructed backend.
|
||||
//!
|
||||
//! Cors middleware could be used as parameter for `Application::middleware()` or
|
||||
//! Cors middleware could be used as parameter for `App::middleware()` or
|
||||
//! `Resource::middleware()` methods. But you have to use `Cors::register()` method to
|
||||
//! support *preflight* OPTIONS request.
|
||||
//!
|
||||
@ -18,7 +18,7 @@
|
||||
//!
|
||||
//! ```rust
|
||||
//! # extern crate actix_web;
|
||||
//! use actix_web::{http, Application, HttpRequest, HttpResponse};
|
||||
//! use actix_web::{http, App, HttpRequest, HttpResponse};
|
||||
//! use actix_web::middleware::cors;
|
||||
//!
|
||||
//! fn index(mut req: HttpRequest) -> &'static str {
|
||||
@ -26,7 +26,7 @@
|
||||
//! }
|
||||
//!
|
||||
//! fn main() {
|
||||
//! let app = Application::new()
|
||||
//! let app = App::new()
|
||||
//! .resource("/index.html", |r| {
|
||||
//! cors::Cors::build() // <- Construct CORS middleware
|
||||
//! .allowed_origin("https://www.rust-lang.org/")
|
||||
|
@ -22,7 +22,7 @@
|
||||
//!
|
||||
//! ```
|
||||
//! # extern crate actix_web;
|
||||
//! use actix_web::{http, Application, HttpRequest, HttpResponse};
|
||||
//! use actix_web::{http, App, HttpRequest, HttpResponse};
|
||||
//! use actix_web::middleware::csrf;
|
||||
//!
|
||||
//! fn handle_post(_: HttpRequest) -> &'static str {
|
||||
@ -30,7 +30,7 @@
|
||||
//! }
|
||||
//!
|
||||
//! fn main() {
|
||||
//! let app = Application::new()
|
||||
//! let app = App::new()
|
||||
//! .middleware(
|
||||
//! csrf::CsrfFilter::build()
|
||||
//! .allowed_origin("https://www.example.com")
|
||||
|
@ -13,10 +13,10 @@ use middleware::{Response, Middleware};
|
||||
///
|
||||
/// ```rust
|
||||
/// # extern crate actix_web;
|
||||
/// use actix_web::{http, middleware, Application, HttpResponse};
|
||||
/// use actix_web::{http, middleware, App, HttpResponse};
|
||||
///
|
||||
/// fn main() {
|
||||
/// let app = Application::new()
|
||||
/// let app = App::new()
|
||||
/// .middleware(
|
||||
/// middleware::DefaultHeaders::build()
|
||||
/// .header("X-Version", "0.2")
|
||||
|
@ -29,14 +29,14 @@ use middleware::{Middleware, Started, Finished};
|
||||
/// ```rust
|
||||
/// # extern crate actix_web;
|
||||
/// extern crate env_logger;
|
||||
/// use actix_web::Application;
|
||||
/// use actix_web::App;
|
||||
/// use actix_web::middleware::Logger;
|
||||
///
|
||||
/// fn main() {
|
||||
/// std::env::set_var("RUST_LOG", "actix_web=info");
|
||||
/// env_logger::init();
|
||||
///
|
||||
/// let app = Application::new()
|
||||
/// let app = App::new()
|
||||
/// .middleware(Logger::default())
|
||||
/// .middleware(Logger::new("%a %{User-Agent}i"))
|
||||
/// .finish();
|
||||
|
@ -114,11 +114,11 @@ unsafe impl Sync for SessionImplBox {}
|
||||
/// ```rust
|
||||
/// # extern crate actix;
|
||||
/// # extern crate actix_web;
|
||||
/// # use actix_web::middleware::{SessionStorage, CookieSessionBackend};
|
||||
/// use actix_web::*;
|
||||
/// use actix_web::App;
|
||||
/// use actix_web::middleware::{SessionStorage, CookieSessionBackend};
|
||||
///
|
||||
/// fn main() {
|
||||
/// let app = Application::new().middleware(
|
||||
/// let app = App::new().middleware(
|
||||
/// SessionStorage::new( // <- create session middleware
|
||||
/// CookieSessionBackend::build(&[0; 32]) // <- create cookie session backend
|
||||
/// .secure(false)
|
||||
|
@ -20,10 +20,10 @@ pub trait Predicate<S> {
|
||||
///
|
||||
/// ```rust
|
||||
/// # extern crate actix_web;
|
||||
/// use actix_web::{pred, Application, HttpResponse};
|
||||
/// use actix_web::{pred, App, HttpResponse};
|
||||
///
|
||||
/// fn main() {
|
||||
/// Application::new()
|
||||
/// App::new()
|
||||
/// .resource("/index.html", |r| r.route()
|
||||
/// .filter(pred::Any(pred::Get()).or(pred::Post()))
|
||||
/// .f(|r| HttpResponse::MethodNotAllowed()));
|
||||
|
@ -24,10 +24,10 @@ use with::WithHandler;
|
||||
///
|
||||
/// ```rust
|
||||
/// # extern crate actix_web;
|
||||
/// use actix_web::{Application, HttpResponse, http};
|
||||
/// use actix_web::{App, HttpResponse, http};
|
||||
///
|
||||
/// fn main() {
|
||||
/// let app = Application::new()
|
||||
/// let app = App::new()
|
||||
/// .resource(
|
||||
/// "/", |r| r.method(http::Method::GET).f(|r| HttpResponse::Ok()))
|
||||
/// .finish();
|
||||
@ -79,7 +79,7 @@ impl<S: 'static> Resource<S> {
|
||||
/// use actix_web::*;
|
||||
///
|
||||
/// fn main() {
|
||||
/// let app = Application::new()
|
||||
/// let app = App::new()
|
||||
/// .resource(
|
||||
/// "/", |r| r.route()
|
||||
/// .filter(pred::Any(pred::Get()).or(pred::Put()))
|
||||
@ -149,7 +149,7 @@ impl<S: 'static> Resource<S> {
|
||||
|
||||
/// Register a middleware
|
||||
///
|
||||
/// This is similar to `Application's` middlewares, but
|
||||
/// This is similar to `App's` middlewares, but
|
||||
/// middlewares get invoked on resource level.
|
||||
pub fn middleware<M: Middleware<S>>(&mut self, mw: M) {
|
||||
Rc::get_mut(&mut self.middlewares).unwrap().push(Box::new(mw));
|
||||
|
10
src/route.rs
10
src/route.rs
@ -62,7 +62,7 @@ impl<S: 'static> Route<S> {
|
||||
/// # extern crate actix_web;
|
||||
/// # use actix_web::*;
|
||||
/// # fn main() {
|
||||
/// Application::new()
|
||||
/// App::new()
|
||||
/// .resource("/path", |r|
|
||||
/// r.route()
|
||||
/// .filter(pred::Get())
|
||||
@ -109,7 +109,7 @@ impl<S: 'static> Route<S> {
|
||||
/// # extern crate actix_web;
|
||||
/// # extern crate futures;
|
||||
/// #[macro_use] extern crate serde_derive;
|
||||
/// use actix_web::{Application, Path, Result, http};
|
||||
/// use actix_web::{App, Path, Result, http};
|
||||
///
|
||||
/// #[derive(Deserialize)]
|
||||
/// struct Info {
|
||||
@ -122,7 +122,7 @@ impl<S: 'static> Route<S> {
|
||||
/// }
|
||||
///
|
||||
/// fn main() {
|
||||
/// let app = Application::new().resource(
|
||||
/// let app = App::new().resource(
|
||||
/// "/{username}/index.html", // <- define path parameters
|
||||
/// |r| r.method(http::Method::GET).with(index)); // <- use `with` extractor
|
||||
/// }
|
||||
@ -141,7 +141,7 @@ impl<S: 'static> Route<S> {
|
||||
/// # extern crate actix_web;
|
||||
/// # extern crate futures;
|
||||
/// #[macro_use] extern crate serde_derive;
|
||||
/// use actix_web::{Application, Query, Path, Result, http};
|
||||
/// use actix_web::{App, Query, Path, Result, http};
|
||||
///
|
||||
/// #[derive(Deserialize)]
|
||||
/// struct PParam {
|
||||
@ -159,7 +159,7 @@ impl<S: 'static> Route<S> {
|
||||
/// }
|
||||
///
|
||||
/// fn main() {
|
||||
/// let app = Application::new().resource(
|
||||
/// let app = App::new().resource(
|
||||
/// "/{username}/index.html", // <- define path parameters
|
||||
/// |r| r.method(http::Method::GET).with2(index)); // <- use `with` extractor
|
||||
/// }
|
||||
|
@ -38,13 +38,13 @@ pub(crate) const MAX_WRITE_BUFFER_SIZE: usize = 65_536;
|
||||
/// # extern crate actix;
|
||||
/// # extern crate actix_web;
|
||||
/// use actix::*;
|
||||
/// use actix_web::{server, Application, HttpResponse};
|
||||
/// use actix_web::{server, App, HttpResponse};
|
||||
///
|
||||
/// fn main() {
|
||||
/// let sys = actix::System::new("guide");
|
||||
///
|
||||
/// server::new(
|
||||
/// || Application::new()
|
||||
/// || App::new()
|
||||
/// .resource("/", |r| r.f(|_| HttpResponse::Ok())))
|
||||
/// .bind("127.0.0.1:59080").unwrap()
|
||||
/// .start();
|
||||
|
@ -268,7 +268,7 @@ impl<H: IntoHttpHandler> HttpServer<H>
|
||||
/// let sys = actix::System::new("example"); // <- create Actix system
|
||||
///
|
||||
/// HttpServer::new(
|
||||
/// || Application::new()
|
||||
/// || App::new()
|
||||
/// .resource("/", |r| r.h(|_| HttpResponse::Ok())))
|
||||
/// .bind("127.0.0.1:0").expect("Can not bind to 127.0.0.1:0")
|
||||
/// .start();
|
||||
@ -326,7 +326,7 @@ impl<H: IntoHttpHandler> HttpServer<H>
|
||||
///
|
||||
/// fn main() {
|
||||
/// HttpServer::new(
|
||||
/// || Application::new()
|
||||
/// || App::new()
|
||||
/// .resource("/", |r| r.h(|_| HttpResponse::Ok())))
|
||||
/// .bind("127.0.0.1:0").expect("Can not bind to 127.0.0.1:0")
|
||||
/// .run();
|
||||
|
@ -23,7 +23,7 @@ use error::Error;
|
||||
use header::{Header, IntoHeaderValue};
|
||||
use handler::{Handler, Responder, ReplyItem};
|
||||
use middleware::Middleware;
|
||||
use application::{Application, HttpApplication};
|
||||
use application::{App, HttpApplication};
|
||||
use param::Params;
|
||||
use router::Router;
|
||||
use payload::Payload;
|
||||
@ -327,12 +327,12 @@ impl<S: 'static> TestServerBuilder<S> {
|
||||
|
||||
/// Test application helper for testing request handlers.
|
||||
pub struct TestApp<S=()> {
|
||||
app: Option<Application<S>>,
|
||||
app: Option<App<S>>,
|
||||
}
|
||||
|
||||
impl<S: 'static> TestApp<S> {
|
||||
fn new(state: S) -> TestApp<S> {
|
||||
let app = Application::with_state(state);
|
||||
let app = App::with_state(state);
|
||||
TestApp{app: Some(app)}
|
||||
}
|
||||
|
||||
@ -350,7 +350,7 @@ impl<S: 'static> TestApp<S> {
|
||||
}
|
||||
|
||||
/// Register resource. This method is similar
|
||||
/// to `Application::resource()` method.
|
||||
/// to `App::resource()` method.
|
||||
pub fn resource<F>(&mut self, path: &str, f: F) -> &mut TestApp<S>
|
||||
where F: FnOnce(&mut Resource<S>) + 'static
|
||||
{
|
||||
|
@ -37,7 +37,7 @@
|
||||
//! }
|
||||
//! #
|
||||
//! # fn main() {
|
||||
//! # Application::new()
|
||||
//! # App::new()
|
||||
//! # .resource("/ws/", |r| r.f(ws_index)) // <- register websocket route
|
||||
//! # .finish();
|
||||
//! # }
|
||||
|
Reference in New Issue
Block a user