23 releases

new 0.5.0 Nov 7, 2024
0.4.0 Jul 4, 2024
0.4.0-rc5 Dec 7, 2023
0.4.0-rc3 Oct 14, 2023
0.2.7 Jul 1, 2022

#647 in HTTP server

Download history 7/week @ 2024-07-11 8/week @ 2024-07-25 12/week @ 2024-09-12 5/week @ 2024-09-19 6/week @ 2024-09-26 106/week @ 2024-10-03 4/week @ 2024-10-10

112 downloads per month

GPL-2.0-or-later

15KB
138 lines

HTTP SERVER

codecov Rust Crates.io

Alt

Live Demo

The "tinyhttp" crate contains none of the internal code due to the procedural macro crate depending on data types on the internal crate.

Speedy HTTP server built purely in Rust. Comes with built-in GZIP compression and HTTPS support.

Uses procedural macros for easy API building.

Performance

On a Raspberry Pi 4 with ethernet, tinyhttp is able to serve around 15000 requests per second

This was tested with go-wrk

Examples

Example :

use std::net::TcpListener;
use tinyhttp::prelude::*;

#[get("/")]
fn get() -> &'static str {
  "Hello, World!"
}

#[post("/")]
fn post() -> &'static str {
  "Hi, there!"
}

// Example 1: Can return anything that implements Into<Vec<u8>>
#[get("/ex1")]
fn ex1_get() -> &'static str {
  "Hello World!"
}

// Example 2: same as example 1, but takes a Request as an argument
#[get("/ex2")]
fn ex2_get(req: Request) -> String {
    let accept_header = req.get_headers().get("accept").unwrap();
    format!("accept header: {}", accept_header)
}

// Example 3: takes a Request as an argument and returns a Response for more control
#[get("/ex3")]
fn ex3_get(req: Request) -> Response {
    Response::new()
        .status_line("HTTP/1.1 200 OK\r\n")
        .mime("text/plain")
        .body(b"Hello from response!\r\n".to_vec())
}

fn main() {
  let socket = TcpListener::bind(":::9001").unwrap();
  let routes = Routes::new(vec![get(), post(), ex1_get(), ex2_get(), ex3_get()]);
  let config = Config::new().routes(routes);
  let http = HttpListener::new(socket, config);

  http.start();
}

Dependencies

~2–11MB
~126K SLoC