Hey all!
@ag_dubs and I have been working on a project, and today is its first release!
simple-server
is a very standard webserver implementation. In some ways, it's a continuation of chapter 20 of the book, in some ways, it's "hey how close can we make the API to nodejs's createServer
. In Node:
const http = require('http')
const port = 3000
const requestHandler = (request, response) => {
console.log(request.url)
response.end('Hello Node.js Server!')
}
const server = http.createServer(requestHandler)
server.listen(port, (err) => {
if (err) {
return console.log('something bad happened', err)
}
console.log(`server is listening on ${port}`)
})
With simple-server
:
#[macro_use]
extern crate log;
extern crate env_logger;
extern crate simple_server;
use simple_server::Server;
fn main() {
env_logger::init().unwrap();
let host = "127.0.0.1";
let port = "7878";
let server = Server::new(|request, mut response| {
info!("Request received. {} {}", request.method(), request.uri());
Ok(response.body("Hello Rust!".as_bytes())?)
});
server.listen(host, port);
}
Similar levels of complexity, but a bit more Rust-like.
We've built simple-server
on top of four crates: libstd
, http
, httparse
, and scoped-threadpool
. libstd
for the networking, httparse
to parse requests, http
for all of the proper http types, and scoped-threadpool
, as we're using blocking IO. Tokio is great, but we're going for utmost simplicity here, not maximum speed; you can reasonably read this crate and its dependencies and know what's going on.
A 0.1.0 release is up at https://crates.io/crates/simple-server, please kick the tires, open issues, etc!