2019-01-21 14:03:30 -05:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
2018-10-20 22:56:16 -04:00
|
|
|
// Adapted from https://github.com/hyperium/hyper/blob/master/examples/hello.rs
|
|
|
|
|
|
|
|
#![deny(warnings)]
|
|
|
|
extern crate hyper;
|
|
|
|
|
|
|
|
use hyper::rt::{self, Future};
|
2019-03-30 19:27:00 -04:00
|
|
|
use hyper::service::service_fn_ok;
|
|
|
|
use hyper::{Body, Response, Server};
|
|
|
|
use std::env;
|
2018-10-20 22:56:16 -04:00
|
|
|
|
|
|
|
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 = || {
|
2019-03-30 19:27:00 -04:00
|
|
|
// 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)))
|
2018-10-20 22:56:16 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
let server = Server::bind(&addr)
|
2019-04-06 09:56:30 -04:00
|
|
|
.tcp_nodelay(true)
|
2019-03-30 19:27:00 -04:00
|
|
|
.serve(new_service)
|
|
|
|
.map_err(|e| eprintln!("server error: {}", e));
|
2018-10-20 22:56:16 -04:00
|
|
|
|
|
|
|
println!("Listening on http://{}", addr);
|
|
|
|
|
|
|
|
rt::run(server);
|
|
|
|
}
|