2 use std::time::Duration;
4 use async_std::prelude::*;
5 use async_std::net::{ TcpListener, TcpStream };
7 use futures::stream::StreamExt;
11 env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info")).init();
13 // Listen for incoming TCP connections on localhost port 7878
14 let listener = TcpListener::bind("127.0.0.1:7878").await.unwrap();
16 listener.incoming().for_each_concurrent(/* limit */ None, |tcpstream| async move {
17 let tcpstream = tcpstream.unwrap();
18 task::spawn(handle_connection(tcpstream));
22 async fn handle_connection(mut stream: TcpStream) {
23 // Read the first 1024 bytes of data from the stream
24 let mut buffer = [0; 1024];
25 assert!(stream.read(&mut buffer).await.unwrap() > 0);
27 // Respond with greetings or a 404,
28 // depending on the data in the request
29 let (status_line, filename) = if buffer.starts_with(b"GET / HTTP/1.1\r\n") {
30 ("HTTP/1.1 200 OK", "index.html")
31 } else if buffer.starts_with(b"GET /sleep HTTP/1.1\r\n") {
32 task::sleep(Duration::from_secs(5)).await;
33 // sync version, to demonstrate concurrent async vs. parallel threads
34 // std::thread::sleep(Duration::from_secs(5));
35 ("HTTP/1.1 201 Sleep", "index.html")
37 ("HTTP/1.1 404 NOT FOUND", "404.html")
39 let contents = fs::read_to_string(filename).unwrap();
40 log::info!("GET {} {}", filename, status_line);
42 // Write response back to the stream,
43 // and flush the stream to ensure the response is sent back to the client
44 let response = format!("{status_line}\r\n\r\n{contents}");
45 stream.write_all(response.as_bytes()).await.unwrap();
46 stream.flush().await.unwrap();