| Crates.io | keiro |
| lib.rs | keiro |
| version | 0.0.2 |
| created_at | 2021-04-14 14:11:53.175343+00 |
| updated_at | 2021-06-13 14:03:41.555954+00 |
| description | A lightweight router for Rust HTTP services. |
| homepage | |
| repository | https://github.com/giraffate/keiro |
| max_upload_size | |
| id | 383977 |
| size | 47,308 |
Keiro is a lightweight router for Rust HTTP services. It is based on hyper.
use std::convert::Infallible;
use std::net::SocketAddr;
use hyper::{Body, Request, Response, Server};
use keiro::prelude::*;
use keiro::Router;
#[tokio::main]
async fn main() {
let mut router = Router::new();
router.get("/", index);
router.get("/hello/:user1/from/:user2", hello);
router.get("/hi/*path", hi);
let addr = SocketAddr::from(([0, 0, 0, 0], 8080));
Server::bind(&addr)
.serve(router.into_service())
.await
.unwrap();
}
async fn index(_req: Request<Body>) -> Result<Response<Body>, Infallible> {
Ok(Response::new(Body::from("Hello keiro!")))
}
async fn hello(req: Request<Body>) -> Result<Response<Body>, Infallible> {
let params = req.params().unwrap();
Ok(Response::new(Body::from(format!(
"Hello {} from {}!",
params.find("user1").unwrap(),
params.find("user2").unwrap(),
))))
}
async fn hi(req: Request<Body>) -> Result<Response<Body>, Infallible> {
let params = req.params().unwrap();
Ok(Response::new(Body::from(format!(
"Hello {}!",
params.find("path").unwrap(),
))))
}
cargo test command and confirm that it passescargo fmt and pass cargo clippy