Trait gotham::router::builder::DefineSingleRoute [] [src]

pub trait DefineSingleRoute {
    fn to<H>(self, handler: H)
    where
        H: Handler + RefUnwindSafe + Copy + Send + Sync + 'static
;
fn to_new_handler<NH>(self, new_handler: NH)
    where
        NH: NewHandler + 'static
;
fn with_path_extractor<NPE>(self) -> Self::Output
    where
        NPE: PathExtractor + Send + Sync + 'static,
        Self: ReplacePathExtractor<NPE>,
        Self::Output: DefineSingleRoute
;
fn with_query_string_extractor<NQSE>(self) -> Self::Output
    where
        NQSE: QueryStringExtractor + Send + Sync + 'static,
        Self: ReplaceQueryStringExtractor<NQSE>,
        Self::Output: DefineSingleRoute
; }

Describes the API for defining a single route, after determining which request paths will be dispatched here. The API here uses chained function calls to build and add the route into the RouterBuilder which created it.

Examples

fn my_handler(state: State) -> (State, Response) {
    // Handler implementation elided.
}
build_router(chain, pipelines, |route| {
    route.get("/request/path") // <- This value implements `DefineSingleRoute`
         .to(my_handler);
})

Required Methods

Directs the route to the given Handler, automatically creating a NewHandler which copies the Handler. This is the easiest option for code which is using bare functions as Handler functions.

Examples

fn my_handler(state: State) -> (State, Response) {
    // Handler implementation elided.
}

build_router(chain, pipelines, |route| {
    route.get("/request/path").to(my_handler);
})

Directs the route to the given NewHandler. This gives more control over how Handler values are constructed.

Examples

struct MyNewHandler;
struct MyHandler;

impl NewHandler for MyNewHandler {
    type Instance = MyHandler;

    fn new_handler(&self) -> io::Result<Self::Instance> {
        Ok(MyHandler)
    }
}

impl Handler for MyHandler {
    fn handle(self, state: State) -> Box<HandlerFuture> {
        // Handler implementation elided.
    }
}

build_router(chain, pipelines, |route| {
    route.get("/request/path").to_new_handler(MyNewHandler);
})

Applies a PathExtractor type to the current route, to extract path parameters into State with the given type.

Examples

#[derive(Deserialize, StateData, StaticResponseExtender)]
struct MyPathParams {
    name: String,
}

fn my_handler(state: State) -> (State, Response) {
    let params = MyPathParams::borrow_from(&state);

    // Handler implementation elided.
}

build_router(default_pipeline_chain, pipelines, |route| {
    route.get("/hello/:name")
         .with_path_extractor::<MyPathParams>()
         .to(my_handler);
})

Applies a QueryStringExtractor type to the current route, to extract query parameters into State with the given type.

Examples

#[derive(StateData, Deserialize, StaticResponseExtender)]
struct MyQueryParams {
    id: u64,
}

fn my_handler(state: State) -> (State, Response) {
    let id = MyQueryParams::borrow_from(&state).id;

    // Handler implementation elided.
}

build_router(default_pipeline_chain, pipelines, |route| {
    route.get("/request/path")
         .with_query_string_extractor::<MyQueryParams>()
         .to(my_handler);
})

Implementors