Skip to content

mateocabanal/tinyhttp

Repository files navigation

tinyhttp

codecov Rust Crates.io

Alt

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();
}

About

Tiny HTTP server written in Rust

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages