2026-03-22 19:28:57 +08:00
|
|
|
/**
|
|
|
|
|
* Application settings routes (key-value store).
|
|
|
|
|
*
|
|
|
|
|
* GET /api/v1/settings — Get all settings as a key-value object
|
|
|
|
|
* PUT /api/v1/settings — Save settings (admin only)
|
|
|
|
|
* GET /api/v1/settings/:key — Get a specific setting
|
|
|
|
|
*/
|
2026-03-25 09:27:12 +08:00
|
|
|
|
2026-03-22 19:28:57 +08:00
|
|
|
import { eq } from "drizzle-orm";
|
2026-03-25 09:27:12 +08:00
|
|
|
import type { FastifyInstance, FastifyReply, FastifyRequest } from "fastify";
|
2026-04-23 20:26:58 +08:00
|
|
|
import { z } from "zod";
|
2026-03-22 19:28:57 +08:00
|
|
|
import { db, schema } from "../db/index.js";
|
2026-04-22 18:10:04 +08:00
|
|
|
import { requirePermission } from "../permissions.js";
|
|
|
|
|
import { requireAuth } from "../plugins/auth.js";
|
2026-03-22 19:28:57 +08:00
|
|
|
|
2026-04-23 20:26:58 +08:00
|
|
|
const settingsBodySchema = z.record(z.string().min(1), z.unknown());
|
|
|
|
|
|
2026-03-28 18:54:26 +08:00
|
|
|
const HTML_TAG_PATTERN = /<[a-z/!][^>]*>/i;
|
|
|
|
|
|
2026-03-22 19:28:57 +08:00
|
|
|
export async function settingsRoutes(app: FastifyInstance): Promise<void> {
|
|
|
|
|
// GET /api/v1/settings — Get all settings as a key-value object
|
2026-03-25 09:27:12 +08:00
|
|
|
app.get("/api/v1/settings", async (request: FastifyRequest, reply: FastifyReply) => {
|
|
|
|
|
const user = requireAuth(request, reply);
|
|
|
|
|
if (!user) return;
|
2026-03-22 19:28:57 +08:00
|
|
|
|
2026-03-25 09:27:12 +08:00
|
|
|
const rows = db.select().from(schema.settings).all();
|
2026-03-22 19:28:57 +08:00
|
|
|
|
2026-03-25 09:27:12 +08:00
|
|
|
const settings: Record<string, string> = {};
|
|
|
|
|
for (const row of rows) {
|
|
|
|
|
settings[row.key] = row.value;
|
|
|
|
|
}
|
2026-03-22 19:28:57 +08:00
|
|
|
|
2026-04-10 17:38:54 +08:00
|
|
|
return reply.send({ settings });
|
2026-03-25 09:27:12 +08:00
|
|
|
});
|
2026-03-22 19:28:57 +08:00
|
|
|
|
|
|
|
|
// PUT /api/v1/settings — Save settings (admin only)
|
2026-03-25 09:27:12 +08:00
|
|
|
app.put("/api/v1/settings", async (request: FastifyRequest, reply: FastifyReply) => {
|
2026-04-22 18:10:04 +08:00
|
|
|
const admin = requirePermission("settings:write")(request, reply);
|
2026-03-25 09:27:12 +08:00
|
|
|
if (!admin) return;
|
2026-03-22 19:28:57 +08:00
|
|
|
|
2026-04-23 20:26:58 +08:00
|
|
|
const parsed = settingsBodySchema.safeParse(request.body);
|
|
|
|
|
if (!parsed.success) {
|
2026-03-25 09:27:12 +08:00
|
|
|
return reply.status(400).send({
|
|
|
|
|
error: "Request body must be a JSON object with key-value pairs",
|
|
|
|
|
code: "VALIDATION_ERROR",
|
|
|
|
|
});
|
|
|
|
|
}
|
2026-04-23 20:26:58 +08:00
|
|
|
const body = parsed.data;
|
2026-03-22 19:28:57 +08:00
|
|
|
|
2026-03-28 18:58:25 +08:00
|
|
|
// Pass 1: validate all entries before writing any
|
|
|
|
|
const entries: Array<{ key: string; strValue: string }> = [];
|
2026-03-22 19:28:57 +08:00
|
|
|
|
2026-03-25 09:27:12 +08:00
|
|
|
for (const [key, value] of Object.entries(body)) {
|
|
|
|
|
if (typeof key !== "string" || key.length === 0) continue;
|
2026-03-22 19:28:57 +08:00
|
|
|
|
2026-03-25 09:27:12 +08:00
|
|
|
const strValue = typeof value === "string" ? value : JSON.stringify(value);
|
2026-03-22 19:28:57 +08:00
|
|
|
|
2026-03-28 18:54:26 +08:00
|
|
|
if (HTML_TAG_PATTERN.test(key) || HTML_TAG_PATTERN.test(strValue)) {
|
|
|
|
|
return reply.status(400).send({
|
|
|
|
|
error: "Settings keys and values must not contain HTML tags",
|
|
|
|
|
code: "VALIDATION_ERROR",
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-28 18:58:25 +08:00
|
|
|
entries.push({ key, strValue });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Pass 2: write all entries now that all have passed validation
|
|
|
|
|
const now = new Date();
|
|
|
|
|
|
|
|
|
|
for (const { key, strValue } of entries) {
|
2026-03-25 09:27:12 +08:00
|
|
|
// Upsert: insert or update on conflict
|
|
|
|
|
const existing = db.select().from(schema.settings).where(eq(schema.settings.key, key)).get();
|
|
|
|
|
|
|
|
|
|
if (existing) {
|
|
|
|
|
db.update(schema.settings)
|
|
|
|
|
.set({ value: strValue, updatedAt: now })
|
2026-03-22 19:28:57 +08:00
|
|
|
.where(eq(schema.settings.key, key))
|
2026-03-25 09:27:12 +08:00
|
|
|
.run();
|
|
|
|
|
} else {
|
|
|
|
|
db.insert(schema.settings).values({ key, value: strValue }).run();
|
2026-03-22 19:28:57 +08:00
|
|
|
}
|
2026-03-25 09:27:12 +08:00
|
|
|
}
|
|
|
|
|
|
2026-03-28 18:58:25 +08:00
|
|
|
return reply.send({ ok: true, updatedCount: entries.length });
|
2026-03-25 09:27:12 +08:00
|
|
|
});
|
2026-03-22 19:28:57 +08:00
|
|
|
|
|
|
|
|
// GET /api/v1/settings/:key — Get a specific setting
|
|
|
|
|
app.get(
|
|
|
|
|
"/api/v1/settings/:key",
|
2026-03-25 09:27:12 +08:00
|
|
|
async (request: FastifyRequest<{ Params: { key: string } }>, reply: FastifyReply) => {
|
2026-03-22 19:28:57 +08:00
|
|
|
const user = requireAuth(request, reply);
|
|
|
|
|
if (!user) return;
|
|
|
|
|
|
|
|
|
|
const { key } = request.params;
|
|
|
|
|
|
2026-03-25 09:27:12 +08:00
|
|
|
const row = db.select().from(schema.settings).where(eq(schema.settings.key, key)).get();
|
2026-03-22 19:28:57 +08:00
|
|
|
|
|
|
|
|
if (!row) {
|
|
|
|
|
return reply.status(404).send({
|
|
|
|
|
error: `Setting "${key}" not found`,
|
|
|
|
|
code: "NOT_FOUND",
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return reply.send({
|
|
|
|
|
key: row.key,
|
|
|
|
|
value: row.value,
|
|
|
|
|
updatedAt: row.updatedAt.toISOString(),
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
app.log.info("Settings routes registered");
|
|
|
|
|
}
|