Struct actix_web::Application [] [src]

pub struct Application<S = ()> { /* fields omitted */ }

Structure that follows the builder pattern for building Application structs.

Methods

impl Application<()>
[src]

[src]

Create application with empty state. Application can be configured with builder-like pattern.

impl<S> Application<S> where
    S: 'static, 
[src]

[src]

Create application with specific state. Application can be configured with builder-like pattern.

State is shared with all reousrces within same application and could be accessed with HttpRequest::state() method.

[src]

Set application prefix

Only requests that matches application's prefix get processed by this application. Application prefix always contains laading "/" slash. If supplied prefix does not contain leading slash, it get inserted. Prefix should consists valid path segments. i.e for application with prefix /app any request with following paths /app, /app/ or /app/test would match, but path /application would not match.

In the following example only requests with "/app/" path prefix get handled. Request with path "/app/test/" would be handled, but request with path "/application" or "/other/..." would return NOT FOUND

use actix_web::*;

fn main() {
    let app = Application::new()
        .prefix("/app")
        .resource("/test", |r| {
             r.method(Method::GET).f(|_| httpcodes::HTTPOk);
             r.method(Method::HEAD).f(|_| httpcodes::HTTPMethodNotAllowed);
        })
        .finish();
}

[src]

Configure resource for specific path.

Resource may have variable path also. For instance, a resource with the path /a/{name}/c would match all incoming requests with paths such as /a/b/c, /a/1/c, and /a/etc/c.

A variable part is specified in the form {identifier}, where the identifier can be used later in a request handler to access the matched value for that part. This is done by looking up the identifier in the Params object returned by HttpRequest.match_info() method.

By default, each part matches the regular expression [^{}/]+.

You can also specify a custom regex in the form {identifier:regex}:

For instance, to route Get requests on any route matching /users/{userid}/{friend} and store userid and friend in the exposed Params object:

use actix_web::*;

fn main() {
    let app = Application::new()
        .resource("/test", |r| {
             r.method(Method::GET).f(|_| httpcodes::HTTPOk);
             r.method(Method::HEAD).f(|_| httpcodes::HTTPMethodNotAllowed);
        });
}

[src]

Default resource is used if no matched route could be found.

[src]

Register external resource.

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

use actix_web::*;

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

fn main() {
    let app = Application::new()
        .resource("/index.html", |r| r.f(index))
        .external_resource("youtube", "https://youtube.com/watch/{video_id}")
        .finish();
}

[src]

Configure handler for specific path prefix.

Path prefix consists valid path segments. i.e for prefix /app any request with following paths /app, /app/ or /app/test would match, but path /application would not match.

use actix_web::*;

fn main() {
    let app = Application::new()
        .handler("/app", |req: HttpRequest| {
            match *req.method() {
                Method::GET => httpcodes::HTTPOk,
                Method::POST => httpcodes::HTTPMethodNotAllowed,
                _ => httpcodes::HTTPNotFound,
        }});
}

[src]

Register a middleware

[src]

Finish application configuration and create HttpHandler object

Trait Implementations

impl Default for Application<()>
[src]

[src]

Returns the "default value" for a type. Read more

impl<S: 'static> IntoHttpHandler for Application<S>
[src]

The associated type which is result of conversion.

[src]

Convert into HttpHandler object.

impl<'a, S: 'static> IntoHttpHandler for &'a mut Application<S>
[src]

The associated type which is result of conversion.

[src]

Convert into HttpHandler object.