O'Reilly logo

Mastering Rust by Vesa Kaihlavirta

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Hyper as a server

Let's go to the other aspect of Hyper: serving HTTP content. The basic usage is simple: you define a listening address and attach a handler to it. Since it may be called from several threads by Hyper, the handler that it needs to implement the Sync trait. Hyper provides a simple implementation for functions and closures, which takes a parameter for the Request and a Response. So, a simple logging HTTP server could look like this:

extern crate hyper; extern crate chrono; use hyper::server::{Server, Request, Response}; use hyper::header::UserAgent; use hyper::status::StatusCode; use chrono::*; fn log_request(req: Request, mut res: Response) {     let date = UTC::now();  let user_agent = req.headers.get::<UserAgent>().unwrap(); ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required