From 0319c8f531139c2b4c49570a499894d7200760f5 Mon Sep 17 00:00:00 2001 From: Bryan Newbold Date: Tue, 15 May 2018 10:34:16 -0700 Subject: implement HTTP header middleware getting more familar with iron --- rust/src/bin/fatcat-iron.rs | 8 ++++---- rust/src/lib.rs | 21 ++++++++++++++++++--- 2 files changed, 22 insertions(+), 7 deletions(-) (limited to 'rust/src') diff --git a/rust/src/bin/fatcat-iron.rs b/rust/src/bin/fatcat-iron.rs index eed98b1d..0bed0fa2 100644 --- a/rust/src/bin/fatcat-iron.rs +++ b/rust/src/bin/fatcat-iron.rs @@ -8,10 +8,8 @@ extern crate futures; extern crate iron; extern crate iron_slog; extern crate swagger; -#[macro_use] -extern crate error_chain; -#[macro_use] -extern crate slog; +#[macro_use] extern crate error_chain; +#[macro_use] extern crate slog; extern crate slog_term; extern crate slog_async; @@ -50,6 +48,8 @@ fn main() { // for the purpose of this example, pretend we have authenticated a user chain.link_before(AllowAllMiddleware::new("cosmo")); + chain.link_after(fatcat::XClacksOverheadMiddleware); + if matches.is_present("https") { unimplemented!() } else { diff --git a/rust/src/lib.rs b/rust/src/lib.rs index fbff6ab2..55040f0c 100644 --- a/rust/src/lib.rs +++ b/rust/src/lib.rs @@ -4,10 +4,10 @@ extern crate chrono; extern crate diesel; extern crate dotenv; extern crate futures; -extern crate hyper; +#[macro_use] extern crate hyper; extern crate swagger; -#[macro_use] -extern crate error_chain; +#[macro_use] extern crate error_chain; +extern crate iron; pub mod api_server; @@ -20,6 +20,9 @@ use diesel::pg::PgConnection; use diesel::prelude::*; use dotenv::dotenv; use std::env; +use hyper::header::Headers; +use iron::{Request, Response}; +use iron::middleware::AfterMiddleware; pub fn establish_connection() -> PgConnection { dotenv().ok(); @@ -32,3 +35,15 @@ pub fn establish_connection() -> PgConnection { pub fn server() -> Result { Ok(api_server::Server {}) } + +/// HTTP header middleware +header! { (XClacksOverhead, "X-Clacks-Overhead") => [String] } + +pub struct XClacksOverheadMiddleware; + +impl AfterMiddleware for XClacksOverheadMiddleware { + fn after(&self, _req: &mut Request, mut res: Response) -> iron::IronResult { + res.headers.set(XClacksOverhead("GNU aaronsw, jpb".to_owned())); + Ok(res) + } +} -- cgit v1.2.3