Compare commits
12 commits
354949a032
...
7f86743f4a
| Author | SHA1 | Date | |
|---|---|---|---|
| 7f86743f4a | |||
| f794fa3ce0 | |||
| 1a7605c4fc | |||
| 250dc02407 | |||
| 232a1d4526 | |||
| e7aa4cae96 | |||
| 47d6f2e6f0 | |||
| 23fe147861 | |||
| b041941a4a | |||
| ef6a190549 | |||
| d751eb62a9 | |||
| f88476604d |
8 changed files with 442 additions and 93 deletions
66
.env.example
Normal file
66
.env.example
Normal file
|
|
@ -0,0 +1,66 @@
|
||||||
|
# Port to listen on (binds to 127.0.0.1).
|
||||||
|
BOOK_PORT=8123
|
||||||
|
|
||||||
|
# Directory for guestbook entry files.
|
||||||
|
BOOK_DATA_DIR=./data
|
||||||
|
|
||||||
|
# Site title shown in nav and page title.
|
||||||
|
BOOK_SITE_TITLE=guestbook
|
||||||
|
|
||||||
|
# Telegram bot token.
|
||||||
|
BOOK_TELEGRAM_BOT_TOKEN=your-bot-token-here
|
||||||
|
|
||||||
|
# Telegram chat ID for moderation messages.
|
||||||
|
BOOK_TELEGRAM_CHAT_ID=0
|
||||||
|
|
||||||
|
# Enable honeypot field for spam prevention.
|
||||||
|
BOOK_HONEYPOT=true
|
||||||
|
|
||||||
|
# Maximum length for names. 0 for unlimited.
|
||||||
|
BOOK_MAX_NAME_LENGTH=50
|
||||||
|
|
||||||
|
# Maximum length for messages. 0 for unlimited.
|
||||||
|
BOOK_MAX_MESSAGE_LENGTH=1000
|
||||||
|
|
||||||
|
# Maximum length for website URLs. 0 for unlimited.
|
||||||
|
BOOK_MAX_WEBSITE_LENGTH=100
|
||||||
|
|
||||||
|
# Allow new guestbook submissions. When false, the form is hidden and submissions are rejected.
|
||||||
|
BOOK_OPEN_REGISTRATION=true
|
||||||
|
|
||||||
|
# Separator between guestbook entries.
|
||||||
|
BOOK_SEPARATOR=------------------------------------------------------------
|
||||||
|
|
||||||
|
# Path to a CSS file. Takes precedence over BOOK_STYLE.
|
||||||
|
# BOOK_STYLE_FILE=./templates/default.css
|
||||||
|
|
||||||
|
# Custom CSS injected into a style tag.
|
||||||
|
# Classes: .guestbook-form, .guestbook-prompt, .guestbook-label, .guestbook-input,
|
||||||
|
# .guestbook-textarea, .guestbook-button, .entry-header, .entry-name,
|
||||||
|
# .entry-website, .entry-body, .entry-separator
|
||||||
|
# BOOK_STYLE=
|
||||||
|
|
||||||
|
# Text shown above the form.
|
||||||
|
BOOK_FORM_PROMPT=If you visited my site, please sign my guestbook!
|
||||||
|
|
||||||
|
# Submit button text.
|
||||||
|
BOOK_BUTTON_TEXT=sign
|
||||||
|
|
||||||
|
# Label for the name field.
|
||||||
|
BOOK_LABEL_NAME=Your name:
|
||||||
|
|
||||||
|
# Label for the website field.
|
||||||
|
BOOK_LABEL_WEBSITE=Your website (optional):
|
||||||
|
|
||||||
|
# Label for the message field.
|
||||||
|
BOOK_LABEL_MESSAGE=Your message:
|
||||||
|
|
||||||
|
# Number of rows for the message textarea.
|
||||||
|
BOOK_TEXTAREA_ROWS=8
|
||||||
|
|
||||||
|
# Number of columns for the message textarea.
|
||||||
|
BOOK_TEXTAREA_COLS=60
|
||||||
|
|
||||||
|
# Custom HTML template file with {{title}}, {{form}}, {{entries}}, and {{style}} placeholders.
|
||||||
|
# Uses built-in default if unset.
|
||||||
|
# BOOK_TEMPLATE=./templates/default.html
|
||||||
2
LICENSE
2
LICENSE
|
|
@ -1,6 +1,6 @@
|
||||||
MIT License
|
MIT License
|
||||||
|
|
||||||
Copyright (c) 2026 Llywelwyn
|
Copyright (c) 2026 Lewis Wynne
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
of this software and associated documentation files (the "Software"), to deal
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
|
|
||||||
86
module.nix
86
module.nix
|
|
@ -30,11 +30,6 @@ in
|
||||||
description = "Site title shown in nav and page title.";
|
description = "Site title shown in nav and page title.";
|
||||||
};
|
};
|
||||||
|
|
||||||
siteUrl = mkOption {
|
|
||||||
type = types.str;
|
|
||||||
description = "Base URL of the main site (for absolute nav links).";
|
|
||||||
};
|
|
||||||
|
|
||||||
telegramChatId = mkOption {
|
telegramChatId = mkOption {
|
||||||
type = types.int;
|
type = types.int;
|
||||||
description = "Telegram chat ID for moderation messages.";
|
description = "Telegram chat ID for moderation messages.";
|
||||||
|
|
@ -75,6 +70,72 @@ in
|
||||||
description = "Allow new guestbook submissions. When false, the form is hidden and submissions are rejected.";
|
description = "Allow new guestbook submissions. When false, the form is hidden and submissions are rejected.";
|
||||||
};
|
};
|
||||||
|
|
||||||
|
separator = mkOption {
|
||||||
|
type = types.str;
|
||||||
|
default = "------------------------------------------------------------";
|
||||||
|
description = "Separator between guestbook entries.";
|
||||||
|
};
|
||||||
|
|
||||||
|
style = mkOption {
|
||||||
|
type = types.str;
|
||||||
|
default = "";
|
||||||
|
description = "Custom CSS injected into a style tag. Use class names: .guestbook-form, .guestbook-prompt, .guestbook-label, .guestbook-input, .guestbook-textarea, .guestbook-button, .entry-header, .entry-name, .entry-website, .entry-body, .entry-separator";
|
||||||
|
};
|
||||||
|
|
||||||
|
styleFile = mkOption {
|
||||||
|
type = types.nullOr types.path;
|
||||||
|
default = null;
|
||||||
|
description = "Path to a CSS file. Takes precedence over style.";
|
||||||
|
};
|
||||||
|
|
||||||
|
formPrompt = mkOption {
|
||||||
|
type = types.str;
|
||||||
|
default = "If you visited my site, please sign my guestbook!";
|
||||||
|
description = "Text shown above the form.";
|
||||||
|
};
|
||||||
|
|
||||||
|
buttonText = mkOption {
|
||||||
|
type = types.str;
|
||||||
|
default = "sign";
|
||||||
|
description = "Submit button text.";
|
||||||
|
};
|
||||||
|
|
||||||
|
labelName = mkOption {
|
||||||
|
type = types.str;
|
||||||
|
default = "Your name:";
|
||||||
|
description = "Label for the name field.";
|
||||||
|
};
|
||||||
|
|
||||||
|
labelWebsite = mkOption {
|
||||||
|
type = types.str;
|
||||||
|
default = "Your website (optional):";
|
||||||
|
description = "Label for the website field.";
|
||||||
|
};
|
||||||
|
|
||||||
|
labelMessage = mkOption {
|
||||||
|
type = types.str;
|
||||||
|
default = "Your message:";
|
||||||
|
description = "Label for the message field.";
|
||||||
|
};
|
||||||
|
|
||||||
|
textareaRows = mkOption {
|
||||||
|
type = types.int;
|
||||||
|
default = 8;
|
||||||
|
description = "Number of rows for the message textarea.";
|
||||||
|
};
|
||||||
|
|
||||||
|
textareaCols = mkOption {
|
||||||
|
type = types.int;
|
||||||
|
default = 60;
|
||||||
|
description = "Number of columns for the message textarea.";
|
||||||
|
};
|
||||||
|
|
||||||
|
templateFile = mkOption {
|
||||||
|
type = types.nullOr types.path;
|
||||||
|
default = null;
|
||||||
|
description = "Custom HTML template file with {{title}}, {{form}}, and {{entries}} placeholders. Uses built-in default if null.";
|
||||||
|
};
|
||||||
|
|
||||||
user = mkOption {
|
user = mkOption {
|
||||||
type = types.str;
|
type = types.str;
|
||||||
default = "guestbook";
|
default = "guestbook";
|
||||||
|
|
@ -108,13 +169,26 @@ in
|
||||||
BOOK_PORT = toString cfg.port;
|
BOOK_PORT = toString cfg.port;
|
||||||
BOOK_DATA_DIR = cfg.dataDir;
|
BOOK_DATA_DIR = cfg.dataDir;
|
||||||
BOOK_SITE_TITLE = cfg.siteTitle;
|
BOOK_SITE_TITLE = cfg.siteTitle;
|
||||||
BOOK_SITE_URL = cfg.siteUrl;
|
|
||||||
BOOK_TELEGRAM_CHAT_ID = toString cfg.telegramChatId;
|
BOOK_TELEGRAM_CHAT_ID = toString cfg.telegramChatId;
|
||||||
BOOK_HONEYPOT = if cfg.honeypot then "true" else "false";
|
BOOK_HONEYPOT = if cfg.honeypot then "true" else "false";
|
||||||
BOOK_MAX_NAME_LENGTH = toString cfg.maxNameLength;
|
BOOK_MAX_NAME_LENGTH = toString cfg.maxNameLength;
|
||||||
BOOK_MAX_MESSAGE_LENGTH = toString cfg.maxMessageLength;
|
BOOK_MAX_MESSAGE_LENGTH = toString cfg.maxMessageLength;
|
||||||
BOOK_MAX_WEBSITE_LENGTH = toString cfg.maxWebsiteLength;
|
BOOK_MAX_WEBSITE_LENGTH = toString cfg.maxWebsiteLength;
|
||||||
BOOK_OPEN_REGISTRATION = if cfg.openRegistration then "true" else "false";
|
BOOK_OPEN_REGISTRATION = if cfg.openRegistration then "true" else "false";
|
||||||
|
BOOK_SEPARATOR = cfg.separator;
|
||||||
|
BOOK_STYLE = cfg.style;
|
||||||
|
} // lib.optionalAttrs (cfg.styleFile != null) {
|
||||||
|
BOOK_STYLE_FILE = cfg.styleFile;
|
||||||
|
BOOK_FORM_PROMPT = cfg.formPrompt;
|
||||||
|
BOOK_BUTTON_TEXT = cfg.buttonText;
|
||||||
|
BOOK_LABEL_NAME = cfg.labelName;
|
||||||
|
BOOK_LABEL_WEBSITE = cfg.labelWebsite;
|
||||||
|
BOOK_LABEL_MESSAGE = cfg.labelMessage;
|
||||||
|
BOOK_TEXTAREA_ROWS = toString cfg.textareaRows;
|
||||||
|
BOOK_TEXTAREA_COLS = toString cfg.textareaCols;
|
||||||
|
} // lib.optionalAttrs (cfg.templateFile != null) {
|
||||||
|
BOOK_TEMPLATE = cfg.templateFile;
|
||||||
};
|
};
|
||||||
serviceConfig = {
|
serviceConfig = {
|
||||||
Type = "simple";
|
Type = "simple";
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ pub struct Config {
|
||||||
pub port: u16,
|
pub port: u16,
|
||||||
pub data_dir: PathBuf,
|
pub data_dir: PathBuf,
|
||||||
pub site_title: String,
|
pub site_title: String,
|
||||||
pub site_url: String,
|
|
||||||
pub telegram_bot_token: String,
|
pub telegram_bot_token: String,
|
||||||
pub telegram_chat_id: i64,
|
pub telegram_chat_id: i64,
|
||||||
pub honeypot: bool,
|
pub honeypot: bool,
|
||||||
|
|
@ -14,6 +14,16 @@ pub struct Config {
|
||||||
pub max_message_length: usize,
|
pub max_message_length: usize,
|
||||||
pub max_website_length: usize,
|
pub max_website_length: usize,
|
||||||
pub open_registration: bool,
|
pub open_registration: bool,
|
||||||
|
pub template: Option<String>,
|
||||||
|
pub separator: String,
|
||||||
|
pub style: String,
|
||||||
|
pub form_prompt: String,
|
||||||
|
pub button_text: String,
|
||||||
|
pub label_name: String,
|
||||||
|
pub label_website: String,
|
||||||
|
pub label_message: String,
|
||||||
|
pub textarea_rows: u32,
|
||||||
|
pub textarea_cols: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Config {
|
impl Config {
|
||||||
|
|
@ -31,7 +41,7 @@ impl Config {
|
||||||
.map(PathBuf::from)
|
.map(PathBuf::from)
|
||||||
.unwrap_or_else(|_| PathBuf::from("./data")),
|
.unwrap_or_else(|_| PathBuf::from("./data")),
|
||||||
site_title: env::var("BOOK_SITE_TITLE").unwrap_or_else(|_| "guestbook".into()),
|
site_title: env::var("BOOK_SITE_TITLE").unwrap_or_else(|_| "guestbook".into()),
|
||||||
site_url: env::var("BOOK_SITE_URL").map_err(|_| "BOOK_SITE_URL is required")?,
|
|
||||||
telegram_bot_token: env::var("BOOK_TELEGRAM_BOT_TOKEN")
|
telegram_bot_token: env::var("BOOK_TELEGRAM_BOT_TOKEN")
|
||||||
.map_err(|_| "BOOK_TELEGRAM_BOT_TOKEN is required")?,
|
.map_err(|_| "BOOK_TELEGRAM_BOT_TOKEN is required")?,
|
||||||
telegram_chat_id: env::var("BOOK_TELEGRAM_CHAT_ID")
|
telegram_chat_id: env::var("BOOK_TELEGRAM_CHAT_ID")
|
||||||
|
|
@ -56,6 +66,38 @@ impl Config {
|
||||||
open_registration: env::var("BOOK_OPEN_REGISTRATION")
|
open_registration: env::var("BOOK_OPEN_REGISTRATION")
|
||||||
.map(|v| v != "false")
|
.map(|v| v != "false")
|
||||||
.unwrap_or(true),
|
.unwrap_or(true),
|
||||||
|
separator: env::var("BOOK_SEPARATOR")
|
||||||
|
.unwrap_or_else(|_| "------------------------------------------------------------".into()),
|
||||||
|
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}"))
|
||||||
|
}),
|
||||||
|
style: env::var("BOOK_STYLE_FILE")
|
||||||
|
.ok()
|
||||||
|
.map(|path| {
|
||||||
|
std::fs::read_to_string(&path)
|
||||||
|
.unwrap_or_else(|e| panic!("failed to read style file {path}: {e}"))
|
||||||
|
})
|
||||||
|
.or_else(|| env::var("BOOK_STYLE").ok())
|
||||||
|
.unwrap_or_default(),
|
||||||
|
form_prompt: env::var("BOOK_FORM_PROMPT")
|
||||||
|
.unwrap_or_else(|_| "If you visited my site, please sign my guestbook!".into()),
|
||||||
|
button_text: env::var("BOOK_BUTTON_TEXT")
|
||||||
|
.unwrap_or_else(|_| "sign".into()),
|
||||||
|
label_name: env::var("BOOK_LABEL_NAME")
|
||||||
|
.unwrap_or_else(|_| "Your name:".into()),
|
||||||
|
label_website: env::var("BOOK_LABEL_WEBSITE")
|
||||||
|
.unwrap_or_else(|_| "Your website (optional):".into()),
|
||||||
|
label_message: env::var("BOOK_LABEL_MESSAGE")
|
||||||
|
.unwrap_or_else(|_| "Your message:".into()),
|
||||||
|
textarea_rows: env::var("BOOK_TEXTAREA_ROWS")
|
||||||
|
.unwrap_or_else(|_| "8".into())
|
||||||
|
.parse()
|
||||||
|
.map_err(|_| "BOOK_TEXTAREA_ROWS must be a number")?,
|
||||||
|
textarea_cols: env::var("BOOK_TEXTAREA_COLS")
|
||||||
|
.unwrap_or_else(|_| "60".into())
|
||||||
|
.parse()
|
||||||
|
.map_err(|_| "BOOK_TEXTAREA_COLS must be a number")?,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -73,7 +115,6 @@ mod tests {
|
||||||
env::set_var("BOOK_PORT", "9999");
|
env::set_var("BOOK_PORT", "9999");
|
||||||
env::set_var("BOOK_DATA_DIR", "/tmp/gb");
|
env::set_var("BOOK_DATA_DIR", "/tmp/gb");
|
||||||
env::set_var("BOOK_SITE_TITLE", "test.rs");
|
env::set_var("BOOK_SITE_TITLE", "test.rs");
|
||||||
env::set_var("BOOK_SITE_URL", "https://test.rs");
|
|
||||||
env::set_var("BOOK_TELEGRAM_BOT_TOKEN", "123:ABC");
|
env::set_var("BOOK_TELEGRAM_BOT_TOKEN", "123:ABC");
|
||||||
env::set_var("BOOK_TELEGRAM_CHAT_ID", "12345");
|
env::set_var("BOOK_TELEGRAM_CHAT_ID", "12345");
|
||||||
|
|
||||||
|
|
@ -82,14 +123,12 @@ mod tests {
|
||||||
assert_eq!(config.listen_addr(), "127.0.0.1:9999");
|
assert_eq!(config.listen_addr(), "127.0.0.1:9999");
|
||||||
assert_eq!(config.data_dir, PathBuf::from("/tmp/gb"));
|
assert_eq!(config.data_dir, PathBuf::from("/tmp/gb"));
|
||||||
assert_eq!(config.site_title, "test.rs");
|
assert_eq!(config.site_title, "test.rs");
|
||||||
assert_eq!(config.site_url, "https://test.rs");
|
|
||||||
assert_eq!(config.telegram_chat_id, 12345);
|
assert_eq!(config.telegram_chat_id, 12345);
|
||||||
|
|
||||||
// Clean up
|
// Clean up
|
||||||
env::remove_var("BOOK_PORT");
|
env::remove_var("BOOK_PORT");
|
||||||
env::remove_var("BOOK_DATA_DIR");
|
env::remove_var("BOOK_DATA_DIR");
|
||||||
env::remove_var("BOOK_SITE_TITLE");
|
env::remove_var("BOOK_SITE_TITLE");
|
||||||
env::remove_var("BOOK_SITE_URL");
|
|
||||||
env::remove_var("BOOK_TELEGRAM_BOT_TOKEN");
|
env::remove_var("BOOK_TELEGRAM_BOT_TOKEN");
|
||||||
env::remove_var("BOOK_TELEGRAM_CHAT_ID");
|
env::remove_var("BOOK_TELEGRAM_CHAT_ID");
|
||||||
}
|
}
|
||||||
|
|
@ -97,7 +136,6 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn test_defaults() {
|
fn test_defaults() {
|
||||||
let _lock = ENV_LOCK.lock().unwrap();
|
let _lock = ENV_LOCK.lock().unwrap();
|
||||||
env::set_var("BOOK_SITE_URL", "https://test.rs");
|
|
||||||
env::set_var("BOOK_TELEGRAM_BOT_TOKEN", "123:ABC");
|
env::set_var("BOOK_TELEGRAM_BOT_TOKEN", "123:ABC");
|
||||||
env::set_var("BOOK_TELEGRAM_CHAT_ID", "12345");
|
env::set_var("BOOK_TELEGRAM_CHAT_ID", "12345");
|
||||||
|
|
||||||
|
|
@ -106,7 +144,6 @@ mod tests {
|
||||||
assert_eq!(config.data_dir, PathBuf::from("./data"));
|
assert_eq!(config.data_dir, PathBuf::from("./data"));
|
||||||
assert_eq!(config.site_title, "guestbook");
|
assert_eq!(config.site_title, "guestbook");
|
||||||
|
|
||||||
env::remove_var("BOOK_SITE_URL");
|
|
||||||
env::remove_var("BOOK_TELEGRAM_BOT_TOKEN");
|
env::remove_var("BOOK_TELEGRAM_BOT_TOKEN");
|
||||||
env::remove_var("BOOK_TELEGRAM_CHAT_ID");
|
env::remove_var("BOOK_TELEGRAM_CHAT_ID");
|
||||||
}
|
}
|
||||||
|
|
@ -114,7 +151,6 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn test_missing_required() {
|
fn test_missing_required() {
|
||||||
let _lock = ENV_LOCK.lock().unwrap();
|
let _lock = ENV_LOCK.lock().unwrap();
|
||||||
env::remove_var("BOOK_SITE_URL");
|
|
||||||
env::remove_var("BOOK_TELEGRAM_BOT_TOKEN");
|
env::remove_var("BOOK_TELEGRAM_BOT_TOKEN");
|
||||||
env::remove_var("BOOK_TELEGRAM_CHAT_ID");
|
env::remove_var("BOOK_TELEGRAM_CHAT_ID");
|
||||||
|
|
||||||
|
|
|
||||||
217
src/render.rs
217
src/render.rs
|
|
@ -1,69 +1,106 @@
|
||||||
|
use crate::config::Config;
|
||||||
use crate::entries::Entry;
|
use crate::entries::Entry;
|
||||||
|
|
||||||
pub fn render_page(site_title: &str, site_url: &str, entries: &[Entry], form_html: &str) -> String {
|
pub const DEFAULT_TEMPLATE: &str = include_str!("../templates/default.html");
|
||||||
let nav_url = site_url.trim_end_matches('/');
|
pub const DEFAULT_STYLE: &str = include_str!("../templates/default.css");
|
||||||
let mut html = format!(
|
|
||||||
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">
|
|
||||||
</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}
|
|
||||||
"#
|
|
||||||
);
|
|
||||||
|
|
||||||
|
pub fn render_page(template: &str, config: &Config, entries: &[Entry], form_html: &str) -> String {
|
||||||
|
let entries_html = render_entries(entries, &config.separator);
|
||||||
|
let css = if config.style.is_empty() {
|
||||||
|
DEFAULT_STYLE
|
||||||
|
} else {
|
||||||
|
&config.style
|
||||||
|
};
|
||||||
|
let style = format!("<style>\n{css}\n </style>");
|
||||||
|
template
|
||||||
|
.replace("{{title}}", &config.site_title)
|
||||||
|
.replace("{{form}}", form_html)
|
||||||
|
.replace("{{entries}}", &entries_html)
|
||||||
|
.replace("{{style}}", &style)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn render_form(config: &Config) -> String {
|
||||||
|
format!(
|
||||||
|
r#"<span class="guestbook-prompt">{prompt}</span>
|
||||||
|
<form class="guestbook-form" method="post" action="/submit" accept-charset="UTF-8">
|
||||||
|
<label class="guestbook-label">{label_name}</label>
|
||||||
|
<input class="guestbook-input" name="name" required>
|
||||||
|
|
||||||
|
<label class="guestbook-label">{label_website}</label>
|
||||||
|
<input class="guestbook-input" name="website">
|
||||||
|
|
||||||
|
<label class="guestbook-label">{label_message}</label>
|
||||||
|
<textarea class="guestbook-textarea" name="message" rows="{rows}" cols="{cols}" required></textarea>
|
||||||
|
<input name="url" style="display:none" tabindex="-1" autocomplete="off">
|
||||||
|
<button class="guestbook-button" type="submit">{button}</button>
|
||||||
|
</form>"#,
|
||||||
|
prompt = config.form_prompt,
|
||||||
|
label_name = config.label_name,
|
||||||
|
label_website = config.label_website,
|
||||||
|
label_message = config.label_message,
|
||||||
|
rows = config.textarea_rows,
|
||||||
|
cols = config.textarea_cols,
|
||||||
|
button = config.button_text,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn render_entries(entries: &[Entry], separator: &str) -> String {
|
||||||
|
let mut html = String::new();
|
||||||
for entry in entries {
|
for entry in entries {
|
||||||
html.push_str(&render_entry(entry));
|
html.push_str(&render_entry(entry, separator));
|
||||||
}
|
}
|
||||||
|
|
||||||
html.push_str("</body>\n</html>\n");
|
|
||||||
html
|
html
|
||||||
}
|
}
|
||||||
|
|
||||||
fn render_entry(entry: &Entry) -> String {
|
fn render_entry(entry: &Entry, separator: &str) -> String {
|
||||||
let mut header = format!(" <div class=\"entry\">\n <p>{} - <b>{}</b>", entry.meta.date, entry.meta.name);
|
let mut header = format!(
|
||||||
|
"<span class=\"entry-header\">{} - <span class=\"entry-name\">{}</span>",
|
||||||
|
entry.meta.date, entry.meta.name
|
||||||
|
);
|
||||||
if !entry.meta.website.is_empty() {
|
if !entry.meta.website.is_empty() {
|
||||||
header.push_str(&format!(
|
header.push_str(&format!(
|
||||||
" (<a href=\"{}\">{}</a>)",
|
" (<a class=\"entry-website\" href=\"{}\">{}</a>)",
|
||||||
entry.meta.website, entry.meta.website
|
entry.meta.website, entry.meta.website
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
header.push_str("</p>\n");
|
header.push_str("</span>");
|
||||||
format!("{header} {}\n </div>\n", entry.body)
|
format!(
|
||||||
|
"\n{header}\n\n<span class=\"entry-body\">{}</span>\n\n<span class=\"entry-separator\">{separator}</span>\n",
|
||||||
|
entry.body
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub const FORM_HTML: &str = r#" <form method="post" action="/submit">
|
|
||||||
<input name="name" placeholder="name" required>
|
|
||||||
<input name="website" placeholder="website (optional)">
|
|
||||||
<textarea name="message" placeholder="message" required></textarea>
|
|
||||||
<input name="url" style="display:none" tabindex="-1" autocomplete="off">
|
|
||||||
<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)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::entries::{Entry, EntryMeta, Status};
|
use crate::entries::{Entry, EntryMeta, Status};
|
||||||
|
use std::path::PathBuf;
|
||||||
|
|
||||||
|
fn test_config() -> Config {
|
||||||
|
Config {
|
||||||
|
port: 0,
|
||||||
|
data_dir: PathBuf::from("./data"),
|
||||||
|
site_title: "test".into(),
|
||||||
|
|
||||||
|
telegram_bot_token: "fake".into(),
|
||||||
|
telegram_chat_id: 0,
|
||||||
|
honeypot: true,
|
||||||
|
max_name_length: 50,
|
||||||
|
max_message_length: 1000,
|
||||||
|
max_website_length: 100,
|
||||||
|
open_registration: true,
|
||||||
|
template: None,
|
||||||
|
separator: "---".into(),
|
||||||
|
style: String::new(),
|
||||||
|
form_prompt: "Sign my guestbook!".into(),
|
||||||
|
button_text: "sign".into(),
|
||||||
|
label_name: "Your name:".into(),
|
||||||
|
label_website: "Your website (optional):".into(),
|
||||||
|
label_message: "Your message:".into(),
|
||||||
|
textarea_rows: 8,
|
||||||
|
textarea_cols: 60,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn make_entry(name: &str, date: &str, body: &str) -> Entry {
|
fn make_entry(name: &str, date: &str, body: &str) -> Entry {
|
||||||
Entry {
|
Entry {
|
||||||
|
|
@ -79,41 +116,91 @@ mod tests {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_render_page_contains_nav() {
|
fn test_render_default_template() {
|
||||||
let html = render_page("ily.rs", "https://ily.rs", &[], FORM_HTML);
|
let config = test_config();
|
||||||
assert!(html.contains(r#"<a href="https://ily.rs/">ily.rs</a>"#));
|
let form = render_form(&config);
|
||||||
assert!(html.contains(r#"<a href="https://ily.rs/links/">links</a>"#));
|
let html = render_page(DEFAULT_TEMPLATE, &config, &[], &form);
|
||||||
|
assert!(html.contains("<title>test</title>"));
|
||||||
|
assert!(html.contains("guestbook-form"));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_render_page_contains_form() {
|
fn test_render_custom_template() {
|
||||||
let html = render_page("ily.rs", "https://ily.rs", &[], FORM_HTML);
|
let config = test_config();
|
||||||
assert!(html.contains(r#"action="/submit""#));
|
let custom = "<html>{{title}} {{form}} {{entries}} {{style}}</html>";
|
||||||
assert!(html.contains(r#"style="display:none""#)); // honeypot
|
let form = render_form(&config);
|
||||||
|
let html = render_page(custom, &config, &[], &form);
|
||||||
|
assert!(html.contains("test"));
|
||||||
|
assert!(html.contains("guestbook-form"));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_render_entry_no_website() {
|
fn test_render_entry_classes() {
|
||||||
|
let config = test_config();
|
||||||
let entry = make_entry("alice", "2026-04-09", "Hello!");
|
let entry = make_entry("alice", "2026-04-09", "Hello!");
|
||||||
let html = render_page("ily.rs", "https://ily.rs", &[entry], FORM_HTML);
|
let form = render_form(&config);
|
||||||
assert!(html.contains("<b>alice</b>"));
|
let html = render_page(DEFAULT_TEMPLATE, &config, &[entry], &form);
|
||||||
assert!(html.contains("Hello!"));
|
assert!(html.contains("entry-header"));
|
||||||
assert!(!html.contains("<hr"));
|
assert!(html.contains("entry-name"));
|
||||||
|
assert!(html.contains("entry-body"));
|
||||||
|
assert!(html.contains("entry-separator"));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_render_entry_with_website() {
|
fn test_render_entry_with_website() {
|
||||||
|
let config = test_config();
|
||||||
let mut entry = make_entry("bob", "2026-04-09", "Hi!");
|
let mut entry = make_entry("bob", "2026-04-09", "Hi!");
|
||||||
entry.meta.website = "https://bob.com".into();
|
entry.meta.website = "https://bob.com".into();
|
||||||
let html = render_page("ily.rs", "https://ily.rs", &[entry], FORM_HTML);
|
let form = render_form(&config);
|
||||||
assert!(html.contains(r#"<a href="https://bob.com">"#));
|
let html = render_page(DEFAULT_TEMPLATE, &config, &[entry], &form);
|
||||||
|
assert!(html.contains("entry-website"));
|
||||||
|
assert!(html.contains(r#"href="https://bob.com">"#));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_render_preserves_html_in_body() {
|
fn test_render_preserves_html_in_body() {
|
||||||
let entry = make_entry("carol", "2026-04-09", "<b>Bold</b> <script>alert(1)</script>");
|
let config = test_config();
|
||||||
let html = render_page("ily.rs", "https://ily.rs", &[entry], FORM_HTML);
|
let entry = make_entry("carol", "2026-04-09", "<b>Bold</b>");
|
||||||
|
let form = render_form(&config);
|
||||||
|
let html = render_page(DEFAULT_TEMPLATE, &config, &[entry], &form);
|
||||||
assert!(html.contains("<b>Bold</b>"));
|
assert!(html.contains("<b>Bold</b>"));
|
||||||
assert!(html.contains("<script>alert(1)</script>"));
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_render_empty_form_when_closed() {
|
||||||
|
let config = test_config();
|
||||||
|
let html = render_page(DEFAULT_TEMPLATE, &config, &[], "");
|
||||||
|
assert!(!html.contains("action=\"/submit\""));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_render_custom_style() {
|
||||||
|
let mut config = test_config();
|
||||||
|
config.style = ".entry-name { color: red; }".into();
|
||||||
|
let html = render_page(DEFAULT_TEMPLATE, &config, &[], "");
|
||||||
|
assert!(html.contains(".entry-name { color: red; }"));
|
||||||
|
assert!(html.contains("<style>"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_render_form_custom_labels() {
|
||||||
|
let mut config = test_config();
|
||||||
|
config.form_prompt = "Leave a note!".into();
|
||||||
|
config.button_text = "submit".into();
|
||||||
|
config.label_name = "Name:".into();
|
||||||
|
let form = render_form(&config);
|
||||||
|
assert!(form.contains("Leave a note!"));
|
||||||
|
assert!(form.contains("submit"));
|
||||||
|
assert!(form.contains("Name:"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_render_form_custom_textarea() {
|
||||||
|
let mut config = test_config();
|
||||||
|
config.textarea_rows = 12;
|
||||||
|
config.textarea_cols = 40;
|
||||||
|
let form = render_form(&config);
|
||||||
|
assert!(form.contains("rows=\"12\""));
|
||||||
|
assert!(form.contains("cols=\"40\""));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
46
src/web.rs
46
src/web.rs
|
|
@ -1,7 +1,6 @@
|
||||||
use axum::{
|
use axum::{
|
||||||
extract::State,
|
extract::State,
|
||||||
http::header,
|
response::Html,
|
||||||
response::{Html, IntoResponse},
|
|
||||||
routing::{get, post},
|
routing::{get, post},
|
||||||
Form, Router,
|
Form, Router,
|
||||||
};
|
};
|
||||||
|
|
@ -11,7 +10,7 @@ use uuid::Uuid;
|
||||||
|
|
||||||
use crate::config::Config;
|
use crate::config::Config;
|
||||||
use crate::entries::{self, Entry, EntryMeta, Status};
|
use crate::entries::{self, Entry, EntryMeta, Status};
|
||||||
use crate::render::{self, FORM_HTML, STYLE_CSS};
|
use crate::render::{self, DEFAULT_TEMPLATE};
|
||||||
|
|
||||||
pub struct AppState {
|
pub struct AppState {
|
||||||
pub config: Config,
|
pub config: Config,
|
||||||
|
|
@ -32,19 +31,23 @@ pub fn router(state: Arc<AppState>) -> Router {
|
||||||
Router::new()
|
Router::new()
|
||||||
.route("/", get(index))
|
.route("/", get(index))
|
||||||
.route("/submit", post(submit))
|
.route("/submit", post(submit))
|
||||||
.route("/style.css", get(style))
|
|
||||||
.with_state(state)
|
.with_state(state)
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn index(State(state): State<Arc<AppState>>) -> Html<String> {
|
async fn index(State(state): State<Arc<AppState>>) -> Html<String> {
|
||||||
let entries_dir = state.config.data_dir.join("entries");
|
let entries_dir = state.config.data_dir.join("entries");
|
||||||
let entries = entries::read_approved(&entries_dir);
|
let entries = entries::read_approved(&entries_dir);
|
||||||
let form = if state.config.open_registration { FORM_HTML } else { "" };
|
let form = if state.config.open_registration {
|
||||||
|
render::render_form(&state.config)
|
||||||
|
} else {
|
||||||
|
String::new()
|
||||||
|
};
|
||||||
|
let template = state.config.template.as_deref().unwrap_or(DEFAULT_TEMPLATE);
|
||||||
let html = render::render_page(
|
let html = render::render_page(
|
||||||
&state.config.site_title,
|
template,
|
||||||
&state.config.site_url,
|
&state.config,
|
||||||
&entries,
|
&entries,
|
||||||
form,
|
&form,
|
||||||
);
|
);
|
||||||
Html(html)
|
Html(html)
|
||||||
}
|
}
|
||||||
|
|
@ -113,10 +116,6 @@ async fn submit(
|
||||||
Html("Thanks! Your message is pending approval.".to_string())
|
Html("Thanks! Your message is pending approval.".to_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn style() -> impl IntoResponse {
|
|
||||||
([(header::CONTENT_TYPE, "text/css")], STYLE_CSS)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
@ -130,7 +129,7 @@ mod tests {
|
||||||
port: 0,
|
port: 0,
|
||||||
data_dir: dir.to_path_buf(),
|
data_dir: dir.to_path_buf(),
|
||||||
site_title: "test".into(),
|
site_title: "test".into(),
|
||||||
site_url: "https://test.rs".into(),
|
|
||||||
telegram_bot_token: "fake".into(),
|
telegram_bot_token: "fake".into(),
|
||||||
telegram_chat_id: 0,
|
telegram_chat_id: 0,
|
||||||
honeypot: true,
|
honeypot: true,
|
||||||
|
|
@ -138,6 +137,16 @@ mod tests {
|
||||||
max_message_length: 1000,
|
max_message_length: 1000,
|
||||||
max_website_length: 100,
|
max_website_length: 100,
|
||||||
open_registration: true,
|
open_registration: true,
|
||||||
|
template: None,
|
||||||
|
separator: "---".into(),
|
||||||
|
style: String::new(),
|
||||||
|
form_prompt: "Sign my guestbook!".into(),
|
||||||
|
button_text: "sign".into(),
|
||||||
|
label_name: "Your name:".into(),
|
||||||
|
label_website: "Your website (optional):".into(),
|
||||||
|
label_message: "Your message:".into(),
|
||||||
|
textarea_rows: 8,
|
||||||
|
textarea_cols: 60,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -270,6 +279,17 @@ mod tests {
|
||||||
assert!(body.contains("too long"));
|
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]
|
#[tokio::test]
|
||||||
async fn test_valid_submission_creates_entry() {
|
async fn test_valid_submission_creates_entry() {
|
||||||
let dir = tempfile::tempdir().unwrap();
|
let dir = tempfile::tempdir().unwrap();
|
||||||
|
|
|
||||||
23
templates/default.css
Normal file
23
templates/default.css
Normal file
|
|
@ -0,0 +1,23 @@
|
||||||
|
/* Page container */
|
||||||
|
.page-container {
|
||||||
|
max-width: 70ch;
|
||||||
|
margin: 0 auto;
|
||||||
|
padding: 1rem;
|
||||||
|
white-space: pre-wrap;
|
||||||
|
word-wrap: break-word;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Form */
|
||||||
|
.guestbook-prompt {}
|
||||||
|
.guestbook-form {}
|
||||||
|
.guestbook-label {}
|
||||||
|
.guestbook-input {}
|
||||||
|
.guestbook-textarea {}
|
||||||
|
.guestbook-button {}
|
||||||
|
|
||||||
|
/* Entries */
|
||||||
|
.entry-header {}
|
||||||
|
.entry-name {}
|
||||||
|
.entry-website {}
|
||||||
|
.entry-body {}
|
||||||
|
.entry-separator {}
|
||||||
43
templates/default.html
Normal file
43
templates/default.html
Normal file
|
|
@ -0,0 +1,43 @@
|
||||||
|
<!--
|
||||||
|
Default guestbook template.
|
||||||
|
Copy this file and point BOOK_TEMPLATE at your copy to customize.
|
||||||
|
|
||||||
|
Placeholders are inserted with double curly braces, e.g. curly-title-curly.
|
||||||
|
|
||||||
|
Available placeholders:
|
||||||
|
|
||||||
|
title - Site title (BOOK_SITE_TITLE). Useful in <title> and headings.
|
||||||
|
form - The submission form (labels, inputs, button). Controlled by
|
||||||
|
BOOK_FORM_PROMPT, BOOK_LABEL_NAME, BOOK_LABEL_WEBSITE,
|
||||||
|
BOOK_LABEL_MESSAGE, BOOK_BUTTON_TEXT, BOOK_TEXTAREA_ROWS,
|
||||||
|
BOOK_TEXTAREA_COLS. Empty when BOOK_OPEN_REGISTRATION=false.
|
||||||
|
entries - Approved guestbook entries, newest first. Entry separator
|
||||||
|
controlled by BOOK_SEPARATOR.
|
||||||
|
style - Custom CSS from BOOK_STYLE or BOOK_STYLE_FILE, wrapped in
|
||||||
|
a <style> tag. Uses built-in default.css when neither is set.
|
||||||
|
|
||||||
|
See default.css for available CSS classes on rendered elements.
|
||||||
|
-->
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||||
|
<title>{{title}}</title>
|
||||||
|
{{style}}
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div class="page-container">
|
||||||
|
{{title}}
|
||||||
|
|
||||||
|
guestbook
|
||||||
|
=========
|
||||||
|
|
||||||
|
{{form}}
|
||||||
|
|
||||||
|
entries
|
||||||
|
=======
|
||||||
|
{{entries}}
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
Loading…
Add table
Add a link
Reference in a new issue