[][src]Module roa::router

This is supported on feature="router" only.

This module provides a context extension RouterParam and many endpoint wrappers like Router, Dispatcher and Guard.

Example

use roa::router::{Router, RouterParam, get, allow};
use roa::{App, Context, Status, MiddlewareExt, Next};
use roa::http::{StatusCode, Method};
use roa::tcp::Listener;
use async_std::task::spawn;


async fn gate(_ctx: &mut Context, next: Next<'_>) -> Result<(), Status> {
    next.await
}

async fn query(ctx: &mut Context) -> Result<(), Status> {
    Ok(())
}

async fn create(ctx: &mut Context) -> Result<(), Status> {
    Ok(())
}

async fn graphql(ctx: &mut Context) -> Result<(), Status> {
    Ok(())
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let router = Router::new()
        .gate(gate)
        .on("/restful", get(query).post(create))
        .on("/graphql", allow([Method::GET, Method::POST], graphql));
    let app = App::new()
        .end(router.routes("/api")?);
    let (addr, server) = app.run()?;
    spawn(server);
    let resp = reqwest::get(&format!("http://{}/api/restful", addr)).await?;
    assert_eq!(StatusCode::OK, resp.status());

    let resp = reqwest::get(&format!("http://{}/restful", addr)).await?;
    assert_eq!(StatusCode::NOT_FOUND, resp.status());
    Ok(())
}

Structs

Dispatcherfeature="router"

An endpoint wrapper to dispatch requests by http method.

Guardfeature="router"

An endpoint wrapper to guard endpoint by http method.

RouteTablefeature="router"

An endpoint to route request by uri path.

Routerfeature="router"

A builder of RouteTable.

Enums

RouterErrorfeature="router"

Error occurring in building route table.

Traits

RouterParamfeature="router"

A context extension. This extension must be used in Router, otherwise you cannot get expected router parameters.

Functions

allowfeature="router"

A function to construct guard by white list.

connectfeature="router"

Function to construct dispatcher with Method::CONNECT and an endpoint.

deletefeature="router"

Function to construct dispatcher with Method::DELETE and an endpoint.

denyfeature="router"

A function to construct guard by black list.

getfeature="router"

Function to construct dispatcher with Method::GET and an endpoint.

headfeature="router"

Function to construct dispatcher with Method::HEAD and an endpoint.

optionsfeature="router"

Function to construct dispatcher with Method::OPTIONS and an endpoint.

patchfeature="router"

Function to construct dispatcher with Method::PATCH and an endpoint.

postfeature="router"

Function to construct dispatcher with Method::POST and an endpoint.

putfeature="router"

Function to construct dispatcher with Method::PUT and an endpoint.

tracefeature="router"

Function to construct dispatcher with Method::TRACE and an endpoint.