tinyhttp

Crates.iotinyhttp
lib.rstinyhttp
version0.5.0
sourcesrc
created_at2022-05-24 19:58:13.028714
updated_at2024-11-07 21:16:23.736439
descriptionA HTTP library with SSL and GZIP support
homepage
repositoryhttps://github.com/mateocabanal/tinyhttp
max_upload_size
id593034
size51,400
Mateo Cabanal (mateocabanal)

documentation

README

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();
}
Commit count: 261

cargo fmt