A simple, multi-threaded HTTP web server built in Rust from scratch, featuring a custom thread pool implementation for handling concurrent connections.
- Multi-threaded Architecture: Custom thread pool implementation for efficient concurrent request handling
- Graceful Shutdown: Clean shutdown handling with Ctrl+C signal interception
- Simple Routing: Basic HTTP GET request routing with support for custom endpoints
- Static File Serving: Serves HTML content with proper HTTP headers
- Performance Testing: Includes a
/sleependpoint to simulate slow responses for load testing - Error Handling: Proper 404 responses for unmatched routes
- Rust (latest stable version recommended)
- Cargo (comes with Rust)
Clone the repository and build the project:
git clone https://github.com/kabir-fx/Native-rust-web-server.git
cd Native-rust-web-server
cargo build --releaseStart the server with:
cargo runThe server will start on http://127.0.0.1:7878 and display:
Server started. Press Ctrl+C to shutdown gracefully.
Once running, you can test the server with curl or a web browser:
# Root endpoint - returns hello message
curl http://127.0.0.1:7878/
# Sleep endpoint - simulates 5-second delay (good for testing concurrency)
curl http://127.0.0.1:7878/sleep
# Any other endpoint - returns 404 error
curl http://127.0.0.1:7878/unknown-pagePress Ctrl+C to gracefully shutdown the server. The server will:
- Stop accepting new connections
- Wait for all worker threads to complete their current tasks
- Display shutdown messages for each worker
- Exit cleanly
| Endpoint | Method | Description | Response |
|---|---|---|---|
/ |
GET | Root endpoint | Serves hello.html with welcome message |
/sleep |
GET | Slow response simulation | Serves hello.html after 5-second delay |
/* |
GET | Any other path | Serves 404.html with error message |
The server uses a custom thread pool (ThreadPool) for handling concurrent connections:
- Workers: Individual threads that execute tasks
- Channels: MPSC (Multi-Producer, Single-Consumer) channels for task distribution
- Jobs: Closures wrapped in
Box<dyn FnOnce() + Send + 'static>for thread-safe execution
- Manages a pool of worker threads
- Distributes incoming connections across available workers
- Handles graceful shutdown by consuming the sender channel
- Represents an individual thread in the pool
- Each worker has a unique ID and runs in a loop waiting for jobs
- Automatically shuts down when the channel is closed
- Non-blocking TCP listener for efficient polling
- Each connection is handled in a separate worker thread
- Proper HTTP/1.1 response formatting with Content-Length headers
- Listener: TCP listener accepts incoming connections
- Thread Pool: New connections are assigned to available worker threads
- Request Parsing: Worker reads HTTP request line
- Routing: Matches request against defined routes
- Response: Serves appropriate HTML file with HTTP headers
- Cleanup: Connection is closed after response
ctrlc: Cross-platform signal handling for graceful shutdown
src/
├── lib.rs # Thread pool implementation
└── main.rs # Server main loop and connection handling
Static files:
├── hello.html # Welcome page
└── 404.html # Error page
cargo buildcargo test# Check for issues
cargo check
# Format code
cargo fmt
# Lint code
cargo clippy- Thread Pool Size: Currently configured with 4 worker threads
- Non-blocking I/O: Server uses non-blocking TCP listener to avoid blocking on accept()
- Connection Handling: Each connection gets its own thread from the pool
- Memory Management: Efficient use of Arc and Mutex for shared state
This is a basic HTTP server implementation intended for learning purposes. For production use, consider:
- HTTPS/TLS support
- Request validation and sanitization
- Rate limiting
- Authentication and authorization
- Proper error handling and logging
- Security headers (CSP, HSTS, etc.)
This project demonstrates:
- Low-level TCP networking in Rust
- Multi-threading and concurrency patterns
- Custom data structure implementation (ThreadPool)
- HTTP protocol basics
- Graceful shutdown patterns
- Error handling in concurrent systems
- Cargo workspace organization
Feel free to submit issues and enhancement requests!
This project is open source and available under the MIT License.