-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.rs
More file actions
56 lines (50 loc) · 1.57 KB
/
main.rs
File metadata and controls
56 lines (50 loc) · 1.57 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
use hyper::service::{make_service_fn, service_fn};
use hyper::{Body, Request, Response, Server};
use std::{convert::Infallible, net::SocketAddr};
async fn handle(req: Request<Body>) -> Result<Response<Body>, Infallible> {
let headers = req.headers();
println!("{:?}", headers);
let mut h = String::new();
h += r#"<!DOCTYPE html>
<html lang="en">
<head></head>
<body>"#;
//h += "<ul>\n";
//for (k, v) in headers {
// h += format!("<li>{}: {}</li>\n", k, v.to_str().unwrap()).as_str();
//}
let uri = req.uri().to_string();
let method = req.method().to_string();
h += format!(
r#"<table>
<tr><td>URI:<td><td>{}</td></tr>
<tr><td>Method:<td><td>{}</td></tr>
</table>
"#,
uri, method
)
.as_str();
h += "<p/><h2>Headers</h2><p/>\n";
h += r#"<table><tr><th>Name</th><th>Value</th></tr>"#;
for (k, v) in headers {
h += "<tr>\n";
let value = match v.to_str() {
Ok(s) => s,
Err(_) => "",
};
h += format!("<td>{}</td> <td>{}</td>\n", k, value).as_str();
h += "</tr>\n";
}
h += "</table>\n";
h += "</body>\n</html>";
Ok(Response::new(h.into()))
}
#[tokio::main]
async fn main() {
let addr = SocketAddr::from(([127, 0, 0, 1], 3001));
let make_svc = make_service_fn(|_conn| async { Ok::<_, Infallible>(service_fn(handle)) });
let server = Server::bind(&addr).serve(make_svc);
if let Err(e) = server.await {
eprintln!("server error: {}", e);
}
}