18 releases
0.8.3 | Jan 23, 2020 |
---|---|
0.7.0 | Sep 16, 2019 |
0.6.4 | Dec 29, 2018 |
0.6.3 | Nov 21, 2018 |
0.3.0 | Nov 1, 2017 |
#1352 in HTTP server
71 downloads per month
31KB
587 lines
reset-router
A fast RegexSet
based router for use with async Hyper (0.13).
Individual handler functions should have the type H
, where
H: Fn(Request) -> F,
F: Future<Output = Result<S, E>> + Send,
S: Into<Response>,
E: Into<Response>,
You can return something as simple as Ok(Response::new("hello world".into()))
. You don't have to worry about futures
unless you need to read the request body or interact with other future-aware things.
Usage:
use reset_router::{Request, RequestExtensions, Response, Router, SharedService};
use std::sync::Arc;
pub struct Handler(Arc<String>);
impl SharedService for Handler {
type Response = Response;
type Error = Box<dyn std::error::Error + Send + Sync + 'static>;
type Future = futures::future::Ready<Result<Self::Response, Self::Error>>;
fn call(&self, _: Request) -> Self::Future {
futures::future::ready(Ok(http::Response::builder()
.status(200)
.body(format!("Hello, {}!", &self.0).into())
.unwrap()))
}
}
#[derive(Clone)]
pub struct State(pub i32);
async fn hello(req: Request) -> Result<Response, Response> {
let (first_name, last_name) = req.parsed_captures::<(String, String)>()?;
Ok(http::Response::builder()
.status(200)
.body(format!("Hello, {} {}!", first_name, last_name).into())
.unwrap())
}
async fn add(req: Request) -> Result<Response, Response> {
let (add1, add2) = req.parsed_captures::<(i32, i32)>()?;
let state_num: i32 = req.data::<State>().map(|x| x.0).unwrap_or(0);
Ok(http::Response::builder()
.status(200)
.body(
format!("{} + {} + {} = {}\r\n", add1, add2, state_num, add1 + add2 + state_num).into(),
)
.unwrap())
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let router = Router::build()
.data(State(42))
.add(http::Method::POST, r"^/hello/([^/]+)/(.+)$", hello)
.add(http::Method::GET, r"^/hello/([^/]+)/(.+)$", hello)
.add(http::Method::GET, r"^/add/([\d]+)/([\d]+)$", add)
.add(http::Method::GET, r"^/other$", Handler(Arc::new(String::from("world"))))
.add_not_found(|_| {
async {
Ok::<_, Response>(http::Response::builder().status(404).body("404".into()).unwrap())
}
})
.finish()?;
let addr = "0.0.0.0:3000".parse()?;
let server = hyper::Server::bind(&addr).serve(router);
server.await?;
Ok(())
}
Current version: 0.8.1
License: MIT
Dependencies
~14MB
~241K SLoC