pub struct CORSMiddleware { /* private fields */ }
Expand description

Struct to perform the necessary CORS functionality needed. Allows some customisation through use of the new() function.

Example of use:

extern crate gotham;
extern crate gotham_cors_middleware;

use gotham::pipeline::new_pipeline;
use gotham_cors_middleware::CORSMiddleware;
use gotham::pipeline::single::single_pipeline;
use gotham::router::builder::*;
use gotham::router::Router;

pub fn router() -> Router {
    let (chain, pipeline) = single_pipeline(
        new_pipeline()
            .add(CORSMiddleware::default())
            .build()
    );

    build_router(chain, pipeline, |route| {
        // Routes
    })
}

Implementations

Create a new CORSMiddleware with custom methods, origin and max_age properties.

Expects methods to be a Vec of hyper::Method enum values, origin to be an Option containing a String (so allows for None values - which defaults to returning the sender origin on request or returning a string of “*” - see the call function source) and max age to be a u32 value.

Example of use:

extern crate gotham;
extern crate gotham_cors_middleware;
extern crate hyper;

use gotham::pipeline::new_pipeline;
use gotham_cors_middleware::CORSMiddleware;
use gotham::pipeline::single::single_pipeline;
use gotham::router::builder::*;
use gotham::router::Router;
use hyper::Method;

fn create_custom_middleware() -> CORSMiddleware {
    let methods = vec![Method::Delete, Method::Get, Method::Head, Method::Options];

    let max_age = 1000;

    let origin = Some("http://www.example.com".to_string());

    CORSMiddleware::new(methods, origin, max_age)
}

pub fn router() -> Router {
    let (chain, pipeline) = single_pipeline(
        new_pipeline()
            .add(create_custom_middleware())
            .build()
    );

    build_router(chain, pipeline, |route| {
        // Routes
    })
}

Creates a new CORSMiddleware with what is currently the “default” values for methods/origin/max_age.

This is based off the values that were used previously before they were customisable. If you need different values, use the new() function.

Trait Implementations

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Entry point to the middleware. To pass the request on to the application, the middleware invokes the chain function with the provided state. Read more
The type of Middleware created by the NewMiddleware.
Create and return a new Middleware value.
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. 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.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
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.