Crates.io | tinyhttp |
lib.rs | tinyhttp |
version | 0.5.0 |
source | src |
created_at | 2022-05-24 19:58:13.028714 |
updated_at | 2024-11-07 21:16:23.736439 |
description | A HTTP library with SSL and GZIP support |
homepage | |
repository | https://github.com/mateocabanal/tinyhttp |
max_upload_size | |
id | 593034 |
size | 51,400 |
Speedy HTTP server built purely in Rust. Comes with built-in GZIP compression and HTTPS support.
Uses procedural macros for easy API building.
On a Raspberry Pi 4 with ethernet, tinyhttp is able to serve around 15000 requests per second
This was tested with go-wrk
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();
}