68 lines
1.9 KiB
JavaScript
68 lines
1.9 KiB
JavaScript
const express = require('express');
|
|
const router = express.Router();
|
|
const settings = require('../settings');
|
|
const { notify } = require('../notify');
|
|
|
|
// GET /api/settings
|
|
router.get('/', (req, res) => {
|
|
res.json(settings.get());
|
|
});
|
|
|
|
// PUT /api/settings
|
|
router.put('/', (req, res) => {
|
|
try {
|
|
const updated = settings.update(req.body);
|
|
res.json(updated);
|
|
} catch (err) {
|
|
res.status(500).json({ error: err.message });
|
|
}
|
|
});
|
|
|
|
// POST /api/settings/test-notification
|
|
router.post('/test-notification', async (req, res) => {
|
|
// Use the payload from the request body so the user can test
|
|
// before saving (the frontend sends the current form values)
|
|
const { url, token, priority } = req.body;
|
|
|
|
if (!url || !token) {
|
|
return res.status(400).json({ error: 'URL and token are required' });
|
|
}
|
|
|
|
const base = url.replace(/\/$/, '');
|
|
try {
|
|
const response = await fetch(`${base}/message?token=${encodeURIComponent(token)}`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({
|
|
title: '🦥 Sloth Manager — Test',
|
|
message: 'Gotify notifications are working correctly.',
|
|
priority: priority ?? 5,
|
|
}),
|
|
});
|
|
|
|
if (!response.ok) {
|
|
const text = await response.text().catch(() => '');
|
|
return res.status(502).json({ error: `Gotify returned ${response.status}: ${text}` });
|
|
}
|
|
|
|
res.json({ success: true });
|
|
} catch (err) {
|
|
res.status(502).json({ error: `Could not reach Gotify: ${err.message}` });
|
|
}
|
|
});
|
|
|
|
// POST /api/settings/clear-cache
|
|
router.post('/clear-cache', (req, res) => {
|
|
try {
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
const cachePath = process.env.DB_PATH || path.join(__dirname, '../../dns-cache.json');
|
|
fs.writeFileSync(cachePath, '{}', 'utf8');
|
|
res.json({ success: true });
|
|
} catch (err) {
|
|
res.status(500).json({ error: err.message });
|
|
}
|
|
});
|
|
|
|
module.exports = router;
|