Crates.io | httplite |
lib.rs | httplite |
version | 0.1.3 |
source | src |
created_at | 2024-06-11 10:51:54.653705 |
updated_at | 2024-06-24 23:03:54.037895 |
description | A super lightweight HTTP server written in Rust, made to resemble the functionality of the "NET/HTTP" module in Go. |
homepage | |
repository | https://github.com/zeuzmakessoftware/httplite |
max_upload_size | |
id | 1268079 |
size | 19,032 |
A super lightweight HTTP server written in Rust, made to resemble the functionality of the "NET/HTTP" module in Go. Still in early access and working on docs.
Include this is your Cargo.toml in your project.
[dependencies] httplite = "0.1.3"
Hello world example:
use httplite::{Httplite, ResponseWriter, Request};
fn main() {
println!("Server is running at http://localhost:8080");
let port = Httplite::new(":8080");
port.add_route("/hello", hello_server);
port.listen().unwrap();
}
fn hello_server(mut w: ResponseWriter, r: Request) {
let response_text = format!("Hello, {}", r.url().trim_start_matches("/hello/"));
w.print_text(&response_text).unwrap();
}
Hosting JSON example:
use httplite::{Httplite, ResponseWriter, Request};
use std::collections::HashMap;
fn main() {
println!("Server is running at http://localhost:8080");
let port = Httplite::new(":8080");
port.add_route("/json", json_server);
port.listen().unwrap();
}
fn json_server(mut w: ResponseWriter, _r: Request) {
let mut map = HashMap::new();
map.insert("JSON Thing", "This is a thing");
map.insert("Another JSON Thing", "This is another thing");
w.print_hashmap_to_json(&map).unwrap();
}
0.1.3 Added the ability to host hashmaps as serialized json on an endpoint.