https://github.com/d-holguin/rawrustserver
A multi-threaded HTTP server built using only the Rust standard library for practice and learning.
https://github.com/d-holguin/rawrustserver
html http multithreading rust
Last synced: 12 months ago
JSON representation
A multi-threaded HTTP server built using only the Rust standard library for practice and learning.
- Host: GitHub
- URL: https://github.com/d-holguin/rawrustserver
- Owner: d-holguin
- License: apache-2.0
- Created: 2023-07-26T14:52:03.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2025-01-12T22:12:20.000Z (about 1 year ago)
- Last Synced: 2025-02-16T15:36:54.571Z (about 1 year ago)
- Topics: html, http, multithreading, rust
- Language: Rust
- Homepage:
- Size: 44.2 MB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# RawRustServer
`RawRustServer` is a simple multithreaded web server built in Rust for fun and self-learning. It originally expanded on the Rust book's Multi-Threaded Web Server project, which you can find here: https://doc.rust-lang.org/book/ch20-00-final-project-a-web-server.html
This only uses the rust standard library which doesn't include a http server, logger, threadpool, etc.
## Usage

```rust
fn run_server() -> Result<()> {
let database = Arc::new(Database::database_init()?);
let router = Router::new()
.add_route(
HttpMethod::get("/home"),
HomeHandler {
database: Arc::clone(&database),
},
)
.add_route(HttpMethod::get("/styles.css"), CssHandler)
.add_route(HttpMethod::get("/favicon.ico"), FaviconHandler)
.add_route(
HttpMethod::post("/login"),
PostLoginHandler {
database: Arc::clone(&database),
},
)
.add_route(HttpMethod::get("/images/*"), GetImageHandler)
.add_route(HttpMethod::get("/login"), GetLoginHandler);
let server = ServerBuilder::new()
.address("127.0.0.1:8000")
.thread_count(4)
.router(router)
.build()?;
server.run()
}
``````
## Logger Utility
The `Logger Utility` provides a simple and efficient way to log messages both to the terminal and to a file. The logger supports different log levels such as INFO and ERROR.
When logging an `INFO` message, it will appear in the terminal as:
[INFO] Starting Server...
To use the logger, first initialize the global logger backend with the desired log file name:
```rust
logger_backend::init_global_logger("logs/server.log");
```
Then you can use the logger anywhere in code like this:
```
logger::info("Starting Server...");
```