Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple HTTP server example for Rust
// Updated example from http://rosettacode.org/wiki/Hello_world/Web_server#Rust
// to work with Rust 1.0 beta
use std::net::{TcpStream, TcpListener};
use std::io::{Read, Write};
use std::thread;
fn handle_read(mut stream: &TcpStream) {
let mut buf = [0u8 ;4096];
match stream.read(&mut buf) {
Ok(_) => {
let req_str = String::from_utf8_lossy(&buf);
println!("{}", req_str);
},
Err(e) => println!("Unable to read stream: {}", e),
}
}
fn handle_write(mut stream: TcpStream) {
let response = b"HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=UTF-8\r\n\r\n<html><body>Hello world</body></html>\r\n";
match stream.write(response) {
Ok(_) => println!("Response sent"),
Err(e) => println!("Failed sending response: {}", e),
}
}
fn handle_client(stream: TcpStream) {
handle_read(&stream);
handle_write(stream);
}
fn main() {
let listener = TcpListener::bind("127.0.0.1:8080").unwrap();
println!("Listening for connections on port {}", 8080);
for stream in listener.incoming() {
match stream {
Ok(stream) => {
thread::spawn(|| {
handle_client(stream)
});
}
Err(e) => {
println!("Unable to connect: {}", e);
}
}
}
}
@lausek

This comment has been minimized.

Copy link

@lausek lausek commented Mar 13, 2018

@dalu Therefore, they are two separate projects

@chussenot

This comment has been minimized.

Copy link

@chussenot chussenot commented Apr 22, 2018

Cheers ! Nice job :)

@mk-tool

This comment has been minimized.

Copy link

@mk-tool mk-tool commented Apr 24, 2018

Why don't you need shutdown of connection ?

@magichim

This comment has been minimized.

Copy link

@magichim magichim commented Jun 17, 2018

so cool!

@sdedalus

This comment has been minimized.

Copy link

@sdedalus sdedalus commented Aug 1, 2018

@dalu Go is a language pretty much designed for web services so it's not surprising that it would take less code. If a language was written around making flappy bird apps it's fans would be pointing out that it takes too many lines of code to get a bird flapping in Golang.

@ReenExe

This comment has been minimized.

Copy link

@ReenExe ReenExe commented Aug 9, 2018

extern crate iron;
extern crate staticfile;
extern crate mount;

fn main() {
    let mut mount = mount::Mount::new();

    mount.mount("/", staticfile::Static::new(std::path::Path::new("public/")));

    iron::Iron::new(mount).http("127.0.0.1:3000").unwrap();
}
@thejainlabs

This comment has been minimized.

Copy link

@thejainlabs thejainlabs commented Jul 17, 2019

how can i serve my html pages with the help of this server?
i did put my html folder in the same directory and typed "http://127.0.0.1:8080/HTML/index.html" but the page did not load.

Thank you

@ndelvalle

This comment has been minimized.

Copy link

@ndelvalle ndelvalle commented Dec 24, 2019

How can I cleanly shutdown this server TcpListener?

@cezaryB

This comment has been minimized.

Copy link

@cezaryB cezaryB commented Apr 12, 2020

Really nice example, thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment