Skip to content

Commit

Permalink
add cache control header option
Browse files Browse the repository at this point in the history
Signed-off-by: karthik2804 <karthik.ganeshram@fermyon.com>
  • Loading branch information
karthik2804 committed Jun 25, 2023
1 parent e6ad1c4 commit 4ef4ccc
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/bartholomew.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,22 @@ pub fn render(req: Request) -> Result<Response> {
.clone()
.unwrap_or_else(|| DEFAULT_CONTENT_TYPE.to_owned());

let cache_control = doc.head.cache_control.clone();

let data = engine
.render_template(doc, config, req.headers().to_owned())
.map_err(|e| anyhow!("Rendering {:?}: {}", &content_path, e))?;

let content_encoding = req.headers().get(http::header::ACCEPT_ENCODING);

response::send_result(path_info, data, content_type, content_encoding, None)
response::send_result(
path_info,
data,
content_type,
cache_control,
content_encoding,
None,
)
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/content.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ pub struct Head {
///
/// This may result in HTTP headers being altered.
pub content_type: Option<String>,
/// Cache Control header override.
///
/// The default is to send no cachce control headers. This should only be
/// used when you are sure that the content is safe to be cached.
pub cache_control: Option<String>,
/// An optional status line
///
/// This should only ever be set if the status code is _not_ 200.
Expand Down
5 changes: 5 additions & 0 deletions src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,16 @@ pub fn send_result(
route: String,
body: String,
content_type: String,
cache_control: Option<String>,
content_encoding: Option<&HeaderValue>,
status: Option<u16>,
) -> Result<Response> {
eprintln!("Responded: {route}");
let mut bldr = Builder::new().header(http::header::CONTENT_TYPE, content_type);

if let Some(val) = cache_control {
bldr = bldr.header(http::header::CACHE_CONTROL, val);
}
if let Some(status) = status {
bldr = bldr.status(status);
}
Expand Down
1 change: 1 addition & 0 deletions src/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ pub fn error_values(title: &str, msg: &str) -> PageValues {
enable_shortcodes: None,
path_info: None,
body_source: None,
cache_control: None,
},
body: msg.to_string(),
published: true,
Expand Down

0 comments on commit 4ef4ccc

Please sign in to comment.