build app logo
This commit is contained in:
@@ -43,6 +43,11 @@ db.exec(`
|
||||
name TEXT UNIQUE NOT NULL COLLATE NOCASE
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS settings (
|
||||
key TEXT PRIMARY KEY,
|
||||
value TEXT
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS meme_tags (
|
||||
meme_id TEXT NOT NULL REFERENCES memes(id) ON DELETE CASCADE,
|
||||
tag_id INTEGER NOT NULL REFERENCES tags(id) ON DELETE CASCADE,
|
||||
|
||||
@@ -10,6 +10,7 @@ import { authRoutes } from './routes/auth.js';
|
||||
import { collectionsRoutes } from './routes/collections.js';
|
||||
import { adminRoutes } from './routes/admin.js';
|
||||
import { shareRoutes } from './routes/share.js';
|
||||
import { settingsRoutes } from './routes/settings.js';
|
||||
|
||||
// Ensure data dirs exist
|
||||
ensureImagesDir();
|
||||
@@ -46,6 +47,7 @@ await app.register(tagsRoutes);
|
||||
await app.register(adminRoutes);
|
||||
|
||||
await app.register(shareRoutes);
|
||||
await app.register(settingsRoutes);
|
||||
|
||||
// SPA fallback — serve index.html for all non-API, non-image routes
|
||||
app.setNotFoundHandler(async (req, reply) => {
|
||||
|
||||
38
backend/src/routes/settings.ts
Normal file
38
backend/src/routes/settings.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
import type { FastifyInstance } from 'fastify';
|
||||
import db from '../db.js';
|
||||
import { requireAuth } from '../auth.js';
|
||||
|
||||
type SettingsRow = { key: string; value: string };
|
||||
|
||||
function getAllSettings(): Record<string, string> {
|
||||
const rows = db.prepare('SELECT key, value FROM settings').all() as SettingsRow[];
|
||||
return Object.fromEntries(rows.map((r) => [r.key, r.value]));
|
||||
}
|
||||
|
||||
export async function settingsRoutes(app: FastifyInstance) {
|
||||
// Public — anyone can read settings (needed to render logo for guests)
|
||||
app.get('/api/settings', async () => {
|
||||
return getAllSettings();
|
||||
});
|
||||
|
||||
// Admin — update one or more settings keys
|
||||
app.put<{ Body: Record<string, string> }>(
|
||||
'/api/settings',
|
||||
{ preHandler: requireAuth },
|
||||
async (req) => {
|
||||
const allowed = new Set(['logo_url']);
|
||||
const stmt = db.prepare(
|
||||
'INSERT INTO settings (key, value) VALUES (?, ?) ON CONFLICT(key) DO UPDATE SET value = excluded.value'
|
||||
);
|
||||
for (const [key, value] of Object.entries(req.body)) {
|
||||
if (!allowed.has(key)) continue;
|
||||
if (value === '' || value == null) {
|
||||
db.prepare('DELETE FROM settings WHERE key = ?').run(key);
|
||||
} else {
|
||||
stmt.run(key, String(value));
|
||||
}
|
||||
}
|
||||
return getAllSettings();
|
||||
}
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user