[][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.

Implementations

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, U>(self, handler: F) -> Self where
    F: Factory<T, R, U>,
    T: FromRequest + 'static,
    R: Future<Output = U> + 'static,
    U: Responder + 'static, 
[src]

Set handler function, use request extractors for parameters.

use actix_web::{web, http, App};
use serde_derive::Deserialize;

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

/// extract path info using serde
async 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
async 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))
    );
}

Trait Implementations

impl ServiceFactory for Route[src]

type Config = ()

Service factory configuration.

type Request = ServiceRequest

Requests handled by the created services.

type Response = ServiceResponse

Responses given by the created services.

type Error = Error

Errors produced by the created services.

type InitError = ()

Errors potentially raised while building a service.

type Service = RouteService

The kind of Service created by this factory.

type Future = CreateRouteService

The future of the Service instance.

Auto Trait Implementations

impl !RefUnwindSafe for Route

impl !Send for Route

impl !Sync for Route

impl Unpin for Route

impl !UnwindSafe for Route

Blanket Implementations

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

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

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

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

impl<T> Instrument for T[src]

impl<T> Instrument for T[src]

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

impl<T> IntoServiceFactory<T> for T where
    T: ServiceFactory
[src]

impl<T> Same<T> for T

type Output = T

Should always be Self

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<V, T> VZip<V> for T where
    V: MultiLane<T>,