custom html templates, inline css, drop /style.css route
This commit is contained in:
parent
354949a032
commit
f88476604d
4 changed files with 76 additions and 47 deletions
|
|
@ -14,6 +14,7 @@ pub struct Config {
|
|||
pub max_message_length: usize,
|
||||
pub max_website_length: usize,
|
||||
pub open_registration: bool,
|
||||
pub template: Option<String>,
|
||||
}
|
||||
|
||||
impl Config {
|
||||
|
|
@ -56,6 +57,10 @@ impl Config {
|
|||
open_registration: env::var("BOOK_OPEN_REGISTRATION")
|
||||
.map(|v| v != "false")
|
||||
.unwrap_or(true),
|
||||
template: env::var("BOOK_TEMPLATE").ok().map(|path| {
|
||||
std::fs::read_to_string(&path)
|
||||
.unwrap_or_else(|e| panic!("failed to read template {path}: {e}"))
|
||||
}),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,39 +1,49 @@
|
|||
use crate::entries::Entry;
|
||||
|
||||
pub fn render_page(site_title: &str, site_url: &str, entries: &[Entry], form_html: &str) -> String {
|
||||
let nav_url = site_url.trim_end_matches('/');
|
||||
let mut html = format!(
|
||||
r#"<!DOCTYPE html>
|
||||
pub const DEFAULT_TEMPLATE: &str = r#"<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<title>{site_title}</title>
|
||||
<link rel="stylesheet" href="/style.css">
|
||||
<title>{{title}}</title>
|
||||
<style>
|
||||
body {
|
||||
max-width: 70ch;
|
||||
line-height: 1.5;
|
||||
margin: 0 auto;
|
||||
padding: 1rem;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<nav>
|
||||
<a href="{nav_url}/">{site_title}</a> |
|
||||
<a href="{nav_url}/links/">links</a> |
|
||||
<a href="{nav_url}/now/">now</a> |
|
||||
sign the <a href="/">guestbook</a>
|
||||
</nav>
|
||||
<h1>guestbook</h1>
|
||||
<p>If you visited my site, please sign my guestbook!</p>
|
||||
{form_html}
|
||||
"#
|
||||
);
|
||||
{{form}}
|
||||
{{entries}}
|
||||
</body>
|
||||
</html>
|
||||
"#;
|
||||
|
||||
pub fn render_page(template: &str, title: &str, entries: &[Entry], form_html: &str) -> String {
|
||||
let entries_html = render_entries(entries);
|
||||
template
|
||||
.replace("{{title}}", title)
|
||||
.replace("{{form}}", form_html)
|
||||
.replace("{{entries}}", &entries_html)
|
||||
}
|
||||
|
||||
fn render_entries(entries: &[Entry]) -> String {
|
||||
let mut html = String::new();
|
||||
for entry in entries {
|
||||
html.push_str(&render_entry(entry));
|
||||
}
|
||||
|
||||
html.push_str("</body>\n</html>\n");
|
||||
html
|
||||
}
|
||||
|
||||
fn render_entry(entry: &Entry) -> String {
|
||||
let mut header = format!(" <div class=\"entry\">\n <p>{} - <b>{}</b>", entry.meta.date, entry.meta.name);
|
||||
let mut header = format!(
|
||||
" <div class=\"entry\">\n <p>{} - <b>{}</b>",
|
||||
entry.meta.date, entry.meta.name
|
||||
);
|
||||
if !entry.meta.website.is_empty() {
|
||||
header.push_str(&format!(
|
||||
" (<a href=\"{}\">{}</a>)",
|
||||
|
|
@ -52,14 +62,6 @@ pub const FORM_HTML: &str = r#" <form method="post" action="/submit">
|
|||
<button type="submit">sign</button>
|
||||
</form>"#;
|
||||
|
||||
pub const STYLE_CSS: &str = "body {
|
||||
max-width: 70ch;
|
||||
line-height: 1.5;
|
||||
margin: 0 auto;
|
||||
padding: 1rem;
|
||||
}
|
||||
";
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
|
@ -79,23 +81,24 @@ mod tests {
|
|||
}
|
||||
|
||||
#[test]
|
||||
fn test_render_page_contains_nav() {
|
||||
let html = render_page("ily.rs", "https://ily.rs", &[], FORM_HTML);
|
||||
assert!(html.contains(r#"<a href="https://ily.rs/">ily.rs</a>"#));
|
||||
assert!(html.contains(r#"<a href="https://ily.rs/links/">links</a>"#));
|
||||
fn test_render_default_template() {
|
||||
let html = render_page(DEFAULT_TEMPLATE, "ily.rs", &[], FORM_HTML);
|
||||
assert!(html.contains("<title>ily.rs</title>"));
|
||||
assert!(html.contains("action=\"/submit\""));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_render_page_contains_form() {
|
||||
let html = render_page("ily.rs", "https://ily.rs", &[], FORM_HTML);
|
||||
assert!(html.contains(r#"action="/submit""#));
|
||||
assert!(html.contains(r#"style="display:none""#)); // honeypot
|
||||
fn test_render_custom_template() {
|
||||
let custom = "<html>{{title}} {{form}} {{entries}}</html>";
|
||||
let html = render_page(custom, "my site", &[], FORM_HTML);
|
||||
assert!(html.contains("my site"));
|
||||
assert!(html.contains("action=\"/submit\""));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_render_entry_no_website() {
|
||||
let entry = make_entry("alice", "2026-04-09", "Hello!");
|
||||
let html = render_page("ily.rs", "https://ily.rs", &[entry], FORM_HTML);
|
||||
let html = render_page(DEFAULT_TEMPLATE, "test", &[entry], FORM_HTML);
|
||||
assert!(html.contains("<b>alice</b>"));
|
||||
assert!(html.contains("Hello!"));
|
||||
assert!(!html.contains("<hr"));
|
||||
|
|
@ -105,15 +108,21 @@ mod tests {
|
|||
fn test_render_entry_with_website() {
|
||||
let mut entry = make_entry("bob", "2026-04-09", "Hi!");
|
||||
entry.meta.website = "https://bob.com".into();
|
||||
let html = render_page("ily.rs", "https://ily.rs", &[entry], FORM_HTML);
|
||||
let html = render_page(DEFAULT_TEMPLATE, "test", &[entry], FORM_HTML);
|
||||
assert!(html.contains(r#"<a href="https://bob.com">"#));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_render_preserves_html_in_body() {
|
||||
let entry = make_entry("carol", "2026-04-09", "<b>Bold</b> <script>alert(1)</script>");
|
||||
let html = render_page("ily.rs", "https://ily.rs", &[entry], FORM_HTML);
|
||||
let html = render_page(DEFAULT_TEMPLATE, "test", &[entry], FORM_HTML);
|
||||
assert!(html.contains("<b>Bold</b>"));
|
||||
assert!(html.contains("<script>alert(1)</script>"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_render_empty_form_when_closed() {
|
||||
let html = render_page(DEFAULT_TEMPLATE, "test", &[], "");
|
||||
assert!(!html.contains("action=\"/submit\""));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
25
src/web.rs
25
src/web.rs
|
|
@ -1,7 +1,6 @@
|
|||
use axum::{
|
||||
extract::State,
|
||||
http::header,
|
||||
response::{Html, IntoResponse},
|
||||
response::Html,
|
||||
routing::{get, post},
|
||||
Form, Router,
|
||||
};
|
||||
|
|
@ -11,7 +10,7 @@ use uuid::Uuid;
|
|||
|
||||
use crate::config::Config;
|
||||
use crate::entries::{self, Entry, EntryMeta, Status};
|
||||
use crate::render::{self, FORM_HTML, STYLE_CSS};
|
||||
use crate::render::{self, DEFAULT_TEMPLATE, FORM_HTML};
|
||||
|
||||
pub struct AppState {
|
||||
pub config: Config,
|
||||
|
|
@ -32,7 +31,6 @@ pub fn router(state: Arc<AppState>) -> Router {
|
|||
Router::new()
|
||||
.route("/", get(index))
|
||||
.route("/submit", post(submit))
|
||||
.route("/style.css", get(style))
|
||||
.with_state(state)
|
||||
}
|
||||
|
||||
|
|
@ -40,9 +38,10 @@ async fn index(State(state): State<Arc<AppState>>) -> Html<String> {
|
|||
let entries_dir = state.config.data_dir.join("entries");
|
||||
let entries = entries::read_approved(&entries_dir);
|
||||
let form = if state.config.open_registration { FORM_HTML } else { "" };
|
||||
let template = state.config.template.as_deref().unwrap_or(DEFAULT_TEMPLATE);
|
||||
let html = render::render_page(
|
||||
template,
|
||||
&state.config.site_title,
|
||||
&state.config.site_url,
|
||||
&entries,
|
||||
form,
|
||||
);
|
||||
|
|
@ -113,10 +112,6 @@ async fn submit(
|
|||
Html("Thanks! Your message is pending approval.".to_string())
|
||||
}
|
||||
|
||||
async fn style() -> impl IntoResponse {
|
||||
([(header::CONTENT_TYPE, "text/css")], STYLE_CSS)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
|
@ -138,6 +133,7 @@ mod tests {
|
|||
max_message_length: 1000,
|
||||
max_website_length: 100,
|
||||
open_registration: true,
|
||||
template: None,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -270,6 +266,17 @@ mod tests {
|
|||
assert!(body.contains("too long"));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_custom_template() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let mut config = test_config(dir.path());
|
||||
config.template = Some("<html><nav>custom nav</nav>{{form}}{{entries}}</html>".into());
|
||||
let (app, _rx) = test_app(config);
|
||||
let html = get_index(&app).await;
|
||||
assert!(html.contains("custom nav"));
|
||||
assert!(html.contains("action=\"/submit\""));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_valid_submission_creates_entry() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue