2 unstable releases
new 0.2.0 | Nov 30, 2024 |
---|---|
0.1.0 | Nov 26, 2024 |
#283 in HTTP server
127 downloads per month
115KB
2K
SLoC
claro
⚠️ Work in progress ...
A simple, predictable HTTP/1.1 server
This library will be interesting to you if any of the following describe you:
- You need a simple http server for your static site generator.
- See also:
file-serve
- See also:
- You are going to be putting your service behind a reverse proxy, so it does not need to speak anythinmg other than http/1.1
- You despair at the lack of maintained non-async http servers in the Rust ecosystem.
Turn around if:
- You expect that what your are building will need to maintain a "high" number of concurrent connections (what "high" here means is TBD).
lib.rs
:
This crate provides a lightweight mini-framework for writing HTTP/1.1 servers.
The Handler
trait is the protagonist of this library.
It represents anything that can respond to an http Request
with an http Response
.
There is not separate concept for "middleware"; everything that needs to do something with the request is a handler (even the path router implements the trait).
Usage
The obligatory "hello world":
use claro::{handler, Response, Status};
// Start the server
let server = claro::listen("localhost:0", handler::respond(|_| {
Response::from_bytes(Status::Ok, "text/plain", "Hello, world!")
})).unwrap();
// Verify that it works
let response = ureq::get(&format!("http://{}", server.address))
.call()
.unwrap()
.into_string()
.unwrap();
assert_eq!(response, "Hello, world!");
server.stop();
An echo server:
use claro::{Server, Response, Status, handler};
let mut router = handler::Router::new();
router.register("/hello/{name}", handler::respond(|req| {
let name = &req.path_values["name"];
Response::from_bytes(Status::Ok, "text/plain", format!("Hello, {name}!"))
}));
let server = claro::listen("localhost:0", router).unwrap();
// Verify that it works
let response = ureq::get(&format!("http://{}/hello/friends", server.address))
.call()
.unwrap()
.into_string()
.unwrap();
assert_eq!(response, "Hello, friends!");
Limitations
- No support for TLS/HTTPS
Dependencies
~4–10MB
~93K SLoC