From 0eb625c188b16e7829690e19a6a70ee129368667 Mon Sep 17 00:00:00 2001 From: nmemmert <67961292+nmemmert@users.noreply.github.com> Date: Mon, 17 Aug 2026 13:22:28 -0400 Subject: [PATCH] Expand compose env vars; add ntfy token + priority + TZ support MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit docker-compose.yml: - Every env var documented inline with type/default/purpose - env_file: .env (required: false) — container starts without it - NTFY_TOPIC uses :? operator so compose errors clearly if unset - New vars: NTFY_TOKEN (private topics), NTFY_PRIORITY, TZ, APP_PORT, DEV_PORT - Removed duplicate NTFY_URL entry notifier/index.mjs: - Reads TZ from env (was hardcoded "America/Chicago") - Reads NTFY_PRIORITY from env (was hardcoded "default") - Adds Authorization: Bearer header when NTFY_TOKEN is set - Startup log shows auth status and full schedule config .env.example: mirrors every compose variable with descriptions Co-Authored-By: Claude Sonnet 4.6 --- .env.example | 43 ++++++++++++++++++++++++++++++++----------- docker-compose.yml | 44 +++++++++++++++++++++++++++++++++++++------- notifier/index.mjs | 34 +++++++++++++++++++++------------- 3 files changed, 90 insertions(+), 31 deletions(-) diff --git a/.env.example b/.env.example index 232be16..1619d34 100644 --- a/.env.example +++ b/.env.example @@ -1,17 +1,38 @@ -# Copy to .env and fill in your values +# Copy this file to .env and fill in your values. +# docker-compose reads .env automatically for variable substitution. -# ntfy topic — make it unguessable (anyone who knows it can receive/send) -# Subscribe at https://ntfy.sh/YOUR_TOPIC or in the ntfy app +# ── App ──────────────────────────────────────────────────────────────────────── +# Port the web app is exposed on (default: 8080) +APP_PORT=8080 + +# ── ntfy ────────────────────────────────────────────────────────────────────── +# Required — your ntfy topic. Keep it unguessable (anyone with the name can sub). +# Subscribe at https://ntfy.sh/YOUR_TOPIC or in the ntfy iOS/Android app. NTFY_TOPIC=your-unguessable-topic-here -# Optional: self-hosted ntfy server -# NTFY_URL=https://ntfy.sh +# ntfy server — leave as-is for ntfy.sh, or point to your self-hosted instance +NTFY_URL=https://ntfy.sh -# Optional: URL the notification links back to (your deployed app) -# APP_URL=http://localhost:8080 +# Bearer token for private/access-controlled topics (leave blank for public) +NTFY_TOKEN= -# Optional: cron schedule (default: 9 AM on the 1st of every month) -# CRON_SCHEDULE=0 9 1 * * +# Notification priority: min | low | default | high | urgent +NTFY_PRIORITY=default -# Set to "true" to get a test ping the moment the notifier starts -# NTFY_TEST_ON_START=false +# ── Schedule ────────────────────────────────────────────────────────────────── +# Cron schedule for the monthly reminder (default: 9 AM on the 1st of each month) +# Format: minute hour day-of-month month day-of-week +CRON_SCHEDULE=0 9 1 * * + +# IANA timezone for the cron schedule +TZ=America/Chicago + +# URL the notification deep-links back to +APP_URL=http://localhost:8080 + +# Set to "true" to fire a test notification the moment the notifier container starts +NTFY_TEST_ON_START=false + +# ── Dev ─────────────────────────────────────────────────────────────────────── +# Port for the Vite dev server (docker compose --profile dev up dev) +DEV_PORT=5173 diff --git a/docker-compose.yml b/docker-compose.yml index 08f0ba6..dc1c345 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,26 +1,54 @@ services: + + # ── Web app (nginx, port 8080) ────────────────────────────────────────────── app: build: . image: ghcr.io/nmemmert/letter:main ports: - - "8080:80" + - "${APP_PORT:-8080}:80" restart: unless-stopped + # ── Monthly ntfy reminder ─────────────────────────────────────────────────── notifier: build: ./notifier restart: unless-stopped + env_file: + - path: .env + required: false # container still starts without a .env file environment: - # Required: set your ntfy topic (keep it unguessable, e.g. "nate-letter-k7x2") - NTFY_TOPIC: ${NTFY_TOPIC} - # Optional overrides + # ── ntfy connection ─────────────────────────────────────────────────── + # Required: your ntfy topic — keep it unguessable, e.g. "nate-letter-k7x2" + # Subscribe at https://ntfy.sh/ or in the ntfy mobile app + NTFY_TOPIC: ${NTFY_TOPIC:?NTFY_TOPIC is required — set it in .env} + + # ntfy server (default: ntfy.sh; set to your self-hosted URL if applicable) NTFY_URL: ${NTFY_URL:-https://ntfy.sh} + + # Bearer token for access-controlled / private topics (leave blank for public) + NTFY_TOKEN: ${NTFY_TOKEN:-} + + # Notification priority: min | low | default | high | urgent + NTFY_PRIORITY: ${NTFY_PRIORITY:-default} + + # ── Schedule ────────────────────────────────────────────────────────── + # Cron expression — default fires at 9 AM on the 1st of every month + # Format: minute hour day-of-month month day-of-week + CRON_SCHEDULE: ${CRON_SCHEDULE:-0 9 1 * *} + + # IANA timezone for the cron schedule + TZ: ${TZ:-America/Chicago} + + # ── App ─────────────────────────────────────────────────────────────── + # URL the notification deep-links back to (shown as "Open" in ntfy) APP_URL: ${APP_URL:-http://localhost:8080} - CRON_SCHEDULE: ${CRON_SCHEDULE:-0 9 1 * *} # 9 AM on the 1st of every month - # Set to "true" to get a test ping immediately on container start + + # Set to "true" to fire a test notification immediately on container start NTFY_TEST_ON_START: ${NTFY_TEST_ON_START:-false} + depends_on: - app + # ── Local dev (Vite HMR, port 5173) — run with: docker compose --profile dev up dev dev: image: node:20-alpine working_dir: /app @@ -28,7 +56,9 @@ services: - .:/app - node_modules:/app/node_modules ports: - - "5173:5173" + - "${DEV_PORT:-5173}:5173" + environment: + NODE_ENV: development command: sh -c "npm install && npm run dev -- --host 0.0.0.0" profiles: [dev] diff --git a/notifier/index.mjs b/notifier/index.mjs index 4d41c5d..96f19a8 100644 --- a/notifier/index.mjs +++ b/notifier/index.mjs @@ -1,9 +1,12 @@ import cron from "node-cron"; -const NTFY_URL = process.env.NTFY_URL || "https://ntfy.sh"; -const NTFY_TOPIC = process.env.NTFY_TOPIC; -const CRON_SCHEDULE = process.env.CRON_SCHEDULE || "0 9 1 * *"; // 9 AM on the 1st of each month -const APP_URL = process.env.APP_URL || "http://localhost:8080"; +const NTFY_URL = process.env.NTFY_URL || "https://ntfy.sh"; +const NTFY_TOPIC = process.env.NTFY_TOPIC; +const NTFY_PRIORITY = process.env.NTFY_PRIORITY || "default"; +const NTFY_TOKEN = process.env.NTFY_TOKEN; // Bearer token for private topics +const CRON_SCHEDULE = process.env.CRON_SCHEDULE || "0 9 1 * *"; +const TZ = process.env.TZ || "America/Chicago"; +const APP_URL = process.env.APP_URL || "http://localhost:8080"; if (!NTFY_TOPIC) { console.error("NTFY_TOPIC is required. Set it in your environment or docker-compose."); @@ -26,15 +29,18 @@ async function sendReminder() { const message = `${monthName} ${year} is done. Take 10 minutes to capture the month before it fades. Open your letter app to answer this month's 6 prompts.`; try { + const headers = { + Title: title, + Priority: NTFY_PRIORITY, + Tags: "pencil,calendar", + Click: APP_URL, + "Content-Type": "text/plain", + }; + if (NTFY_TOKEN) headers["Authorization"] = `Bearer ${NTFY_TOKEN}`; + const res = await fetch(`${NTFY_URL}/${NTFY_TOPIC}`, { method: "POST", - headers: { - Title: title, - Priority: "default", - Tags: "pencil,calendar", - Click: APP_URL, - "Content-Type": "text/plain", - }, + headers, body: message, }); @@ -50,11 +56,13 @@ async function sendReminder() { console.log(`[${new Date().toISOString()}] Notifier started`); console.log(` Topic: ${NTFY_URL}/${NTFY_TOPIC}`); -console.log(` Schedule: ${CRON_SCHEDULE}`); +console.log(` Priority: ${NTFY_PRIORITY}`); +console.log(` Auth: ${NTFY_TOKEN ? "Bearer token set" : "none (public topic)"}`); +console.log(` Schedule: ${CRON_SCHEDULE} (${TZ})`); console.log(` App URL: ${APP_URL}`); // Run on schedule -cron.schedule(CRON_SCHEDULE, sendReminder, { timezone: "America/Chicago" }); +cron.schedule(CRON_SCHEDULE, sendReminder, { timezone: TZ }); // Send a startup test ping in dev mode if (process.env.NTFY_TEST_ON_START === "true") {