[][src]Struct actix_web::dev::AppRouter

pub struct AppRouter<C, P, B, T> { /* fields omitted */ }

Application router builder - Structure that follows the builder pattern for building application instances.

Methods

impl<C, P, B, T> AppRouter<C, P, B, T> where
    P: 'static,
    B: MessageBody,
    T: NewService<Request = ServiceRequest<P>, Response = ServiceResponse<B>, Error = Error, InitError = ()>, 
[src]

pub fn configure<F>(self, f: F) -> Self where
    F: Fn(&mut RouterConfig<P>), 
[src]

Run external configuration as part of the application building process

This function is useful for moving parts of configuration to a different module or even library. For example, some of the resource's configuration could be moved to different module.

use actix_web::{web, middleware, App, HttpResponse};

// this function could be located in different module
fn config<P>(cfg: &mut web::RouterConfig<P>) {
    cfg.service(web::resource("/test")
        .route(web::get().to(|| HttpResponse::Ok()))
        .route(web::head().to(|| HttpResponse::MethodNotAllowed()))
    );
}

fn main() {
    let app = App::new()
        .wrap(middleware::Logger::default())
        .configure(config)  // <- register resources
        .route("/index.html", web::get().to(|| HttpResponse::Ok()));
}

pub fn route(self, path: &str, route: Route<P>) -> Self[src]

Configure route for a specific path.

This is a simplified version of the App::service() method. This method can not be could multiple times, in that case multiple resources with one route would be registered for same resource path.

use actix_web::{web, App, HttpResponse};

fn index(data: web::Path<(String, String)>) -> &'static str {
    "Welcome!"
}

fn main() {
    let app = App::new()
        .route("/test1", web::get().to(index))
        .route("/test2", web::post().to(|| HttpResponse::MethodNotAllowed()));
}

pub fn service<F>(self, factory: F) -> Self where
    F: HttpServiceFactory<P> + 'static, 
[src]

Register http service.

Http service is any type that implements HttpServiceFactory trait.

Actix web provides several services implementations:

  • Resource is an entry in resource table which corresponds to requested URL.
  • Scope is a set of resources with common root path.
  • "StaticFiles" is a service for static files support

pub fn wrap<M, B1, F>(
    self,
    mw: F
) -> AppRouter<C, P, B1, impl NewService<Request = ServiceRequest<P>, Response = ServiceResponse<B1>, Error = Error, InitError = ()>> where
    M: Transform<T::Service, Request = ServiceRequest<P>, Response = ServiceResponse<B1>, Error = Error, InitError = ()>,
    B1: MessageBody,
    F: IntoTransform<M, T::Service>, 
[src]

Registers middleware, in the form of a middleware component (type), that runs during inbound and/or outbound processing in the request lifecycle (request -> response), modifying request/response as necessary, across all requests managed by the Route.

Use middleware when you need to read or modify every request or response in some way.

pub fn wrap_fn<B1, F, R>(
    self,
    mw: F
) -> AppRouter<C, P, B1, impl NewService<Request = ServiceRequest<P>, Response = ServiceResponse<B1>, Error = Error, InitError = ()>> where
    B1: MessageBody,
    F: FnMut(ServiceRequest<P>, &mut T::Service) -> R + Clone,
    R: IntoFuture<Item = ServiceResponse<B1>, Error = Error>, 
[src]

Registers middleware, in the form of a closure, that runs during inbound and/or outbound processing in the request lifecycle (request -> response), modifying request/response as necessary, across all requests managed by the Route.

Use middleware when you need to read or modify every request or response in some way.

pub fn default_resource<F, U>(self, f: F) -> Self where
    F: FnOnce(Resource<P>) -> Resource<P, U>,
    U: NewService<Request = ServiceRequest<P>, Response = ServiceResponse, Error = Error, InitError = ()> + 'static, 
[src]

Default resource to be used if no matching resource could be found.

pub fn external_resource<N, U>(self, name: N, url: U) -> Self where
    N: AsRef<str>,
    U: AsRef<str>, 
[src]

Register an external resource.

External resources are useful for URL generation purposes only and are never considered for matching at request time. Calls to HttpRequest::url_for() will work as expected.

use actix_web::{web, App, HttpRequest, HttpResponse, Result};

fn index(req: HttpRequest) -> Result<HttpResponse> {
    let url = req.url_for("youtube", &["asdlkjqme"])?;
    assert_eq!(url.as_str(), "https://youtube.com/watch/asdlkjqme");
    Ok(HttpResponse::Ok().into())
}

fn main() {
    let app = App::new()
        .service(web::resource("/index.html").route(
            web::get().to(index)))
        .external_resource("youtube", "https://youtube.com/watch/{video_id}");
}

Auto Trait Implementations

impl<C, P, B, T> !Send for AppRouter<C, P, B, T>

impl<C, P, B, T> !Sync for AppRouter<C, P, B, T>

Blanket Implementations

impl<T> From for T[src]

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

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

type Error = Infallible

The type returned in the event of a conversion error.

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

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

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

impl<T, U> TryInto 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> Erased for T