A lightweight asynchronous HTTP server library in Rust. Provides:
- TCP-based HTTP parsing
- Async request handling with
tokio - Builder-style server and router API
- Easy header and body access for handlers
- Minimal, no heavy dependencies besides
tokio - Async-first design - handlers are
async fns - Incremental HTTP parsing for efficiency
- Builder pattern for configurating routes
- Easily integrated into other projects as a library
[dependencies]
rust_http = { git = "https://github.com/danielgrotan/rust-http.git" }
tokio = { version = "1", features = ["full"] }use rust_http::{Request, Response, Server};
#[tokio::main]
async fn main() {
let mut server = Server::bind("127.0.0.1:3000")
.get("/hello", handle_hello)
.build();
server.run().await;
}
async fn handle_hello(req: Request) -> Response {
Response::ok(
req.headers
.get("user-agent")
.map(|user_agent| format!("Hello {}!", user_agent))
.unwrap_or_else(|| "Who are you? Give me your user agent ;(".to_string()),
)
}Server::bind(addr)-> returns aServerBuilder.get(path, handler)-> registers a GET route with an async handler.post(path, handler)-> registers a POST route with an async handler.build()-> builds the server.run().await-> starts listening and serving requests
request_line: HTTP method, path, and versionheaders: header map with helpersget/get_orbody: optional body asVec<u8>
Response::ok(body)-> create a 200 OK response.with_header(key, value)-> add HTTP headers.as_bytes()-> convert the response to raw bytes for writing to TCP