Crates.io | lil_http |
lib.rs | lil_http |
version | 0.1.2 |
source | src |
created_at | 2022-12-28 17:11:06.873635 |
updated_at | 2022-12-29 00:38:13.842305 |
description | A simple web framework with no external dependencies |
homepage | |
repository | https://github.com/m1guelpf/lil-http-rs |
max_upload_size | |
id | 746911 |
size | 67,088 |
A barebones HTTP 1.1 framework, built in Rust with no external dependencies (other than tokio).
use lil_http::{Body, Response, Server};
#[tokio::main]
async fn main() {
let mut http = Server::new().await.unwrap();
http.routes
.get("/", |request| {
println!("Received {} request to {}", request.method, request.path);
Response::text(
format!(
"Hello, {}!",
request.query.get("name").unwrap_or(&"World".to_string())
)
.as_str(),
)
})
.get("/api/user", |request| {
println!("Received {} request to {}", request.method, request.path);
Response::json(&serde_json::json!({
"name": "Miguel Piedrafita",
"age": 20,
}))
})
.post("/api/hello", |request| {
println!("Received {} request to {}", request.method, request.path);
let Body::Json(body) = request.body else {
return Response::invalid_request();
};
let Some(name) = body.get("name") else {
return Response::invalid_request();
};
Response::json(&serde_json::json!({
"message": format!("Hello, {name}!"),
}))
});
http.run().await;
}
This project is licensed under the MIT License - see the LICENSE file for details.