Skip to main content

Module routing

Module routing 

Source
Expand description

Layer-7 routing: pick a document root by host, path prefix, method and headers.

A Router holds validated Routes. Each route mounts one document root under a path prefix; a request that matches is served from that root with the prefix removed. When a server has a router, a request that matches no route gets a plain 404 Not Found, never the server’s own document root, so routing is deterministic. Add a route with the prefix / as an explicit fallback.

Routes are tried from the highest Route::priority down. Among routes of equal priority the more specific one wins: a route with a host before one without, then the longer path prefix, then the route with more header constraints, then one with a method constraint. Two routes with the same priority and the same constraints are rejected as ambiguous by Router::new.

§Examples

use http_handle::routing::{Route, Router};
use http_handle::Server;

let router = Router::new(vec![
    Route::new("docs", "/docs", ".").host("docs.example.com"),
    Route::new("site", "/", "."),
])
.expect("valid routes");

let server = Server::builder()
    .address("127.0.0.1:0")
    .document_root(".")
    .router(router)
    .build()
    .expect("server");
assert_eq!(server.router().map(|r| r.routes().len()), Some(2));

Structs§

Route
One routing rule: request constraints and the document root it serves.
Router
A validated, ordered set of routes.

Enums§

RouteError
Why a set of routes was rejected.