Crates.io | haro |
lib.rs | haro |
version | 0.3.0 |
source | src |
created_at | 2023-01-26 07:55:07.919638 |
updated_at | 2023-01-29 03:36:11.28889 |
description | A simple and synchronous web framework written in and for Rust |
homepage | |
repository | https://github.com/shellfly/haro/ |
max_upload_size | |
id | 768298 |
size | 122,761 |
Haro is a simple and synchronous web framework written in and for Rust.
The project was named after the Haro character. The application interface was inspired by the web.py project.
In short, async Rust is more difficult to use and can result in a higher maintenance burden than synchronous Rust, but gives you best-in-class performance in return. All areas of async Rust are constantly improving, so the impact of these issues will wear off over time
https://rust-lang.github.io/async-book/01_getting_started/03_state_of_async_rust.html
As the async book says, async Rust is not mature yet. While bringing performance, it also results in a higher maintenance burden. The goal of this project is to create a simple and minimum synchronous Web framework for Rust.
Add haro
as a dependency by cargo
cargo add haro
Then, on your main.rs:
use haro::{Application, Request, Response, Handler};
use serde_json::json;
fn main() {
let mut app = Application::new("0:8080");
let hello_handler = HelloHandler {
name: "Haro".to_string(),
};
app.route("/", |_| Response::str("Hello Haro")); // route by closure
app.route("/input/:name", input); // route by function
app.route_handler("/hello", hello_handler); //route by `Handler` trait type
app.run();
}
fn input(req: Request) -> Response {
let data = json!({
"method":req.method(),
"args":req.args,
"params":req.params,
"data":req.data,
});
Response::json(data)
}
struct HelloHandler {
name: String,
}
impl Handler for HelloHandler {
fn call(&self, _: Request) -> Response {
Response::str(format!("hello {}", self.name))
}
}
http get "localhost:8080/"
HTTP/1.1 200 OK
content-length: 12
content-type: text/plain
Hello Haro
http post "localhost:8080/input/world?a=b" c=d
HTTP/1.1 200 OK
content-length: 77
content-type: application/json
{
"args": {
"a": "b"
},
"data": {
"c": "d"
},
"method": "POST",
"params": {
"name": "world"
}
}
The repo contains more examples that show how to put all the pieces together.
URL Routing with function/closure/trait type
Request & Response with minimal boilerplate
Middleware
Template (Optional)
Database (Optional)
Tests
HTTP2
Licensed under either of
at your option.