| Crates.io | slhs |
| lib.rs | slhs |
| version | 0.4.1 |
| created_at | 2025-10-14 16:26:32.052451+00 |
| updated_at | 2025-10-31 12:44:26.375258+00 |
| description | A simple http server |
| homepage | https://github.com/Polygons1/slhs |
| repository | https://github.com/Polygons1/slhs |
| max_upload_size | |
| id | 1882683 |
| size | 26,731 |
SLHS is a simple, no-frills HTTP server library written in Rust. It aims to provide a minimalistic yet functional base for building web services without the overhead of larger frameworks.
GET and POST endpoints with ease.gzip and brotli compression (requires feature flags).Add SLHS to your Cargo.toml with:
cargo add slhs
To enable compression, add the respective features with:
cargo add slhs --features "gzip,brotli"
This example sets up a server that listens on 127.0.0.1:8080 and responds to a GET request at the root path / and a POST request at /submit.
use slhs::*;
fn main() {
let mut router = Router::default();
router.get("/", |req, mut res| {
println!("Headers: {:?}", req.headers);
println!("URL Params: {:?}", req.url_params);
res.end("Hello from SLHS!")
});
router.post("/submit", |req, mut res| {
println!("Received POST request with body: {:?}", req.body);
res.status(201); // Created
res.end(format!("Thanks for the data: {:?}", req.body.unwrap_or_default()))
});
println!("Server listening on 127.0.0.1:8080");
router.start("127.0.0.1:8080");
}
This example demonstrates how to access URL query parameters and set custom response headers using the headers! macro.
use slhs::*;
fn main() {
let mut router = Router::default();
router.get("/greet", |req, mut res| {
let name = req.url_params.get("name").map(|s| s.as_str()).unwrap_or("Guest");
res.headers(headers!{
X-Powered-By => "SLHS",
Content-Type => "text/plain",
});
res.end(format!("Hello, {}!", name))
});
println!("Try navigating to http://127.0.0.1:8080/greet?name=Alice");
router.start("127.0.0.1:8080");
}
SLHS automatically handles 404 Not Found and 405 Method Not Allowed. You can explicitly set other statuses. If compression features are enabled and the client supports it via Accept-Encoding header, the response body will be compressed.
use slhs::*;
fn main() {
let mut router = Router::default();
router.get("/forbidden", |_, mut res| {
res.status(403); // Forbidden
res.end("You don't have permission to view this page.")
});
router.get("/teapot", |_, mut res| {
// This will be compressed if the client supports it and `brotli` or `gzip` features are enabled.
res.status(418); // I'm a teapot
res.end("I'm a little teapot, short and stout...")
});
println!("Server listening on 127.0.0.1:8080");
router.start("127.0.0.1:8080");
}
Feel free to open issues or submit pull requests!
This project is licensed under the MIT License.