[][src]Struct actix_web::Route

pub struct Route { /* fields omitted */ }

Resource route definition

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

Methods

impl Route[src]

pub fn new() -> Route[src]

Create new route which matches any request.

impl Route[src]

pub fn method(self, method: Method) -> Self[src]

Add method guard to the route.

App::new().service(web::resource("/path").route(
    web::get()
        .method(http::Method::CONNECT)
        .guard(guard::Header("content-type", "text/plain"))
        .to(|req: HttpRequest| HttpResponse::Ok()))
);

pub fn guard<F: Guard + 'static>(self, f: F) -> Self[src]

Add guard to the route.

App::new().service(web::resource("/path").route(
    web::route()
        .guard(guard::Get())
        .guard(guard::Header("content-type", "text/plain"))
        .to(|req: HttpRequest| HttpResponse::Ok()))
);

pub fn to<F, T, R>(self, handler: F) -> Route where
    F: Factory<T, R> + 'static,
    T: FromRequest + 'static,
    R: Responder + 'static, 
[src]

Set handler function, use request extractors for parameters.

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

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

/// extract path info using serde
fn index(info: web::Path<Info>) -> String {
    format!("Welcome {}!", info.username)
}

fn main() {
    let app = App::new().service(
        web::resource("/{username}/index.html") // <- define path parameters
            .route(web::get().to(index))        // <- register handler
    );
}

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

use actix_web::{web, App};

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

/// extract path info using serde
fn index(path: web::Path<Info>, query: web::Query<HashMap<String, String>>, body: web::Json<Info>) -> String {
    format!("Welcome {}!", path.username)
}

fn main() {
    let app = App::new().service(
        web::resource("/{username}/index.html") // <- define path parameters
            .route(web::get().to(index))
    );
}

pub fn to_async<F, T, R>(self, handler: F) -> Self where
    F: AsyncFactory<T, R>,
    T: FromRequest + 'static,
    R: IntoFuture + 'static,
    R::Item: Responder,
    R::Error: Into<Error>, 
[src]

Set async handler function, use request extractors for parameters. This method has to be used if your handler function returns impl Future<>

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

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

/// extract path info using serde
fn index(info: web::Path<Info>) -> impl Future<Item = &'static str, Error = Error> {
    ok("Hello World!")
}

fn main() {
    let app = App::new().service(
        web::resource("/{username}/index.html") // <- define path parameters
            .route(web::get().to_async(index))  // <- register async handler
    );
}

Trait Implementations

impl NewService for Route[src]

type Config = ()

Service factory configuration

type Request = ServiceRequest

Requests handled by the service.

type Response = ServiceResponse

Responses given by the service

type Error = Error

Errors produced by the service

type InitError = ()

Errors produced while building a service.

type Service = RouteService

The Service value created by this factory

type Future = CreateRouteService

The future of the Service instance.

Auto Trait Implementations

impl !Send for Route

impl Unpin for Route

impl !Sync for Route

impl !UnwindSafe for Route

impl !RefUnwindSafe for Route

Blanket Implementations

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> From<T> for T[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> IntoNewService<T> for T where
    T: NewService
[src]

impl<T> Erased for T

impl<V, T> VZip<V> for T where
    V: MultiLane<T>,