-
Notifications
You must be signed in to change notification settings - Fork 5.4k
/
hyper_hello.rs
39 lines (30 loc) · 1.1 KB
/
hyper_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
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
// Adapted from https://github.com/hyperium/hyper/blob/master/examples/hello.rs
#![deny(warnings)]
extern crate hyper;
use hyper::rt::{self, Future};
use hyper::service::service_fn_ok;
use hyper::{Body, Response, Server};
use std::env;
static PHRASE: &'static [u8] = b"Hello World!";
fn main() {
let mut port: u16 = 4544;
if let Some(custom_port) = env::args().nth(1) {
port = custom_port.parse::<u16>().unwrap();
}
let addr = ([127, 0, 0, 1], port).into();
// new_service is run for each connection, creating a 'service'
// to handle requests for that specific connection.
let new_service = || {
// This is the `Service` that will handle the connection.
// `service_fn_ok` is a helper to convert a function that
// returns a Response into a `Service`.
service_fn_ok(|_| Response::new(Body::from(PHRASE)))
};
let server = Server::bind(&addr)
.tcp_nodelay(true)
.serve(new_service)
.map_err(|e| eprintln!("server error: {}", e));
println!("Listening on http://{}", addr);
rt::run(server);
}