-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.rs
42 lines (35 loc) · 1.15 KB
/
main.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
use hyper::service::{make_service_fn, service_fn};
use hyper::{Body, Request, Response, Server, StatusCode};
use serde::Serialize;
use std::convert::Infallible;
#[derive(Serialize)]
struct VersionInfo {
version: String,
}
async fn handle_request(req: Request<Body>) -> Result<Response<Body>, Infallible> {
match req.uri().path() {
"/version" => {
let version_info = VersionInfo {
version: "1.0.0".to_string(),
};
let json = serde_json::to_string(&version_info).unwrap();
Ok(Response::new(Body::from(json)))
}
_ => {
let mut not_found = Response::new(Body::from("404 Not Found"));
*not_found.status_mut() = StatusCode::NOT_FOUND;
Ok(not_found)
}
}
}
#[tokio::main]
async fn main() {
let make_svc =
make_service_fn(|_conn| async { Ok::<_, Infallible>(service_fn(handle_request)) });
let addr = ([127, 0, 0, 1], 8000).into();
let server = Server::bind(&addr).serve(make_svc);
println!("listening on http://{}", addr);
if let Err(e) = server.await {
eprintln!("server error: {}", e);
}
}