๐ 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
BufReaderand routing based on request paths.
- Supports reading HTTP headers via
-
Static file serving
- Can serve
.htmlfiles directly from the filesystem.
- Can serve
-
Clean shutdown mechanism
- Implements graceful shutdown via message passing.
-
Zero dependencies
- Uses only what
stdoffers โ 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
tokiolater
๐ License
This project is licensed under the MIT License โ feel free to build on it, fork it, or turn it into something bigger.