Skip to main content

Crate torus_http

Crate torus_http 

Source
Expand description

§toy-rusttp

A small, synchronous http server library with a surprisingly pleasant developer experience and minimal moving parts.

§Example usage:

use torus_http::prelude::*;

fn main() {
    let server: HttpServer = HttpServer::new()
        .get("/", hello_world)
        .route(
            "/hello",
            HttpMethod::other("custom"),
            |_| "hello from a custom method",
        )
        .add_middleware(|req| {
            println!("got request: {req:#?}");
            req
        });
     
    server
        .listen(("127.0.0.1", 8080))
        .expect("Failed listening...");
}

pub fn hello_world(req: HttpRequest) -> impl Response {
    HttpResponse::new()
        .set_body(format!(
            "<h1>hey there from torus!</h1><p>this is a test, your request is: {req:#?}</p>",
        ))
        .insert_header("Content-Type", "text/html")
}

Modules§

method
Contains safe wrappers for http methods, note that the conversion is case insensitive so custom methods might not behave as expected
prelude
Re-export of the common things required for making a rudimentary http server
request
This module handles parsing the client’s request into a simple to work with data structure
response
Exports a trait for generating http responses, with a simple default implementation for string bodies.
server
The actual http server on which you define your routes
status
Http status wrapper