3 releases

0.5.0-rc Mar 20, 2020
0.5.0-beta Mar 4, 2020
0.5.0-alpha Mar 3, 2020

#7 in #koa


Used in roa-websocket

MIT license

100KB
2K SLoC

Stable Test codecov Rust Docs Crate version Download Version License: MIT

Roa-router

The router module of roa. This module provides an endpoint RouteEndpoint and a context extension RouterParam.

Example

use roa_router::{Router, RouterParam};
use roa_core::App;
use roa_core::http::StatusCode;
use roa_tcp::Listener;
use async_std::task::spawn;

#[tokio::test]
async fn gate() -> Result<(), Box<dyn std::error::Error>> {
    let mut router = Router::<()>::new();
    router
        .gate_fn(|_ctx, next| next)
        .get("/", |_ctx| async move {
            Ok(())
        });
    let (addr, server) = App::new(()).gate(router.routes("/route")?).run()?;
    spawn(server);
    let resp = reqwest::get(&format!("http://{}/route", addr)).await?;
    assert_eq!(StatusCode::OK, resp.status());

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

Dependencies

~9.5MB
~181K SLoC