mirror of
https://github.com/actix/actix-extras.git
synced 2024-11-24 07:53:00 +01:00
cargo fmt
This commit is contained in:
parent
154cd3c5de
commit
3f5a39a5b7
@ -300,10 +300,12 @@ where
|
||||
///
|
||||
/// fn main() {
|
||||
/// let app = App::new()
|
||||
/// .route("/test", http::Method::GET,
|
||||
/// |_: HttpRequest| HttpResponse::Ok())
|
||||
/// .route("/test", http::Method::POST,
|
||||
/// |_: HttpRequest| HttpResponse::MethodNotAllowed());
|
||||
/// .route("/test", http::Method::GET, |_: HttpRequest| {
|
||||
/// HttpResponse::Ok()
|
||||
/// })
|
||||
/// .route("/test", http::Method::POST, |_: HttpRequest| {
|
||||
/// HttpResponse::MethodNotAllowed()
|
||||
/// });
|
||||
/// }
|
||||
/// ```
|
||||
pub fn route<T, F, R>(mut self, path: &str, method: Method, f: F) -> App<S>
|
||||
@ -345,9 +347,9 @@ where
|
||||
/// use actix_web::{http, App, HttpRequest, HttpResponse};
|
||||
///
|
||||
/// fn main() {
|
||||
/// let app = App::new()
|
||||
/// .scope("/{project_id}", |scope| {
|
||||
/// scope.resource("/path1", |r| r.f(|_| HttpResponse::Ok()))
|
||||
/// let app = App::new().scope("/{project_id}", |scope| {
|
||||
/// scope
|
||||
/// .resource("/path1", |r| r.f(|_| HttpResponse::Ok()))
|
||||
/// .resource("/path2", |r| r.f(|_| HttpResponse::Ok()))
|
||||
/// .resource("/path3", |r| r.f(|_| HttpResponse::MethodNotAllowed()))
|
||||
/// });
|
||||
@ -402,8 +404,7 @@ where
|
||||
/// use actix_web::{http, App, HttpResponse};
|
||||
///
|
||||
/// fn main() {
|
||||
/// let app = App::new()
|
||||
/// .resource("/users/{userid}/{friend}", |r| {
|
||||
/// let app = App::new().resource("/users/{userid}/{friend}", |r| {
|
||||
/// r.get().f(|_| HttpResponse::Ok());
|
||||
/// r.head().f(|_| HttpResponse::MethodNotAllowed());
|
||||
/// });
|
||||
@ -514,13 +515,11 @@ where
|
||||
/// use actix_web::{http, App, HttpRequest, HttpResponse};
|
||||
///
|
||||
/// fn main() {
|
||||
/// let app = App::new()
|
||||
/// .handler("/app", |req: HttpRequest| {
|
||||
/// match *req.method() {
|
||||
/// let app = App::new().handler("/app", |req: HttpRequest| match *req.method() {
|
||||
/// http::Method::GET => HttpResponse::Ok(),
|
||||
/// http::Method::POST => HttpResponse::MethodNotAllowed(),
|
||||
/// _ => HttpResponse::NotFound(),
|
||||
/// }});
|
||||
/// });
|
||||
/// }
|
||||
/// ```
|
||||
pub fn handler<H: Handler<S>>(mut self, path: &str, handler: H) -> App<S> {
|
||||
@ -561,12 +560,11 @@ where
|
||||
///
|
||||
/// ```rust
|
||||
/// # extern crate actix_web;
|
||||
/// use actix_web::{App, HttpResponse, fs, middleware};
|
||||
/// use actix_web::{fs, middleware, App, HttpResponse};
|
||||
///
|
||||
/// // this function could be located in different module
|
||||
/// fn config(app: App) -> App {
|
||||
/// app
|
||||
/// .resource("/test", |r| {
|
||||
/// app.resource("/test", |r| {
|
||||
/// r.get().f(|_| HttpResponse::Ok());
|
||||
/// r.head().f(|_| HttpResponse::MethodNotAllowed());
|
||||
/// })
|
||||
@ -636,8 +634,9 @@ where
|
||||
/// struct State2;
|
||||
///
|
||||
/// fn main() {
|
||||
/// # thread::spawn(|| {
|
||||
/// server::new(|| { vec![
|
||||
/// //#### # thread::spawn(|| {
|
||||
/// server::new(|| {
|
||||
/// vec![
|
||||
/// App::with_state(State1)
|
||||
/// .prefix("/app1")
|
||||
/// .resource("/", |r| r.f(|r| HttpResponse::Ok()))
|
||||
@ -645,10 +644,12 @@ where
|
||||
/// App::with_state(State2)
|
||||
/// .prefix("/app2")
|
||||
/// .resource("/", |r| r.f(|r| HttpResponse::Ok()))
|
||||
/// .boxed() ]})
|
||||
/// .bind("127.0.0.1:8080").unwrap()
|
||||
/// .boxed(),
|
||||
/// ]
|
||||
/// }).bind("127.0.0.1:8080")
|
||||
/// .unwrap()
|
||||
/// .run()
|
||||
/// # });
|
||||
/// //#### # });
|
||||
/// }
|
||||
/// ```
|
||||
pub fn boxed(mut self) -> Box<HttpHandler> {
|
||||
|
@ -81,7 +81,8 @@ impl ResponseError for SendRequestError {
|
||||
/// println!("Response: {:?}", response);
|
||||
/// # process::exit(0);
|
||||
/// Ok(())
|
||||
/// }));
|
||||
/// }),
|
||||
/// );
|
||||
/// }
|
||||
/// ```
|
||||
pub fn get<U: AsRef<str>>(uri: U) -> ClientRequestBuilder {
|
||||
|
@ -43,7 +43,7 @@ use httprequest::HttpRequest;
|
||||
/// println!("Response: {:?}", response);
|
||||
/// # process::exit(0);
|
||||
/// Ok(())
|
||||
/// })
|
||||
/// }),
|
||||
/// );
|
||||
/// }
|
||||
/// ```
|
||||
@ -344,7 +344,8 @@ impl ClientRequestBuilder {
|
||||
/// let req = client::ClientRequest::build()
|
||||
/// .set(http::header::Date::now())
|
||||
/// .set(http::header::ContentType(mime::TEXT_HTML))
|
||||
/// .finish().unwrap();
|
||||
/// .finish()
|
||||
/// .unwrap();
|
||||
/// }
|
||||
/// ```
|
||||
#[doc(hidden)]
|
||||
@ -376,7 +377,8 @@ impl ClientRequestBuilder {
|
||||
/// let req = ClientRequest::build()
|
||||
/// .header("X-TEST", "value")
|
||||
/// .header(header::CONTENT_TYPE, "application/json")
|
||||
/// .finish().unwrap();
|
||||
/// .finish()
|
||||
/// .unwrap();
|
||||
/// }
|
||||
/// ```
|
||||
pub fn header<K, V>(&mut self, key: K, value: V) -> &mut Self
|
||||
@ -486,8 +488,10 @@ impl ClientRequestBuilder {
|
||||
/// .path("/")
|
||||
/// .secure(true)
|
||||
/// .http_only(true)
|
||||
/// .finish())
|
||||
/// .finish().unwrap();
|
||||
/// .finish(),
|
||||
/// )
|
||||
/// .finish()
|
||||
/// .unwrap();
|
||||
/// }
|
||||
/// ```
|
||||
pub fn cookie<'c>(&mut self, cookie: Cookie<'c>) -> &mut Self {
|
||||
|
@ -24,7 +24,7 @@ use httprequest::HttpRequest;
|
||||
/// # extern crate bytes;
|
||||
/// # extern crate actix_web;
|
||||
/// # extern crate futures;
|
||||
/// use actix_web::{App, Path, Result, http};
|
||||
/// use actix_web::{http, App, Path, Result};
|
||||
///
|
||||
/// /// extract path info from "/{username}/{count}/index.html" url
|
||||
/// /// {username} - deserializes to a String
|
||||
@ -36,7 +36,8 @@ use httprequest::HttpRequest;
|
||||
/// fn main() {
|
||||
/// let app = App::new().resource(
|
||||
/// "/{username}/{count}/index.html", // <- define path parameters
|
||||
/// |r| r.method(http::Method::GET).with(index)); // <- use `with` extractor
|
||||
/// |r| r.method(http::Method::GET).with(index),
|
||||
/// ); // <- use `with` extractor
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
@ -48,7 +49,7 @@ use httprequest::HttpRequest;
|
||||
/// # extern crate actix_web;
|
||||
/// # extern crate futures;
|
||||
/// #[macro_use] extern crate serde_derive;
|
||||
/// use actix_web::{App, Path, Result, http};
|
||||
/// use actix_web::{http, App, Path, Result};
|
||||
///
|
||||
/// #[derive(Deserialize)]
|
||||
/// struct Info {
|
||||
@ -63,7 +64,8 @@ use httprequest::HttpRequest;
|
||||
/// fn main() {
|
||||
/// let app = App::new().resource(
|
||||
/// "/{username}/index.html", // <- define path parameters
|
||||
/// |r| r.method(http::Method::GET).with(index)); // <- use `with` extractor
|
||||
/// |r| r.method(http::Method::GET).with(index),
|
||||
/// ); // <- use `with` extractor
|
||||
/// }
|
||||
/// ```
|
||||
pub struct Path<T> {
|
||||
@ -118,13 +120,13 @@ where
|
||||
/// ## Example
|
||||
///
|
||||
/// ```rust
|
||||
/// # extern crate bytes;
|
||||
/// # extern crate actix_web;
|
||||
/// # extern crate futures;
|
||||
/// #[macro_use] extern crate serde_derive;
|
||||
/// //#### # extern crate bytes;
|
||||
/// //#### # extern crate actix_web;
|
||||
/// //#### # extern crate futures;
|
||||
/// //#### #[macro_use] extern crate serde_derive;
|
||||
/// use actix_web::{App, Query, http};
|
||||
///
|
||||
/// #[derive(Deserialize)]
|
||||
/// //#### #[derive(Deserialize)]
|
||||
/// struct Info {
|
||||
/// username: String,
|
||||
/// }
|
||||
@ -255,7 +257,7 @@ where
|
||||
/// ```rust
|
||||
/// # extern crate actix_web;
|
||||
/// #[macro_use] extern crate serde_derive;
|
||||
/// use actix_web::{App, Form, Result, http};
|
||||
/// use actix_web::{http, App, Form, Result};
|
||||
///
|
||||
/// #[derive(Deserialize)]
|
||||
/// struct FormData {
|
||||
@ -270,10 +272,10 @@ where
|
||||
///
|
||||
/// fn main() {
|
||||
/// let app = App::new().resource(
|
||||
/// "/index.html", |r| {
|
||||
/// r.method(http::Method::GET)
|
||||
/// .with(index)
|
||||
/// .limit(4096);} // <- change form extractor configuration
|
||||
/// "/index.html",
|
||||
/// |r| {
|
||||
/// r.method(http::Method::GET).with(index).limit(4096);
|
||||
/// }, // <- change form extractor configuration
|
||||
/// );
|
||||
/// }
|
||||
/// ```
|
||||
@ -315,9 +317,8 @@ impl Default for FormConfig {
|
||||
/// }
|
||||
///
|
||||
/// fn main() {
|
||||
/// let app = App::new().resource(
|
||||
/// "/index.html", |r|
|
||||
/// r.method(http::Method::GET).with(index));
|
||||
/// let app = App::new()
|
||||
/// .resource("/index.html", |r| r.method(http::Method::GET).with(index));
|
||||
/// }
|
||||
/// ```
|
||||
impl<S: 'static> FromRequest<S> for Bytes {
|
||||
@ -354,8 +355,7 @@ impl<S: 'static> FromRequest<S> for Bytes {
|
||||
/// }
|
||||
///
|
||||
/// fn main() {
|
||||
/// let app = App::new().resource(
|
||||
/// "/index.html", |r| {
|
||||
/// let app = App::new().resource("/index.html", |r| {
|
||||
/// r.method(http::Method::GET)
|
||||
/// .with(index) // <- register handler with extractor params
|
||||
/// .limit(4096); // <- limit size of the payload
|
||||
|
@ -61,22 +61,24 @@ pub trait FromRequest<S>: Sized {
|
||||
/// # extern crate actix_web;
|
||||
/// # extern crate futures;
|
||||
/// # use futures::future::Future;
|
||||
/// use actix_web::{AsyncResponder, Either, Error, HttpRequest, HttpResponse};
|
||||
/// use futures::future::result;
|
||||
/// use actix_web::{Either, Error, HttpRequest, HttpResponse, AsyncResponder};
|
||||
///
|
||||
/// type RegisterResult = Either<HttpResponse, Box<Future<Item=HttpResponse, Error=Error>>>;
|
||||
///
|
||||
/// type RegisterResult =
|
||||
/// Either<HttpResponse, Box<Future<Item = HttpResponse, Error = Error>>>;
|
||||
///
|
||||
/// fn index(req: HttpRequest) -> RegisterResult {
|
||||
/// if is_a_variant() { // <- choose variant A
|
||||
/// Either::A(
|
||||
/// HttpResponse::BadRequest().body("Bad data"))
|
||||
/// if is_a_variant() {
|
||||
/// // <- choose variant A
|
||||
/// Either::A(HttpResponse::BadRequest().body("Bad data"))
|
||||
/// } else {
|
||||
/// Either::B( // <- variant B
|
||||
/// Either::B(
|
||||
/// // <- variant B
|
||||
/// result(Ok(HttpResponse::Ok()
|
||||
/// .content_type("text/html")
|
||||
/// .body("Hello!")))
|
||||
/// .responder())
|
||||
/// .responder(),
|
||||
/// )
|
||||
/// }
|
||||
/// }
|
||||
/// # fn is_a_variant() -> bool { true }
|
||||
@ -138,9 +140,10 @@ where
|
||||
/// # extern crate actix_web;
|
||||
/// # extern crate futures;
|
||||
/// # #[macro_use] extern crate serde_derive;
|
||||
/// use futures::future::Future;
|
||||
/// use actix_web::{
|
||||
/// App, HttpRequest, HttpResponse, HttpMessage, Error, AsyncResponder};
|
||||
/// App, AsyncResponder, Error, HttpMessage, HttpRequest, HttpResponse,
|
||||
/// };
|
||||
/// use futures::future::Future;
|
||||
///
|
||||
/// #[derive(Deserialize, Debug)]
|
||||
/// struct MyObj {
|
||||
@ -479,10 +482,12 @@ where
|
||||
/// # extern crate actix_web;
|
||||
/// # extern crate futures;
|
||||
/// #[macro_use] extern crate serde_derive;
|
||||
/// use actix_web::{App, Path, State, http};
|
||||
/// use actix_web::{http, App, Path, State};
|
||||
///
|
||||
/// /// Application state
|
||||
/// struct MyApp {msg: &'static str}
|
||||
/// struct MyApp {
|
||||
/// msg: &'static str,
|
||||
/// }
|
||||
///
|
||||
/// #[derive(Deserialize)]
|
||||
/// struct Info {
|
||||
@ -498,7 +503,8 @@ where
|
||||
/// fn main() {
|
||||
/// let app = App::with_state(MyApp { msg: "Welcome" }).resource(
|
||||
/// "/{username}/index.html", // <- define path parameters
|
||||
/// |r| r.method(http::Method::GET).with(index)); // <- use `with` extractor
|
||||
/// |r| r.method(http::Method::GET).with(index),
|
||||
/// ); // <- use `with` extractor
|
||||
/// }
|
||||
/// ```
|
||||
pub struct State<S>(HttpRequest<S>);
|
||||
|
@ -283,9 +283,9 @@ impl<S> HttpRequest<S> {
|
||||
/// Generate url for named resource
|
||||
///
|
||||
/// ```rust
|
||||
/// # extern crate actix_web;
|
||||
/// # use actix_web::{App, HttpRequest, HttpResponse, http};
|
||||
/// #
|
||||
/// //#### # extern crate actix_web;
|
||||
/// //#### # 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
|
||||
/// HttpResponse::Ok().into()
|
||||
|
@ -297,11 +297,13 @@ impl HttpResponseBuilder {
|
||||
///
|
||||
/// ```rust
|
||||
/// # extern crate actix_web;
|
||||
/// use actix_web::{HttpRequest, HttpResponse, Result, http};
|
||||
/// use actix_web::{http, HttpRequest, HttpResponse, Result};
|
||||
///
|
||||
/// fn index(req: HttpRequest) -> Result<HttpResponse> {
|
||||
/// Ok(HttpResponse::Ok()
|
||||
/// .set(http::header::IfModifiedSince("Sun, 07 Nov 1994 08:48:37 GMT".parse()?))
|
||||
/// .set(http::header::IfModifiedSince(
|
||||
/// "Sun, 07 Nov 1994 08:48:37 GMT".parse()?,
|
||||
/// ))
|
||||
/// .finish())
|
||||
/// }
|
||||
/// fn main() {}
|
||||
@ -455,7 +457,8 @@ impl HttpResponseBuilder {
|
||||
/// .path("/")
|
||||
/// .secure(true)
|
||||
/// .http_only(true)
|
||||
/// .finish())
|
||||
/// .finish(),
|
||||
/// )
|
||||
/// .finish()
|
||||
/// }
|
||||
/// ```
|
||||
|
17
src/json.rs
17
src/json.rs
@ -32,11 +32,11 @@ use httpresponse::HttpResponse;
|
||||
/// ## Example
|
||||
///
|
||||
/// ```rust
|
||||
/// # extern crate actix_web;
|
||||
/// #[macro_use] extern crate serde_derive;
|
||||
/// //#### # extern crate actix_web;
|
||||
/// //#### #[macro_use] extern crate serde_derive;
|
||||
/// use actix_web::{App, Json, Result, http};
|
||||
///
|
||||
/// #[derive(Deserialize)]
|
||||
/// //#### #[derive(Deserialize)]
|
||||
/// struct Info {
|
||||
/// username: String,
|
||||
/// }
|
||||
@ -69,7 +69,9 @@ use httpresponse::HttpResponse;
|
||||
/// }
|
||||
///
|
||||
/// fn index(req: HttpRequest) -> Result<Json<MyObj>> {
|
||||
/// Ok(Json(MyObj{name: req.match_info().query("name")?}))
|
||||
/// Ok(Json(MyObj {
|
||||
/// name: req.match_info().query("name")?,
|
||||
/// }))
|
||||
/// }
|
||||
/// # fn main() {}
|
||||
/// ```
|
||||
@ -154,7 +156,7 @@ where
|
||||
/// ```rust
|
||||
/// # extern crate actix_web;
|
||||
/// #[macro_use] extern crate serde_derive;
|
||||
/// use actix_web::{App, Json, HttpResponse, Result, http, error};
|
||||
/// use actix_web::{error, http, App, HttpResponse, Json, Result};
|
||||
///
|
||||
/// #[derive(Deserialize)]
|
||||
/// struct Info {
|
||||
@ -167,8 +169,7 @@ where
|
||||
/// }
|
||||
///
|
||||
/// fn main() {
|
||||
/// let app = App::new().resource(
|
||||
/// "/index.html", |r| {
|
||||
/// let app = App::new().resource("/index.html", |r| {
|
||||
/// r.method(http::Method::POST)
|
||||
/// .with(index)
|
||||
/// .limit(4096) // <- change json extractor configuration
|
||||
@ -223,8 +224,8 @@ impl<S> Default for JsonConfig<S> {
|
||||
/// # extern crate actix_web;
|
||||
/// # extern crate futures;
|
||||
/// # #[macro_use] extern crate serde_derive;
|
||||
/// use actix_web::{AsyncResponder, Error, HttpMessage, HttpRequest, HttpResponse};
|
||||
/// use futures::future::Future;
|
||||
/// use actix_web::{AsyncResponder, HttpRequest, HttpResponse, HttpMessage, Error};
|
||||
///
|
||||
/// #[derive(Deserialize, Debug)]
|
||||
/// struct MyObj {
|
||||
|
@ -19,16 +19,16 @@
|
||||
//!
|
||||
//! ```rust
|
||||
//! # extern crate actix_web;
|
||||
//! use actix_web::{http, App, HttpRequest, HttpResponse};
|
||||
//! use actix_web::middleware::cors::Cors;
|
||||
//! use actix_web::{http, App, HttpRequest, HttpResponse};
|
||||
//!
|
||||
//! fn index(mut req: HttpRequest) -> &'static str {
|
||||
//! "Hello world"
|
||||
//! }
|
||||
//!
|
||||
//! fn main() {
|
||||
//! let app = App::new()
|
||||
//! .configure(|app| Cors::for_app(app) // <- Construct CORS middleware builder
|
||||
//! let app = App::new().configure(|app| {
|
||||
//! Cors::for_app(app) // <- Construct CORS middleware builder
|
||||
//! .allowed_origin("https://www.rust-lang.org/")
|
||||
//! .allowed_methods(vec!["GET", "POST"])
|
||||
//! .allowed_headers(vec![http::header::AUTHORIZATION, http::header::ACCEPT])
|
||||
@ -38,7 +38,8 @@
|
||||
//! r.method(http::Method::GET).f(|_| HttpResponse::Ok());
|
||||
//! r.method(http::Method::HEAD).f(|_| HttpResponse::MethodNotAllowed());
|
||||
//! })
|
||||
//! .register());
|
||||
//! .register()
|
||||
//! });
|
||||
//! }
|
||||
//! ```
|
||||
//! In this example custom *CORS* middleware get registered for "/index.html"
|
||||
@ -232,17 +233,19 @@ impl Cors {
|
||||
///
|
||||
/// ```rust
|
||||
/// # extern crate actix_web;
|
||||
/// use actix_web::{http, App, HttpResponse};
|
||||
/// use actix_web::middleware::cors::Cors;
|
||||
/// use actix_web::{http, App, HttpResponse};
|
||||
///
|
||||
/// fn main() {
|
||||
/// let app = App::new()
|
||||
/// .configure(|app| Cors::for_app(app) // <- Construct CORS builder
|
||||
/// let app = App::new().configure(
|
||||
/// |app| {
|
||||
/// Cors::for_app(app) // <- Construct CORS builder
|
||||
/// .allowed_origin("https://www.rust-lang.org/")
|
||||
/// .resource("/resource", |r| { // register resource
|
||||
/// r.method(http::Method::GET).f(|_| HttpResponse::Ok());
|
||||
/// })
|
||||
/// .register() // construct CORS and return application instance
|
||||
/// .register()
|
||||
/// }, // construct CORS and return application instance
|
||||
/// );
|
||||
/// }
|
||||
/// ```
|
||||
@ -491,8 +494,8 @@ impl<S> Middleware<S> for Cors {
|
||||
/// ```rust
|
||||
/// # extern crate http;
|
||||
/// # extern crate actix_web;
|
||||
/// use http::header;
|
||||
/// use actix_web::middleware::cors;
|
||||
/// use http::header;
|
||||
///
|
||||
/// # fn main() {
|
||||
/// let cors = cors::Cors::build()
|
||||
@ -764,12 +767,13 @@ impl<S: 'static> CorsBuilder<S> {
|
||||
///
|
||||
/// ```rust
|
||||
/// # extern crate actix_web;
|
||||
/// use actix_web::{http, App, HttpResponse};
|
||||
/// use actix_web::middleware::cors::Cors;
|
||||
/// use actix_web::{http, App, HttpResponse};
|
||||
///
|
||||
/// fn main() {
|
||||
/// let app = App::new()
|
||||
/// .configure(|app| Cors::for_app(app) // <- Construct CORS builder
|
||||
/// let app = App::new().configure(
|
||||
/// |app| {
|
||||
/// Cors::for_app(app) // <- Construct CORS builder
|
||||
/// .allowed_origin("https://www.rust-lang.org/")
|
||||
/// .allowed_methods(vec!["GET", "POST"])
|
||||
/// .allowed_header(http::header::CONTENT_TYPE)
|
||||
@ -781,7 +785,8 @@ impl<S: 'static> CorsBuilder<S> {
|
||||
/// r.method(http::Method::HEAD)
|
||||
/// .f(|_| HttpResponse::MethodNotAllowed());
|
||||
/// })
|
||||
/// .register() // construct CORS and return application instance
|
||||
/// .register()
|
||||
/// }, // construct CORS and return application instance
|
||||
/// );
|
||||
/// }
|
||||
/// ```
|
||||
|
@ -22,8 +22,8 @@
|
||||
//!
|
||||
//! ```
|
||||
//! # extern crate actix_web;
|
||||
//! use actix_web::{http, App, HttpRequest, HttpResponse};
|
||||
//! use actix_web::middleware::csrf;
|
||||
//! use actix_web::{http, App, HttpRequest, HttpResponse};
|
||||
//!
|
||||
//! fn handle_post(_: HttpRequest) -> &'static str {
|
||||
//! "This action should only be triggered with requests from the same site"
|
||||
@ -32,8 +32,8 @@
|
||||
//! fn main() {
|
||||
//! let app = App::new()
|
||||
//! .middleware(
|
||||
//! csrf::CsrfFilter::new()
|
||||
//! .allowed_origin("https://www.example.com"))
|
||||
//! csrf::CsrfFilter::new().allowed_origin("https://www.example.com"),
|
||||
//! )
|
||||
//! .resource("/", |r| {
|
||||
//! r.method(http::Method::GET).f(|_| HttpResponse::Ok());
|
||||
//! r.method(http::Method::POST).f(handle_post);
|
||||
@ -120,13 +120,12 @@ fn origin(headers: &HeaderMap) -> Option<Result<Cow<str>, CsrfError>> {
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use actix_web::App;
|
||||
/// use actix_web::middleware::csrf;
|
||||
/// use actix_web::App;
|
||||
///
|
||||
/// # fn main() {
|
||||
/// let app = App::new().middleware(
|
||||
/// csrf::CsrfFilter::new()
|
||||
/// .allowed_origin("https://www.example.com"));
|
||||
/// let app = App::new()
|
||||
/// .middleware(csrf::CsrfFilter::new().allowed_origin("https://www.example.com"));
|
||||
/// # }
|
||||
/// ```
|
||||
#[derive(Default)]
|
||||
|
@ -17,12 +17,11 @@ use middleware::{Middleware, Response};
|
||||
///
|
||||
/// fn main() {
|
||||
/// let app = App::new()
|
||||
/// .middleware(
|
||||
/// middleware::DefaultHeaders::new()
|
||||
/// .header("X-Version", "0.2"))
|
||||
/// .middleware(middleware::DefaultHeaders::new().header("X-Version", "0.2"))
|
||||
/// .resource("/test", |r| {
|
||||
/// r.method(http::Method::GET).f(|_| HttpResponse::Ok());
|
||||
/// r.method(http::Method::HEAD).f(|_| HttpResponse::MethodNotAllowed());
|
||||
/// r.method(http::Method::HEAD)
|
||||
/// .f(|_| HttpResponse::MethodNotAllowed());
|
||||
/// })
|
||||
/// .finish();
|
||||
/// }
|
||||
|
@ -18,8 +18,8 @@ type ErrorHandler<S> = Fn(&mut HttpRequest<S>, HttpResponse) -> Result<Response>
|
||||
///
|
||||
/// ```rust
|
||||
/// # extern crate actix_web;
|
||||
/// use actix_web::middleware::{ErrorHandlers, Response};
|
||||
/// use actix_web::{http, App, HttpRequest, HttpResponse, Result};
|
||||
/// use actix_web::middleware::{Response, ErrorHandlers};
|
||||
///
|
||||
/// fn render_500<S>(_: &mut HttpRequest<S>, resp: HttpResponse) -> Result<Response> {
|
||||
/// let mut builder = resp.into_builder();
|
||||
@ -31,10 +31,12 @@ type ErrorHandler<S> = Fn(&mut HttpRequest<S>, HttpResponse) -> Result<Response>
|
||||
/// let app = App::new()
|
||||
/// .middleware(
|
||||
/// ErrorHandlers::new()
|
||||
/// .handler(http::StatusCode::INTERNAL_SERVER_ERROR, render_500))
|
||||
/// .handler(http::StatusCode::INTERNAL_SERVER_ERROR, render_500),
|
||||
/// )
|
||||
/// .resource("/test", |r| {
|
||||
/// r.method(http::Method::GET).f(|_| HttpResponse::Ok());
|
||||
/// r.method(http::Method::HEAD).f(|_| HttpResponse::MethodNotAllowed());
|
||||
/// r.method(http::Method::HEAD)
|
||||
/// .f(|_| HttpResponse::MethodNotAllowed());
|
||||
/// })
|
||||
/// .finish();
|
||||
/// }
|
||||
|
@ -62,8 +62,8 @@ use middleware::{Middleware, Response, Started};
|
||||
/// The helper trait to obtain your identity from a request.
|
||||
///
|
||||
/// ```rust
|
||||
/// use actix_web::*;
|
||||
/// use actix_web::middleware::identity::RequestIdentity;
|
||||
/// use actix_web::*;
|
||||
///
|
||||
/// fn index(req: HttpRequest) -> Result<String> {
|
||||
/// // access request identity
|
||||
@ -144,16 +144,16 @@ pub trait IdentityPolicy<S>: Sized + 'static {
|
||||
///
|
||||
/// ```rust
|
||||
/// # extern crate actix_web;
|
||||
/// use actix_web::middleware::identity::{CookieIdentityPolicy, IdentityService};
|
||||
/// use actix_web::App;
|
||||
/// use actix_web::middleware::identity::{IdentityService, CookieIdentityPolicy};
|
||||
///
|
||||
/// fn main() {
|
||||
/// let app = App::new().middleware(
|
||||
/// IdentityService::new( // <- create identity middleware
|
||||
/// let app = App::new().middleware(IdentityService::new(
|
||||
/// // <- create identity middleware
|
||||
/// CookieIdentityPolicy::new(&[0; 32]) // <- create cookie session backend
|
||||
/// .name("auth-cookie")
|
||||
/// .secure(false))
|
||||
/// );
|
||||
/// .secure(false),
|
||||
/// ));
|
||||
/// }
|
||||
/// ```
|
||||
pub struct IdentityService<T> {
|
||||
@ -317,17 +317,18 @@ impl CookieIdentityInner {
|
||||
///
|
||||
/// ```rust
|
||||
/// # extern crate actix_web;
|
||||
/// use actix_web::middleware::identity::{CookieIdentityPolicy, IdentityService};
|
||||
/// use actix_web::App;
|
||||
/// use actix_web::middleware::identity::{IdentityService, CookieIdentityPolicy};
|
||||
///
|
||||
/// fn main() {
|
||||
/// let app = App::new().middleware(
|
||||
/// IdentityService::new( // <- create identity middleware
|
||||
/// let app = App::new().middleware(IdentityService::new(
|
||||
/// // <- create identity middleware
|
||||
/// CookieIdentityPolicy::new(&[0; 32]) // <- construct cookie policy
|
||||
/// .domain("www.rust-lang.org")
|
||||
/// .name("actix_auth")
|
||||
/// .path("/")
|
||||
/// .secure(true)));
|
||||
/// .secure(true),
|
||||
/// ));
|
||||
/// }
|
||||
/// ```
|
||||
pub struct CookieIdentityPolicy(Rc<CookieIdentityInner>);
|
||||
|
@ -31,8 +31,8 @@ use middleware::{Finished, Middleware, Started};
|
||||
/// ```rust
|
||||
/// # extern crate actix_web;
|
||||
/// extern crate env_logger;
|
||||
/// use actix_web::App;
|
||||
/// use actix_web::middleware::Logger;
|
||||
/// use actix_web::App;
|
||||
///
|
||||
/// fn main() {
|
||||
/// std::env::set_var("RUST_LOG", "actix_web=info");
|
||||
|
20
src/pred.rs
20
src/pred.rs
@ -22,10 +22,11 @@ pub trait Predicate<S> {
|
||||
/// use actix_web::{pred, App, HttpResponse};
|
||||
///
|
||||
/// fn main() {
|
||||
/// App::new()
|
||||
/// .resource("/index.html", |r| r.route()
|
||||
/// App::new().resource("/index.html", |r| {
|
||||
/// r.route()
|
||||
/// .filter(pred::Any(pred::Get()).or(pred::Post()))
|
||||
/// .f(|r| HttpResponse::MethodNotAllowed()));
|
||||
/// .f(|r| HttpResponse::MethodNotAllowed())
|
||||
/// });
|
||||
/// }
|
||||
/// ```
|
||||
pub fn Any<S: 'static, P: Predicate<S> + 'static>(pred: P) -> AnyPredicate<S> {
|
||||
@ -61,11 +62,14 @@ impl<S: 'static> Predicate<S> for AnyPredicate<S> {
|
||||
/// use actix_web::{pred, App, HttpResponse};
|
||||
///
|
||||
/// fn main() {
|
||||
/// App::new()
|
||||
/// .resource("/index.html", |r| r.route()
|
||||
/// .filter(pred::All(pred::Get())
|
||||
/// .and(pred::Header("content-type", "plain/text")))
|
||||
/// .f(|_| HttpResponse::MethodNotAllowed()));
|
||||
/// App::new().resource("/index.html", |r| {
|
||||
/// r.route()
|
||||
/// .filter(
|
||||
/// pred::All(pred::Get())
|
||||
/// .and(pred::Header("content-type", "plain/text")),
|
||||
/// )
|
||||
/// .f(|_| HttpResponse::MethodNotAllowed())
|
||||
/// });
|
||||
/// }
|
||||
/// ```
|
||||
pub fn All<S: 'static, P: Predicate<S> + 'static>(pred: P) -> AllPredicate<S> {
|
||||
|
@ -24,7 +24,7 @@ use route::Route;
|
||||
/// predicates route route considered matched and route handler get called.
|
||||
///
|
||||
/// ```rust
|
||||
/// # extern crate actix_web;
|
||||
/// //#### # extern crate actix_web;
|
||||
/// use actix_web::{App, HttpResponse, http};
|
||||
///
|
||||
/// fn main() {
|
||||
@ -82,11 +82,12 @@ impl<S: 'static> ResourceHandler<S> {
|
||||
///
|
||||
/// fn main() {
|
||||
/// let app = App::new()
|
||||
/// .resource(
|
||||
/// "/", |r| r.route()
|
||||
/// .resource("/", |r| {
|
||||
/// r.route()
|
||||
/// .filter(pred::Any(pred::Get()).or(pred::Put()))
|
||||
/// .filter(pred::Header("Content-Type", "text/plain"))
|
||||
/// .f(|r| HttpResponse::Ok()))
|
||||
/// .f(|r| HttpResponse::Ok())
|
||||
/// })
|
||||
/// .finish();
|
||||
/// }
|
||||
/// ```
|
||||
|
30
src/route.rs
30
src/route.rs
@ -66,13 +66,12 @@ impl<S: 'static> Route<S> {
|
||||
/// # extern crate actix_web;
|
||||
/// # use actix_web::*;
|
||||
/// # fn main() {
|
||||
/// App::new()
|
||||
/// .resource("/path", |r|
|
||||
/// App::new().resource("/path", |r| {
|
||||
/// r.route()
|
||||
/// .filter(pred::Get())
|
||||
/// .filter(pred::Header("content-type", "text/plain"))
|
||||
/// .f(|req| HttpResponse::Ok())
|
||||
/// )
|
||||
/// })
|
||||
/// # .finish();
|
||||
/// # }
|
||||
/// ```
|
||||
@ -115,7 +114,7 @@ impl<S: 'static> Route<S> {
|
||||
/// # extern crate actix_web;
|
||||
/// # extern crate futures;
|
||||
/// #[macro_use] extern crate serde_derive;
|
||||
/// use actix_web::{App, Path, Result, http};
|
||||
/// use actix_web::{http, App, Path, Result};
|
||||
///
|
||||
/// #[derive(Deserialize)]
|
||||
/// struct Info {
|
||||
@ -130,7 +129,8 @@ impl<S: 'static> Route<S> {
|
||||
/// fn main() {
|
||||
/// let app = App::new().resource(
|
||||
/// "/{username}/index.html", // <- define path parameters
|
||||
/// |r| r.method(http::Method::GET).with(index)); // <- use `with` extractor
|
||||
/// |r| r.method(http::Method::GET).with(index),
|
||||
/// ); // <- use `with` extractor
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
@ -143,7 +143,7 @@ impl<S: 'static> Route<S> {
|
||||
/// # extern crate futures;
|
||||
/// #[macro_use] extern crate serde_derive;
|
||||
/// # use std::collections::HashMap;
|
||||
/// use actix_web::{http, App, Query, Path, Result, Json};
|
||||
/// use actix_web::{http, App, Json, Path, Query, Result};
|
||||
///
|
||||
/// #[derive(Deserialize)]
|
||||
/// struct Info {
|
||||
@ -151,14 +151,17 @@ impl<S: 'static> Route<S> {
|
||||
/// }
|
||||
///
|
||||
/// /// extract path info using serde
|
||||
/// fn index(info: (Path<Info>, Query<HashMap<String, String>>, Json<Info>)) -> Result<String> {
|
||||
/// fn index(
|
||||
/// info: (Path<Info>, Query<HashMap<String, String>>, Json<Info>),
|
||||
/// ) -> Result<String> {
|
||||
/// Ok(format!("Welcome {}!", info.0.username))
|
||||
/// }
|
||||
///
|
||||
/// fn main() {
|
||||
/// let app = App::new().resource(
|
||||
/// "/{username}/index.html", // <- define path parameters
|
||||
/// |r| r.method(http::Method::GET).with(index)); // <- use `with` extractor
|
||||
/// |r| r.method(http::Method::GET).with(index),
|
||||
/// ); // <- use `with` extractor
|
||||
/// }
|
||||
/// ```
|
||||
pub fn with<T, F, R>(&mut self, handler: F) -> ExtractorConfig<S, T>
|
||||
@ -181,7 +184,7 @@ impl<S: 'static> Route<S> {
|
||||
/// # extern crate actix_web;
|
||||
/// # extern crate futures;
|
||||
/// #[macro_use] extern crate serde_derive;
|
||||
/// use actix_web::{App, Path, Error, http};
|
||||
/// use actix_web::{http, App, Error, Path};
|
||||
/// use futures::Future;
|
||||
///
|
||||
/// #[derive(Deserialize)]
|
||||
@ -197,8 +200,8 @@ impl<S: 'static> Route<S> {
|
||||
/// fn main() {
|
||||
/// let app = App::new().resource(
|
||||
/// "/{username}/index.html", // <- define path parameters
|
||||
/// |r| r.method(http::Method::GET)
|
||||
/// .with_async(index)); // <- use `with` extractor
|
||||
/// |r| r.method(http::Method::GET).with_async(index),
|
||||
/// ); // <- use `with` extractor
|
||||
/// }
|
||||
/// ```
|
||||
pub fn with_async<T, F, R, I, E>(&mut self, handler: F) -> ExtractorConfig<S, T>
|
||||
@ -222,7 +225,7 @@ impl<S: 'static> Route<S> {
|
||||
/// # extern crate actix_web;
|
||||
/// # extern crate futures;
|
||||
/// #[macro_use] extern crate serde_derive;
|
||||
/// use actix_web::{App, Query, Path, Result, http};
|
||||
/// use actix_web::{http, App, Path, Query, Result};
|
||||
///
|
||||
/// #[derive(Deserialize)]
|
||||
/// struct PParam {
|
||||
@ -242,7 +245,8 @@ impl<S: 'static> Route<S> {
|
||||
/// fn main() {
|
||||
/// let app = App::new().resource(
|
||||
/// "/{username}/index.html", // <- define path parameters
|
||||
/// |r| r.method(http::Method::GET).with2(index)); // <- use `with` extractor
|
||||
/// |r| r.method(http::Method::GET).with2(index),
|
||||
/// ); // <- use `with` extractor
|
||||
/// }
|
||||
/// ```
|
||||
pub fn with2<T1, T2, F, R>(
|
||||
|
41
src/scope.rs
41
src/scope.rs
@ -38,9 +38,9 @@ type NestedInfo<S> = (Resource, Route<S>, Vec<Box<Predicate<S>>>);
|
||||
/// use actix_web::{http, App, HttpRequest, HttpResponse};
|
||||
///
|
||||
/// fn main() {
|
||||
/// let app = App::new()
|
||||
/// .scope("/{project_id}/", |scope| {
|
||||
/// scope.resource("/path1", |r| r.f(|_| HttpResponse::Ok()))
|
||||
/// let app = App::new().scope("/{project_id}/", |scope| {
|
||||
/// scope
|
||||
/// .resource("/path1", |r| r.f(|_| HttpResponse::Ok()))
|
||||
/// .resource("/path2", |r| r.f(|_| HttpResponse::Ok()))
|
||||
/// .resource("/path3", |r| r.f(|_| HttpResponse::MethodNotAllowed()))
|
||||
/// });
|
||||
@ -89,12 +89,13 @@ impl<S: 'static> Scope<S> {
|
||||
/// }
|
||||
///
|
||||
/// fn main() {
|
||||
/// let app = App::new()
|
||||
/// .scope("/app", |scope| {
|
||||
/// scope.filter(pred::Header("content-type", "text/plain"))
|
||||
/// let app = App::new().scope("/app", |scope| {
|
||||
/// scope
|
||||
/// .filter(pred::Header("content-type", "text/plain"))
|
||||
/// .route("/test1", http::Method::GET, index)
|
||||
/// .route("/test2", http::Method::POST,
|
||||
/// |_: HttpRequest| HttpResponse::MethodNotAllowed())
|
||||
/// .route("/test2", http::Method::POST, |_: HttpRequest| {
|
||||
/// HttpResponse::MethodNotAllowed()
|
||||
/// })
|
||||
/// });
|
||||
/// }
|
||||
/// ```
|
||||
@ -116,8 +117,7 @@ impl<S: 'static> Scope<S> {
|
||||
/// }
|
||||
///
|
||||
/// fn main() {
|
||||
/// let app = App::new()
|
||||
/// .scope("/app", |scope| {
|
||||
/// let app = App::new().scope("/app", |scope| {
|
||||
/// scope.with_state("/state2", AppState, |scope| {
|
||||
/// scope.resource("/test1", |r| r.f(index))
|
||||
/// })
|
||||
@ -162,11 +162,8 @@ impl<S: 'static> Scope<S> {
|
||||
/// }
|
||||
///
|
||||
/// fn main() {
|
||||
/// let app = App::with_state(AppState)
|
||||
/// .scope("/app", |scope| {
|
||||
/// scope.nested("/v1", |scope| {
|
||||
/// scope.resource("/test1", |r| r.f(index))
|
||||
/// })
|
||||
/// let app = App::with_state(AppState).scope("/app", |scope| {
|
||||
/// scope.nested("/v1", |scope| scope.resource("/test1", |r| r.f(index)))
|
||||
/// });
|
||||
/// }
|
||||
/// ```
|
||||
@ -211,11 +208,12 @@ impl<S: 'static> Scope<S> {
|
||||
/// }
|
||||
///
|
||||
/// fn main() {
|
||||
/// let app = App::new()
|
||||
/// .scope("/app", |scope| {
|
||||
/// scope.route("/test1", http::Method::GET, index)
|
||||
/// .route("/test2", http::Method::POST,
|
||||
/// |_: HttpRequest| HttpResponse::MethodNotAllowed())
|
||||
/// let app = App::new().scope("/app", |scope| {
|
||||
/// scope.route("/test1", http::Method::GET, index).route(
|
||||
/// "/test2",
|
||||
/// http::Method::POST,
|
||||
/// |_: HttpRequest| HttpResponse::MethodNotAllowed(),
|
||||
/// )
|
||||
/// });
|
||||
/// }
|
||||
/// ```
|
||||
@ -261,8 +259,7 @@ impl<S: 'static> Scope<S> {
|
||||
/// use actix_web::*;
|
||||
///
|
||||
/// fn main() {
|
||||
/// let app = App::new()
|
||||
/// .scope("/api", |scope| {
|
||||
/// let app = App::new().scope("/api", |scope| {
|
||||
/// scope.resource("/users/{userid}/{friend}", |r| {
|
||||
/// r.get().f(|_| HttpResponse::Ok());
|
||||
/// r.head().f(|_| HttpResponse::MethodNotAllowed());
|
||||
|
20
src/with.rs
20
src/with.rs
@ -19,7 +19,7 @@ use httpresponse::HttpResponse;
|
||||
/// ```rust
|
||||
/// # extern crate actix_web;
|
||||
/// #[macro_use] extern crate serde_derive;
|
||||
/// use actix_web::{App, Form, Result, http};
|
||||
/// use actix_web::{http, App, Form, Result};
|
||||
///
|
||||
/// #[derive(Deserialize)]
|
||||
/// struct FormData {
|
||||
@ -32,10 +32,10 @@ use httpresponse::HttpResponse;
|
||||
///
|
||||
/// fn main() {
|
||||
/// let app = App::new().resource(
|
||||
/// "/index.html", |r| {
|
||||
/// r.method(http::Method::GET)
|
||||
/// .with(index)
|
||||
/// .limit(4096);} // <- change form extractor configuration
|
||||
/// "/index.html",
|
||||
/// |r| {
|
||||
/// r.method(http::Method::GET).with(index).limit(4096);
|
||||
/// }, // <- change form extractor configuration
|
||||
/// );
|
||||
/// }
|
||||
/// ```
|
||||
@ -45,7 +45,7 @@ use httpresponse::HttpResponse;
|
||||
/// ```rust
|
||||
/// # extern crate actix_web;
|
||||
/// #[macro_use] extern crate serde_derive;
|
||||
/// use actix_web::{App, Form, Path, Result, http};
|
||||
/// use actix_web::{http, App, Form, Path, Result};
|
||||
///
|
||||
/// #[derive(Deserialize)]
|
||||
/// struct FormData {
|
||||
@ -58,10 +58,10 @@ use httpresponse::HttpResponse;
|
||||
///
|
||||
/// fn main() {
|
||||
/// let app = App::new().resource(
|
||||
/// "/index.html", |r| {
|
||||
/// r.method(http::Method::GET)
|
||||
/// .with(index)
|
||||
/// .1.limit(4096);} // <- change form extractor configuration
|
||||
/// "/index.html",
|
||||
/// |r| {
|
||||
/// r.method(http::Method::GET).with(index).1.limit(4096);
|
||||
/// }, // <- change form extractor configuration
|
||||
/// );
|
||||
/// }
|
||||
/// ```
|
||||
|
Loading…
Reference in New Issue
Block a user