Add instance settings and mail delivery through PocketBase

Superadmins can now change signup rules (on/off and the domain allow-list)
and branding at runtime; the environment variables only seed the defaults.
Mail and S3 storage settings pass through to PocketBase's own settings, and
docwell-server sends its own emails through a PocketBase hook so there is a
single SMTP configuration. The boot hook no longer wipes SMTP settings that
were not set from the environment.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
tajniak81andClaude Opus 5.5 committed 2026-10-31 16:22:53 +01:00
1 parent ba394b8fac
commit e33a99241e
21 files changed
+1714 -25

No files matched your search

+49 -12
View File
@@ -16,27 +16,64 @@ routerAdd(
$apis.requireAuth("users"),
);
// Instance settings come from the environment and are applied on every start,
// so changing an env var and restarting is enough.
// Sends one email through PocketBase's SMTP settings, so docwell-server's own
// emails (invites, test mails) use the same mail configuration as the auth
// emails PocketBase sends itself.
routerAdd(
"POST",
"/api/docwell/mail",
(e) => {
const body = e.requestInfo().body;
const to = String(body.to || "").trim();
const subject = String(body.subject || "").trim();
if (!to || !subject) {
throw new BadRequestError("to and subject are required");
}
const meta = e.app.settings().meta;
const message = new MailerMessage({
from: { address: meta.senderAddress, name: meta.senderName },
to: [{ address: to }],
subject: subject,
html: String(body.html || ""),
text: String(body.text || ""),
});
e.app.newMailClient().send(message);
return e.noContent(204);
},
$apis.requireSuperuserAuth(),
);
// Settings that come from the environment are applied on every start, so
// changing an env var and restarting is enough. Settings without an env var
// are left alone: the admin panel manages them.
onBootstrap((e) => {
e.next();
const env = (key, def) => $os.getenv(key) || def;
const env = (key) => $os.getenv(key) || "";
const settings = e.app.settings();
settings.meta.appName = env("DOCWELL_APP_NAME", "Docwell");
settings.meta.appURL = env("DOCWELL_WEB_URL", "http://localhost:3000");
if (env("DOCWELL_APP_NAME")) {
settings.meta.appName = env("DOCWELL_APP_NAME");
} else if (settings.meta.appName === "Acme") {
// PocketBase's placeholder name on a fresh database.
settings.meta.appName = "Docwell";
}
settings.meta.appURL = env("DOCWELL_WEB_URL") || "http://localhost:3000";
settings.meta.senderName = settings.meta.appName;
settings.meta.senderAddress = env("DOCWELL_MAIL_FROM", "no-reply@docwell.localhost");
if (env("DOCWELL_MAIL_FROM")) {
settings.meta.senderAddress = env("DOCWELL_MAIL_FROM");
} else if (settings.meta.senderAddress === "support@example.com") {
settings.meta.senderAddress = "no-reply@docwell.localhost";
}
const smtpHost = env("DOCWELL_SMTP_HOST", "");
settings.smtp.enabled = smtpHost !== "";
const smtpHost = env("DOCWELL_SMTP_HOST");
if (smtpHost) {
settings.smtp.enabled = true;
settings.smtp.host = smtpHost;
settings.smtp.port = parseInt(env("DOCWELL_SMTP_PORT", "587"), 10);
settings.smtp.username = env("DOCWELL_SMTP_USERNAME", "");
settings.smtp.password = env("DOCWELL_SMTP_PASSWORD", "");
settings.smtp.tls = env("DOCWELL_SMTP_TLS", "false") === "true";
settings.smtp.port = parseInt(env("DOCWELL_SMTP_PORT") || "587", 10);
settings.smtp.username = env("DOCWELL_SMTP_USERNAME");
settings.smtp.password = env("DOCWELL_SMTP_PASSWORD");
settings.smtp.tls = env("DOCWELL_SMTP_TLS") === "true";
}
e.app.save(settings);
@@ -0,0 +1,29 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
/// <reference path="../pb_data/types.d.ts" />
// Instance settings the admin panel edits (signup rules, branding), one record
// per key. Mail and file storage settings live in PocketBase's own settings,
// which docwell-server patches directly.
migrate(
(app) => {
const settings = new Collection({
type: "base",
name: "instance_settings",
fields: [
{ name: "key", type: "text", required: true, max: 64, pattern: "^[a-z0-9_]+$" },
{ name: "value", type: "json", maxSize: 1048576 },
{ name: "created", type: "autodate", onCreate: true, onUpdate: false },
{ name: "updated", type: "autodate", onCreate: true, onUpdate: true },
],
indexes: ["CREATE UNIQUE INDEX idx_instance_settings_key ON instance_settings (key)"],
});
app.save(settings);
},
(app) => {
try {
app.delete(app.findCollectionByNameOrId("instance_settings"));
} catch (_) {}
},
);