docs/code-samples/community/serving/helloworld-rust/src/main.rs

47 lines
1.2 KiB
Rust

use hyper::{
server::conn::AddrStream,
service::{make_service_fn, service_fn},
Body, Request, Response, Server,
};
use std::convert::Infallible;
use std::env;
#[tokio::main]
async fn main() {
pretty_env_logger::init();
let mut port: u16 = 8080;
match env::var("PORT") {
Ok(p) => {
match p.parse::<u16>() {
Ok(n) => {
port = n;
}
Err(_e) => {}
};
}
Err(_e) => {}
};
let addr = ([0, 0, 0, 0], port).into();
let make_svc = make_service_fn(|_socket: &AddrStream| async move {
Ok::<_, Infallible>(service_fn(move |_: Request<Body>| async move {
let mut hello = "Hello ".to_string();
match env::var("TARGET") {
Ok(target) => {
hello.push_str(&target);
}
Err(_e) => hello.push_str("World"),
};
Ok::<_, Infallible>(Response::new(Body::from(hello)))
}))
});
let server = Server::bind(&addr).serve(make_svc);
println!("Listening on http://{}", addr);
if let Err(e) = server.await {
eprintln!("server error: {}", e);
}
}