January 2019
Intermediate to advanced
520 pages
14h 32m
English
Every handler in Gotham has to return the HandlerFuture implementation of a tuple that can be converted to HandlerFuture. Also, a handler has to accept a State parameter:
fn register_user_agent(state: State) -> Box<HandlerFuture> { // Implementation}
If you remember, we need to extract the User-Agent header from a request. We can do this using a State value, because we can borrow HeaderMap from a State with the borrow_from method call. It returns a map that we can use to get the User-Agent HTTP header by using the USER_AGENT key imported from the hyper crate:
let user_agent = HeaderMap::borrow_from(&state) .get(USER_AGENT) .map(|value| value.to_str().unwrap().to_string()) .unwrap_or_else(|| "<undefined>".into());
Read now
Unlock full access