-
Notifications
You must be signed in to change notification settings - Fork 324
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #42 from tzilist/feat-default-headers
Feature: Default Headers Middleware
- Loading branch information
Showing
4 changed files
with
76 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#![feature(async_await)] | ||
|
||
use tide::middleware::DefaultHeaders; | ||
|
||
fn main() { | ||
let mut app = tide::App::new(()); | ||
|
||
app.middleware( | ||
DefaultHeaders::new() | ||
.header("X-Version", "1.0.0") | ||
.header("X-Servier", "Tide"), | ||
); | ||
|
||
app.at("/").get(async || "Hello, world!"); | ||
app.serve("127.0.0.1:7878") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
use futures::future::FutureObj; | ||
|
||
use http::{ | ||
header::{HeaderValue, IntoHeaderName}, | ||
HeaderMap, HttpTryFrom, | ||
}; | ||
|
||
use crate::{head::Head, Middleware, Response}; | ||
|
||
#[derive(Clone, Default)] | ||
pub struct DefaultHeaders { | ||
headers: HeaderMap, | ||
} | ||
|
||
impl DefaultHeaders { | ||
pub fn new() -> DefaultHeaders { | ||
DefaultHeaders::default() | ||
} | ||
|
||
#[inline] | ||
pub fn header<K, V>(mut self, key: K, value: V) -> Self | ||
where | ||
K: IntoHeaderName, | ||
HeaderValue: HttpTryFrom<V>, | ||
{ | ||
let value = HeaderValue::try_from(value) | ||
.map_err(Into::into) | ||
.expect("Cannot create default header"); | ||
|
||
self.headers.append(key, value); | ||
|
||
self | ||
} | ||
} | ||
|
||
impl<Data> Middleware<Data> for DefaultHeaders { | ||
fn response( | ||
&self, | ||
data: &mut Data, | ||
head: &Head, | ||
mut resp: Response, | ||
) -> FutureObj<'static, Response> { | ||
let headers = resp.headers_mut(); | ||
for (key, value) in self.headers.iter() { | ||
headers.entry(key).unwrap().or_insert_with(|| value.clone()); | ||
} | ||
FutureObj::new(Box::new(async { resp })) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters