-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
http_listener_hello.rs
42 lines (40 loc) · 1.1 KB
/
http_listener_hello.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//! An example to showcase how to build directly a HTTP server that listens on `127.0.0.1:62007`
//! and returns a JSON response with the method and path of the request.
//!
//! # Run the example
//!
//! ```sh
//! cargo run --example http_listener_hello --features=http-full
//! ```
//!
//! # Expected output
//!
//! The server will start and listen on `:62007`. You can use `curl` to interact with the service:
//!
//! ```sh
//! curl -v http://127.0.0.1:62007
//! ```
//!
//! You should see a response with `HTTP/1.1 200 OK` and a JSON body with the method and path of the request.
use rama::{
http::{response::Json, server::HttpServer, Request},
rt::Executor,
service::service_fn,
};
use serde_json::json;
#[tokio::main]
async fn main() {
let exec = Executor::default();
HttpServer::auto(exec)
.listen(
"127.0.0.1:62007",
service_fn(|req: Request| async move {
Ok(Json(json!({
"method": req.method().as_str(),
"path": req.uri().path(),
})))
}),
)
.await
.unwrap();
}