2 releases
0.1.1 | Feb 24, 2025 |
---|---|
0.1.0 | Feb 21, 2025 |
#1821 in Web programming
67 downloads per month
61KB
1.5K
SLoC
Small-Router Rust library
A simple and small router for the small-http library
Getting Started
A simple example the opens a http server on serves a simple response:
use std::net::{Ipv4Addr, TcpListener};
use small_http::{Request, Response, Status};
use small_router::RouterBuilder;
fn home(_req: &Request, _ctx: &()) -> Response {
Response::with_body("Home")
}
fn hello(req: &Request, _ctx: &()) -> Response {
Response::with_body(format!(
"Hello, {}!",
req.params.get("name").unwrap_or(&"World".to_string())
))
}
fn not_found(_req: &Request, _ctx: &()) -> Response {
Response::with_status(Status::NotFound).body("404 Not Found")
}
fn main() {
let router = RouterBuilder::new()
.get("/", home)
.get("/hello/:name", hello)
.fallback(not_found)
.build();
let listener = TcpListener::bind((Ipv4Addr::LOCALHOST, 8080))
.unwrap_or_else(|_| panic!("Can't bind to port"));
small_http::serve(listener, move |req| router.handle(req));
}
See the examples for many more examples.
Documentation
See the documentation for more information.
License
Copyright © 2024-2025 Bastiaan van der Plaat
Licensed under the MIT license.
Dependencies
~2–3MB
~57K SLoC