Crates.io | http_lolo |
lib.rs | http_lolo |
version | 0.1.0 |
source | src |
created_at | 2024-07-28 00:28:33.298138 |
updated_at | 2024-07-28 00:28:33.298138 |
description | A simple, lightweight, single-threaded HTTP server with non-blocking I/O for concurrent operation handling |
homepage | |
repository | https://github.com/mohaskii/http_lolo |
max_upload_size | |
id | 1317624 |
size | 33,185 |
A simple, lightweight, single-threaded HTTP server with non-blocking I/O for concurrent operation handling.
Add this to your Cargo.toml
:
[dependencies]
http_lolo = "0.1.0"
use http_lolo::{HttpServer, Request, ResponseWriter};
fn main() {
let server = HttpServer::new();
server.handle_route("/", Box::new(|_: &mut Request, w: &mut ResponseWriter| {
w.write_string("Hello, World!".to_string());
}));
server.listen_on("127.0.0.1:8080");
HttpServer::run_all();
}
let server = HttpServer::new();
server.handle_route("/", Box::new(|_: &mut Request, w: &mut ResponseWriter| {
w.write_string("Hello, World!".to_string());
}));
server.listen_on("127.0.0.1:8080");
HttpServer::run_all();
Inside your route handler, you can access request data and write responses:
|req: &mut Request, resp: &mut ResponseWriter| {
// Access request data
println!("Method: {}", req.method);
println!("Path: {}", req.path);
// Write response
resp.set_status(200);
resp.write_string("Response content".to_string());
}
You can create multiple server instances listening on different ports:
let server1 = HttpServer::new();
let server2 = HttpServer::new();
// Add route handlers to server1 and server2
server1.listen_on("127.0.0.1:8080");
server2.listen_on("127.0.0.1:8081");
HttpServer::run_all();
use http_lolo::JsonValue;
resp.write_json(json::object!{
"key" => "value",
"number" => 42
});
Contributions are welcome! Please feel free to submit a Pull Request.