1 unstable release

new 0.1.0 Nov 26, 2024

#341 in HTTP server

Download history 46/week @ 2024-11-20

56 downloads per month

MIT license

105KB
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.
  • 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::{Server, Response, Status, respond};

// Start the server
let server = Server::listen("localhost:0", 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, Router, Response, Status, respond};

let mut router = Router::new();
router.register("/hello/{name}", respond(|req| {
    let name = &req.path_values["name"];
    Response::from_bytes(Status::Ok, "text/plain", format!("Hello, {name}!"))
}));

let server = Server::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
~94K SLoC