Skip to content

Commit

Permalink
doc: update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
shellfly committed Jan 29, 2023
1 parent a01afc8 commit 39fbfb3
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 6 deletions.
26 changes: 20 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,21 @@ cargo add haro
Then, on your main.rs:

```Rust
use haro::{Application, Request, Response};
use haro::{Application, Request, Response, Handler};
use serde_json::json;

fn main() {
let mut app = Application::new("0:8000");
app.route("/", |_|Response::str("Hello Haro"));
app.route("/hello/:name", hello);
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 hello(req: Request) -> Response {
fn input(req: Request) -> Response {
let data = json!({
"method":req.method(),
"args":req.args,
Expand All @@ -44,6 +48,16 @@ fn hello(req: Request) -> Response {
});
Response::json(data)
}

struct HelloHandler {
name: String,
}

impl Handler for HelloHandler {
fn call(&self, _: Request) -> Response {
Response::str(format!("hello {}", self.name))
}
}
```

```bash
Expand All @@ -56,7 +70,7 @@ Hello Haro
```

```bash
http post "localhost:8080/hello/world?a=b" c=d
http post "localhost:8080/input/world?a=b" c=d
HTTP/1.1 200 OK
content-length: 77
content-type: application/json
Expand Down
33 changes: 33 additions & 0 deletions examples/readme/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use haro::{Application, Handler, Request, Response};
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))
}
}

0 comments on commit 39fbfb3

Please sign in to comment.