Struct actix_web::dev::Route

source ·
pub struct Route<S> { /* private fields */ }
Expand description

Resource route definition

Route uses builder-like pattern for configuration. If handler is not explicitly set, default 404 Not Found handler is used.

Implementations

Add match predicate to route.

App::new().resource("/path", |r| {
    r.route()
        .filter(pred::Get())
        .filter(pred::Header("content-type", "text/plain"))
        .f(|req| HttpResponse::Ok())
})

Set handler object. Usually call to this method is last call during route configuration, so it does not return reference to self.

Set handler function. Usually call to this method is last call during route configuration, so it does not return reference to self.

Set async handler function.

Set handler function, use request extractor for parameters.

#[macro_use] extern crate serde_derive;
use actix_web::{http, App, Path, Result};

#[derive(Deserialize)]
struct Info {
    username: String,
}

/// extract path info using serde
fn index(info: Path<Info>) -> Result<String> {
    Ok(format!("Welcome {}!", info.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
}

It is possible to use multiple extractors for one handler function.

#[macro_use] extern crate serde_derive;
use actix_web::{http, App, Json, Path, Query, Result};

#[derive(Deserialize)]
struct Info {
    username: String,
}

/// extract path info using serde
fn index(
    path: Path<Info>, query: Query<HashMap<String, String>>, body: Json<Info>,
) -> Result<String> {
    Ok(format!("Welcome {}!", path.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
}

Set handler function. Same as .with() but it allows to configure extractor. Configuration closure accepts config objects as tuple.

#[macro_use] extern crate serde_derive;
use actix_web::{http, App, Path, Result};

/// extract text data from request
fn index(body: String) -> Result<String> {
    Ok(format!("Body {}!", body))
}

fn main() {
    let app = App::new().resource("/index.html", |r| {
        r.method(http::Method::GET)
               .with_config(index, |cfg| { // <- register handler
                  cfg.0.limit(4096);  // <- limit size of the payload
                })
    });
}

Set async handler function, use request extractor for parameters. Also this method needs to be used if your handler function returns impl Future<>

#[macro_use] extern crate serde_derive;
use actix_web::{http, App, Error, Path};
use futures::Future;

#[derive(Deserialize)]
struct Info {
    username: String,
}

/// extract path info using serde
fn index(info: Path<Info>) -> Box<Future<Item = &'static str, Error = Error>> {
    unimplemented!()
}

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
}

Set async handler function, use request extractor for parameters. This method allows to configure extractor. Configuration closure accepts config objects as tuple.

#[macro_use] extern crate serde_derive;
use actix_web::{http, App, Error, Form};
use futures::Future;

#[derive(Deserialize)]
struct Info {
    username: String,
}

/// extract path info using serde
fn index(info: Form<Info>) -> Box<Future<Item = &'static str, Error = Error>> {
    unimplemented!()
}

fn main() {
    let app = App::new().resource(
        "/{username}/index.html", // <- define path parameters
        |r| r.method(http::Method::GET)
           .with_async_config(index, |cfg| {
               cfg.0.limit(4096);
           }),
    ); // <- use `with` extractor
}

Trait Implementations

Returns the “default value” for a type. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Should always be Self
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.