2 unstable releases
0.2.0 | Aug 26, 2020 |
---|---|
0.1.0 | Aug 23, 2020 |
#30 in #http-router
13KB
252 lines
grout
A dead simple hash-based HTTP router built on hyper.
use grout::{path, Body, Method, Request, Response, ResponseBuilder, RouterBuilder, Server};
async fn handler(params: Vec<String>, _req: Request) -> Response {
Ok(ResponseBuilder::default().body(Body::empty())?)
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let addr = ([127, 0, 0, 1], 3000).into();
let router = RouterBuilder::default()
.register(Method::GET, path![], handler)
.register(Method::GET, path![foo / _], handler)
.register(Method::POST, path![foo / _], handler)
.build();
let server = Server::bind(&addr).serve(router);
println!("Listening on http://{}", addr);
server.await?;
Ok(())
}
Features
- HTTP method routing
- Route parameters (ordered, not keyed)
- Simple API
See the examples folder for example usage.
Limitations
- No state passing or any form of middleware
- I recommend the
state
crate to inject outside structs into your route handlers
- I recommend the
- No complex route matching
- Perform complex validation in your route handlers
Dependencies
~9.5MB
~157K SLoC