Skip to content

Commit c933530

Browse files
committed
rust web server
1 parent 0048bc8 commit c933530

File tree

8 files changed

+119
-0
lines changed

8 files changed

+119
-0
lines changed

rust/hello/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/target

rust/hello/404.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Hello!</title>
6+
</head>
7+
<body>
8+
<h1>Oops!</h1>
9+
<p>Sorry, I don't know what you're asking for.</p>
10+
</body>
11+
</html>

rust/hello/Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/hello/Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[package]
2+
name = "hello"
3+
version = "0.1.0"
4+
edition = "2024"
5+
6+
[dependencies]

rust/hello/hello.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Hello!</title>
6+
</head>
7+
<body>
8+
<h1>Hello!</h1>
9+
<p>Hi from Rust</p>
10+
</body>
11+
</html>

rust/hello/rustfmt.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
edition = "2024"

rust/hello/src/lib.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
use std::{
2+
sync::{Arc, Mutex, mpsc},
3+
thread,
4+
};
5+
6+
pub struct ThreadPool {
7+
workers: Vec<Worker>,
8+
sender: mpsc::Sender<Job>,
9+
}
10+
11+
impl ThreadPool {
12+
pub fn new(size: usize) -> ThreadPool {
13+
assert!(size > 0);
14+
let (sender, receiver) = mpsc::channel();
15+
let receiver = Arc::new(Mutex::new(receiver));
16+
let mut workers = Vec::with_capacity(size);
17+
for id in 0..size {
18+
workers.push(Worker::new(id, Arc::clone(&receiver)));
19+
}
20+
ThreadPool { workers, sender }
21+
}
22+
pub fn execute<F>(&self, f: F)
23+
where
24+
F: FnOnce() + Send + 'static,
25+
{
26+
let job = Box::new(f);
27+
self.sender.send(job).unwrap();
28+
}
29+
}
30+
31+
struct Worker {
32+
id: usize,
33+
thread: thread::JoinHandle<()>,
34+
}
35+
36+
impl Worker {
37+
fn new(id: usize, receiver: Arc<Mutex<mpsc::Receiver<Job>>>) -> Worker {
38+
let thread = thread::spawn(move || {
39+
loop {
40+
let job = receiver.lock().unwrap().recv().unwrap();
41+
println!("Worker {id} got a job! Executing!");
42+
job();
43+
}
44+
});
45+
Worker { id, thread }
46+
}
47+
}
48+
49+
type Job = Box<dyn FnOnce() + Send + 'static>;

rust/hello/src/main.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
use hello::ThreadPool;
2+
use std::{
3+
fs,
4+
io::{BufReader, prelude::*},
5+
net::{TcpListener, TcpStream},
6+
thread,
7+
time::Duration,
8+
};
9+
fn main() {
10+
let listener = TcpListener::bind("127.0.0.1:7878").unwrap();
11+
let pool = ThreadPool::new(4);
12+
for stream in listener.incoming() {
13+
let stream = stream.unwrap();
14+
pool.execute(|| handle_connection(stream));
15+
}
16+
}
17+
18+
fn handle_connection(mut stream: TcpStream) {
19+
let buf_reader = BufReader::new(&stream);
20+
let request_line = buf_reader.lines().next().unwrap().unwrap();
21+
let (status_line, filename) = match &request_line[..] {
22+
"GET / HTTP/1.1" => ("HTTP/1.1 200 OK", "hello.html"),
23+
"GET /sleep HTTP/1.1" => {
24+
thread::sleep(Duration::from_secs(5));
25+
("HTTP/1.1 200 OK", "hello.html")
26+
}
27+
_ => ("HTTP/1.1 404 NOT FOUND", "404.html"),
28+
};
29+
let contents = fs::read_to_string(filename).unwrap();
30+
let length = contents.len();
31+
let response = format!("{status_line}\r\nContent-Length: {length}\r\n\r\n{contents}");
32+
stream.write_all(response.as_bytes()).unwrap();
33+
}

0 commit comments

Comments
 (0)