π std web_server
Minimal yet functional multi-threaded web server implemented entirely using the Rust standard library.
No external dependencies β just pure std
.
This project demonstrates how to build a basic but concurrent HTTP server by leveraging only the Rust Standard Library.
It serves as a deep dive into Rustβs core capabilities around file I/O
, networking
, threading
, and concurrency
.
An excellent hands-on learning example for:
- Systems programmers
- Backend developers exploring Rust
- Students building web server fundamentals
π Tech Stack β std Only
The server is powered by a thread pool and handles each TCP connection asynchronously using:
- π
fs
β for serving static HTML files - π
net::{TcpListener, TcpStream}
β for socket connections - π§΅
thread
β to spawn workers - π¦
sync::{mpsc, Arc, Mutex}
β for shared message passing - π
time::Duration
β for simulated delays (e.g., thread sleeping) - π
io::{BufReader, prelude::*}
β to read and parse incoming streams
β¨ Features
-
Multi-threaded request handling
- Built-in thread pool to handle requests concurrently.
-
Graceful connection handling
- Supports reading HTTP headers via
BufReader
and routing based on request paths.
- Supports reading HTTP headers via
-
Static file serving
- Can serve
.html
files directly from the filesystem.
- Can serve
-
Clean shutdown mechanism
- Implements graceful shutdown via message passing.
-
Zero dependencies
- Uses only what
std
offers β perfect for understanding how things work under the hood.
- Uses only what
π Project Structure
main.rs
β entry point, sets up listener and thread poollib.rs
β contains thread pool implementationstatic/
β directory for HTML files (index.html, 404.html, etc.)
π§ͺ Example Usage
cargo run
Then open your browser and go to:
http://localhost:7878
To simulate a delayed response:
- Access route
/sleep
β one thread will sleep for 5 seconds.
π§ Educational Value
This project mirrors the concepts from "The Rust Programming Language" book (Chapter 20), but it goes further with:
- Better thread pool abstraction
- Error handling improvements
- Project-ready formatting
Use it to:
- Learn how a basic HTTP server works
- Build custom functionality (routes, MIME types, logging, etc.)
- Extend toward async runtime by comparing with
tokio
later
π License
This project is licensed under the MIT License β feel free to build on it, fork it, or turn it into something bigger.