feat: restructures module.nix opts
This commit is contained in:
parent
d26c289f66
commit
2044616aeb
5 changed files with 125 additions and 71 deletions
16
README.md
16
README.md
|
|
@ -196,11 +196,17 @@ services.guestbook = {
|
||||||
forwardAuth = null; # e.g. "localhost:9090"
|
forwardAuth = null; # e.g. "localhost:9090"
|
||||||
};
|
};
|
||||||
|
|
||||||
|
features = {
|
||||||
|
submissions.enable = true;
|
||||||
|
websites.enable = true;
|
||||||
|
drawing = {
|
||||||
|
enable = false;
|
||||||
|
canvasWidth = 400;
|
||||||
|
canvasHeight = 200;
|
||||||
|
};
|
||||||
security = {
|
security = {
|
||||||
enableSubmissions = true;
|
htmlInjection.enable = false;
|
||||||
enableHtmlInjection = false;
|
honeypot.enable = true;
|
||||||
enableWebsiteLinks = true;
|
|
||||||
enableHoneypot = true;
|
|
||||||
captcha = {
|
captcha = {
|
||||||
enable = false;
|
enable = false;
|
||||||
question = "";
|
question = "";
|
||||||
|
|
@ -209,6 +215,7 @@ services.guestbook = {
|
||||||
caseSensitive = false;
|
caseSensitive = false;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
};
|
||||||
|
|
||||||
telegram = {
|
telegram = {
|
||||||
enable = false;
|
enable = false;
|
||||||
|
|
@ -233,6 +240,7 @@ services.guestbook = {
|
||||||
name = "Your name:";
|
name = "Your name:";
|
||||||
website = "Your website (optional):";
|
website = "Your website (optional):";
|
||||||
message = "Your message:";
|
message = "Your message:";
|
||||||
|
drawing = "Draw (optional):";
|
||||||
};
|
};
|
||||||
message = {
|
message = {
|
||||||
width = 400;
|
width = 400;
|
||||||
|
|
|
||||||
80
module.nix
80
module.nix
|
|
@ -57,30 +57,59 @@ in
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
security = {
|
features = {
|
||||||
enableSubmissions = mkOption {
|
submissions = {
|
||||||
|
enable = mkOption {
|
||||||
type = types.bool;
|
type = types.bool;
|
||||||
default = true;
|
default = true;
|
||||||
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.";
|
||||||
};
|
};
|
||||||
|
|
||||||
enableHtmlInjection = mkOption {
|
|
||||||
type = types.bool;
|
|
||||||
default = false;
|
|
||||||
description = "Allow raw HTML/JS in entry names and message bodies. When false, HTML is escaped. Website URLs are always escaped.";
|
|
||||||
};
|
};
|
||||||
|
|
||||||
enableWebsiteLinks = mkOption {
|
websites = {
|
||||||
|
enable = mkOption {
|
||||||
type = types.bool;
|
type = types.bool;
|
||||||
default = true;
|
default = true;
|
||||||
description = "Show website field in form and render website links in entries. When false, the input is hidden, submitted values are ignored, and existing links are not displayed.";
|
description = "Show website field in form and render website links in entries. When false, the input is hidden, submitted values are ignored, and existing links are not displayed.";
|
||||||
};
|
};
|
||||||
|
};
|
||||||
|
|
||||||
enableHoneypot = mkOption {
|
drawing = {
|
||||||
|
enable = mkOption {
|
||||||
|
type = types.bool;
|
||||||
|
default = false;
|
||||||
|
description = "Enable the drawing canvas in the submission form. Stores PNG files in dataDir/drawings/.";
|
||||||
|
};
|
||||||
|
|
||||||
|
canvasWidth = mkOption {
|
||||||
|
type = types.int;
|
||||||
|
default = 400;
|
||||||
|
description = "Drawing canvas width in pixels.";
|
||||||
|
};
|
||||||
|
|
||||||
|
canvasHeight = mkOption {
|
||||||
|
type = types.int;
|
||||||
|
default = 200;
|
||||||
|
description = "Drawing canvas height in pixels.";
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
security = {
|
||||||
|
htmlInjection = {
|
||||||
|
enable = mkOption {
|
||||||
|
type = types.bool;
|
||||||
|
default = false;
|
||||||
|
description = "Allow raw HTML/JS in entry names and message bodies. When false, HTML is escaped. Website URLs are always escaped.";
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
honeypot = {
|
||||||
|
enable = mkOption {
|
||||||
type = types.bool;
|
type = types.bool;
|
||||||
default = true;
|
default = true;
|
||||||
description = "Enable honeypot field for spam prevention.";
|
description = "Enable honeypot field for spam prevention.";
|
||||||
};
|
};
|
||||||
|
};
|
||||||
|
|
||||||
captcha = {
|
captcha = {
|
||||||
enable = mkEnableOption "captcha on submission form";
|
enable = mkEnableOption "captcha on submission form";
|
||||||
|
|
@ -110,6 +139,7 @@ in
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
};
|
||||||
|
|
||||||
telegram = {
|
telegram = {
|
||||||
enable = mkEnableOption "Telegram moderation notifications";
|
enable = mkEnableOption "Telegram moderation notifications";
|
||||||
|
|
@ -149,7 +179,7 @@ in
|
||||||
css = mkOption {
|
css = mkOption {
|
||||||
type = types.str;
|
type = types.str;
|
||||||
default = "";
|
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";
|
description = "Custom CSS injected into a style tag. Use class names: .guestbook-form, .guestbook-prompt, .guestbook-label, .guestbook-input, .guestbook-textarea, .guestbook-button, .guestbook-canvas, .entry-header, .entry-name, .entry-website, .entry-body, .entry-drawing, .entry-separator";
|
||||||
};
|
};
|
||||||
|
|
||||||
cssFile = mkOption {
|
cssFile = mkOption {
|
||||||
|
|
@ -200,6 +230,12 @@ in
|
||||||
default = "Your message:";
|
default = "Your message:";
|
||||||
description = "Label for the message field.";
|
description = "Label for the message field.";
|
||||||
};
|
};
|
||||||
|
|
||||||
|
drawing = mkOption {
|
||||||
|
type = types.str;
|
||||||
|
default = "Draw (optional):";
|
||||||
|
description = "Label for the drawing canvas.";
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
message = {
|
message = {
|
||||||
|
|
@ -230,15 +266,16 @@ in
|
||||||
BOOK_DATA_DIR = cfg.dataDir;
|
BOOK_DATA_DIR = cfg.dataDir;
|
||||||
BOOK_SITE_TITLE = cfg.siteTitle;
|
BOOK_SITE_TITLE = cfg.siteTitle;
|
||||||
|
|
||||||
BOOK_ENABLE_HONEYPOT = if cfg.security.enableHoneypot then "true" else "false";
|
BOOK_ENABLE_SUBMISSIONS = if cfg.features.submissions.enable then "true" else "false";
|
||||||
BOOK_ENABLE_SUBMISSIONS = if cfg.security.enableSubmissions then "true" else "false";
|
BOOK_ENABLE_WEBSITE_LINKS = if cfg.features.websites.enable then "true" else "false";
|
||||||
BOOK_ENABLE_HTML_INJECTION = if cfg.security.enableHtmlInjection then "true" else "false";
|
BOOK_ENABLE_DRAWINGS = if cfg.features.drawing.enable then "true" else "false";
|
||||||
BOOK_ENABLE_WEBSITE_LINKS = if cfg.security.enableWebsiteLinks then "true" else "false";
|
BOOK_ENABLE_HTML_INJECTION = if cfg.features.security.htmlInjection.enable then "true" else "false";
|
||||||
BOOK_ENABLE_CAPTCHA = if cfg.security.captcha.enable then "true" else "false";
|
BOOK_ENABLE_HONEYPOT = if cfg.features.security.honeypot.enable then "true" else "false";
|
||||||
BOOK_CAPTCHA_QUESTION = cfg.security.captcha.question;
|
BOOK_ENABLE_CAPTCHA = if cfg.features.security.captcha.enable then "true" else "false";
|
||||||
BOOK_CAPTCHA_ANSWER = cfg.security.captcha.answer;
|
BOOK_CAPTCHA_QUESTION = cfg.features.security.captcha.question;
|
||||||
BOOK_CAPTCHA_EXACT = if cfg.security.captcha.exact then "true" else "false";
|
BOOK_CAPTCHA_ANSWER = cfg.features.security.captcha.answer;
|
||||||
BOOK_CAPTCHA_CASESENSITIVE = if cfg.security.captcha.caseSensitive then "true" else "false";
|
BOOK_CAPTCHA_EXACT = if cfg.features.security.captcha.exact then "true" else "false";
|
||||||
|
BOOK_CAPTCHA_CASESENSITIVE = if cfg.features.security.captcha.caseSensitive then "true" else "false";
|
||||||
BOOK_MAX_NAME_LENGTH = toString cfg.limits.name;
|
BOOK_MAX_NAME_LENGTH = toString cfg.limits.name;
|
||||||
BOOK_MAX_MESSAGE_LENGTH = toString cfg.limits.message;
|
BOOK_MAX_MESSAGE_LENGTH = toString cfg.limits.message;
|
||||||
BOOK_MAX_WEBSITE_LENGTH = toString cfg.limits.website;
|
BOOK_MAX_WEBSITE_LENGTH = toString cfg.limits.website;
|
||||||
|
|
@ -249,6 +286,9 @@ in
|
||||||
BOOK_LABEL_NAME = cfg.styles.labels.name;
|
BOOK_LABEL_NAME = cfg.styles.labels.name;
|
||||||
BOOK_LABEL_WEBSITE = cfg.styles.labels.website;
|
BOOK_LABEL_WEBSITE = cfg.styles.labels.website;
|
||||||
BOOK_LABEL_MESSAGE = cfg.styles.labels.message;
|
BOOK_LABEL_MESSAGE = cfg.styles.labels.message;
|
||||||
|
BOOK_LABEL_DRAWING = cfg.styles.labels.drawing;
|
||||||
|
BOOK_CANVAS_WIDTH = toString cfg.features.drawing.canvasWidth;
|
||||||
|
BOOK_CANVAS_HEIGHT = toString cfg.features.drawing.canvasHeight;
|
||||||
BOOK_TEXTAREA_WIDTH = toString cfg.styles.message.width;
|
BOOK_TEXTAREA_WIDTH = toString cfg.styles.message.width;
|
||||||
BOOK_TEXTAREA_HEIGHT = toString cfg.styles.message.height;
|
BOOK_TEXTAREA_HEIGHT = toString cfg.styles.message.height;
|
||||||
} // lib.optionalAttrs (cfg.styles.cssFile != null) {
|
} // lib.optionalAttrs (cfg.styles.cssFile != null) {
|
||||||
|
|
@ -261,7 +301,7 @@ in
|
||||||
serviceConfig = {
|
serviceConfig = {
|
||||||
Type = "simple";
|
Type = "simple";
|
||||||
ExecStartPre = "+${pkgs.writeShellScript "guestbook-prepare" ''
|
ExecStartPre = "+${pkgs.writeShellScript "guestbook-prepare" ''
|
||||||
mkdir -p ${cfg.dataDir}/entries
|
mkdir -p ${cfg.dataDir}/entries ${cfg.dataDir}/drawings
|
||||||
chown -R ${cfg.user}:${cfg.group} ${cfg.dataDir}
|
chown -R ${cfg.user}:${cfg.group} ${cfg.dataDir}
|
||||||
''}";
|
''}";
|
||||||
Restart = "on-failure";
|
Restart = "on-failure";
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,7 @@ async fn main() {
|
||||||
|
|
||||||
std::fs::create_dir_all(&entries_dir).ok();
|
std::fs::create_dir_all(&entries_dir).ok();
|
||||||
|
|
||||||
let (tx, rx) = tokio::sync::mpsc::channel(32);
|
let (tx, rx) = tokio::sync::mpsc::channel::<(entries::Entry, Option<Vec<u8>>)>(32);
|
||||||
|
|
||||||
// Spawn telegram tasks if configured
|
// Spawn telegram tasks if configured
|
||||||
match (&config.telegram_bot_token, config.telegram_chat_id) {
|
match (&config.telegram_bot_token, config.telegram_chat_id) {
|
||||||
|
|
|
||||||
|
|
@ -18,9 +18,17 @@ async fn notify(bot: &Bot, chat_id: ChatId, entry: &Entry) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Listen for new entries on the channel and send Telegram notifications.
|
/// Listen for new entries on the channel and send Telegram notifications.
|
||||||
pub async fn notification_task(bot: Bot, chat_id: ChatId, mut rx: Receiver<Entry>) {
|
pub async fn notification_task(bot: Bot, chat_id: ChatId, mut rx: Receiver<(Entry, Option<Vec<u8>>)>) {
|
||||||
while let Some(entry) = rx.recv().await {
|
while let Some((entry, drawing_bytes)) = rx.recv().await {
|
||||||
notify(&bot, chat_id, &entry).await;
|
notify(&bot, chat_id, &entry).await;
|
||||||
|
if let Some(bytes) = drawing_bytes {
|
||||||
|
if let Err(e) = bot.send_photo(
|
||||||
|
chat_id,
|
||||||
|
teloxide::types::InputFile::memory(bytes).file_name("drawing.png"),
|
||||||
|
).await {
|
||||||
|
tracing::error!("failed to send drawing photo: {e}");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,7 @@ use crate::render::{self, DEFAULT_TEMPLATE};
|
||||||
|
|
||||||
pub struct AppState {
|
pub struct AppState {
|
||||||
pub config: Config,
|
pub config: Config,
|
||||||
pub tx: tokio::sync::mpsc::Sender<Entry>,
|
pub tx: tokio::sync::mpsc::Sender<(Entry, Option<Vec<u8>>)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
|
|
@ -205,8 +205,6 @@ async fn submit(
|
||||||
} else {
|
} else {
|
||||||
String::new()
|
String::new()
|
||||||
};
|
};
|
||||||
let _ = drawing_bytes;
|
|
||||||
|
|
||||||
let entry = Entry {
|
let entry = Entry {
|
||||||
id: filename.trim_end_matches(".txt").to_string(),
|
id: filename.trim_end_matches(".txt").to_string(),
|
||||||
meta: EntryMeta {
|
meta: EntryMeta {
|
||||||
|
|
@ -229,7 +227,7 @@ async fn submit(
|
||||||
}
|
}
|
||||||
|
|
||||||
// Notify telegram task
|
// Notify telegram task
|
||||||
let _ = state.tx.send(entry).await;
|
let _ = state.tx.send((entry, drawing_bytes)).await;
|
||||||
|
|
||||||
Html("Thanks! Your message is pending approval.".to_string())
|
Html("Thanks! Your message is pending approval.".to_string())
|
||||||
}
|
}
|
||||||
|
|
@ -280,7 +278,7 @@ mod tests {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn test_app(config: Config) -> (Router, tokio::sync::mpsc::Receiver<Entry>) {
|
fn test_app(config: Config) -> (Router, tokio::sync::mpsc::Receiver<(Entry, Option<Vec<u8>>)>) {
|
||||||
let (tx, rx) = tokio::sync::mpsc::channel(32);
|
let (tx, rx) = tokio::sync::mpsc::channel(32);
|
||||||
let state = Arc::new(AppState { config, tx });
|
let state = Arc::new(AppState { config, tx });
|
||||||
(router(state), rx)
|
(router(state), rx)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue