Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/m1guelpf/lil-http-rs
A Rust web framework with no external dependencies
https://github.com/m1guelpf/lil-http-rs
framework http rust
Last synced: 13 days ago
JSON representation
A Rust web framework with no external dependencies
- Host: GitHub
- URL: https://github.com/m1guelpf/lil-http-rs
- Owner: m1guelpf
- License: mit
- Created: 2022-12-28T09:28:28.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2022-12-29T00:38:09.000Z (almost 2 years ago)
- Last Synced: 2024-10-25T05:57:35.984Z (21 days ago)
- Topics: framework, http, rust
- Language: Rust
- Homepage: https://docs.rs/lil_http
- Size: 35.2 KB
- Stars: 27
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# lil-http
A barebones HTTP 1.1 framework, built in Rust with no external dependencies (other than tokio).
## Features
- [x] Listening to incoming requests
- [x] Parsing method, path, query, headers, and body according to the HTTP 1.1 spec
- [x] Responding to requests with an arbitrary body and headers
- [x] Helpers for responding with text or JSON
- [x] Allow defining routes and methods as closures
- [x] Appropiately routing the request to its function, or 404'ing otherwise
- [x] Appropiately crafting and returning 405 errors on invalid methods.## Usage
```rust
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;
}
```## License
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.