A lightweight HTTP server built with Rust and Hyper for serving static web content. This server is designed to be simple, fast, and easy to configure, making it perfect for local development or simple web hosting needs (mainly for static websites).
- Static File Serving: Serves HTML, CSS, JavaScript, and other static assets
- MIME Type Detection: Automatically detects and sets appropriate content types
- Error Handling: Proper HTTP status codes for different error scenarios
- Asynchronous I/O: Built with Tokio for non-blocking file operations
- Zero Configuration: Ready to run out of the box
- Rust (1.56.0 or later)
- Cargo (comes with Rust)
-
Clone the repository:
git clone https://github.com/MaCh-nE/Rust-HTTP_WebServer.git cd Rust-HTTP_WebServer -
Build the project :
cargo build --release
- Create a
staticfolder in the project root directory - Add your web assets (HTML, CSS, JS files) to the
staticdirectory - Make sure you have an
index.htmlfile in the c directory - Reference all of your static assets with
/{static-subdirectory}/assetNamein theindex.html - Run the server:
cargo run --release
- Access your website at http://127.0.0.1:3000
project/
├── src/
│ └── main.rs # Server implementation
├── static/ # Static files directory
│ ├── index.html # Main HTML file (required and 1 minimum per subrepo in /static)
│ ├── index.css # CSS styles
│ ├── index.js # JavaScript code
│ └── ... # Other static assets
├── Cargo.toml # Rust dependencies and project metadata
└── README.md # This file
This project relies on the following Rust crates:
hyper: HTTP server implementationtokio: Asynchronous runtimemime_guess: MIME type detection- Other standard library components
The server handles HTTP requests with the following logic:
- When a request comes in, the server extracts the URI path
- For the root path (
/), it servesstatic/index.html - For other paths, it looks for corresponding files in the
staticdirectory - The server detects appropriate MIME types based on file extensions
- Content is served with proper HTTP headers and status codes
- Error handling provides appropriate responses for missing files or server issues
To use a different port, modify the SocketAddr in the main function:
let addr = SocketAddr::from(([127, 0, 0, 1], 3000)); // Change 3000 to your desired portTo serve files from a different directory, change the path prefix in the handle_request function:
let file_path = if path == "/" {
"./your_directory/index.html".to_string()
} else {
format!("./your_directory{}", path)
};Contributions are welcome! Please feel free to submit a Pull Request.