2025-12-11 10:40:55 +01:00
|
|
|
/*
|
2026-01-12 15:00:36 +01:00
|
|
|
* Copyright (c) 2026 by Christian Kellner.
|
2025-12-11 10:40:55 +01:00
|
|
|
* Licensed under Apache-2.0 with Commons Clause and Attribution/Naming Clause
|
|
|
|
|
*/
|
|
|
|
|
|
2023-03-13 13:42:43 +01:00
|
|
|
import restana from 'restana';
|
2025-12-09 13:56:46 +01:00
|
|
|
import { getDirName } from '../../utils.js';
|
2023-03-13 13:42:43 +01:00
|
|
|
import fs from 'fs';
|
2025-09-18 15:38:23 +02:00
|
|
|
import { ensureDemoUserExists } from '../../services/storage/userStorage.js';
|
2025-09-13 18:57:56 +02:00
|
|
|
import logger from '../../services/logger.js';
|
2025-12-09 13:56:46 +01:00
|
|
|
import { getSettings, upsertSettings } from '../../services/storage/settingsStorage.js';
|
2023-03-13 13:42:43 +01:00
|
|
|
const service = restana();
|
2021-05-30 09:37:45 +02:00
|
|
|
const generalSettingsRouter = service.newRouter();
|
2025-12-09 13:56:46 +01:00
|
|
|
|
2021-05-30 09:37:45 +02:00
|
|
|
generalSettingsRouter.get('/', async (req, res) => {
|
2025-12-09 13:56:46 +01:00
|
|
|
res.body = Object.assign({}, await getSettings());
|
2021-05-30 09:37:45 +02:00
|
|
|
res.send();
|
|
|
|
|
});
|
|
|
|
|
generalSettingsRouter.post('/', async (req, res) => {
|
2025-12-09 13:56:46 +01:00
|
|
|
const { sqlitepath, ...appSettings } = req.body || {};
|
|
|
|
|
const localSettings = await getSettings();
|
|
|
|
|
|
|
|
|
|
if (localSettings.demoMode) {
|
|
|
|
|
res.send(new Error('In demo mode, it is not allowed to change these settings.'));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-30 09:37:45 +02:00
|
|
|
try {
|
2025-12-09 13:56:46 +01:00
|
|
|
if (typeof sqlitepath !== 'undefined') {
|
|
|
|
|
fs.writeFileSync(`${getDirName()}/../conf/config.json`, JSON.stringify({ sqlitepath }));
|
2024-11-22 09:11:10 +01:00
|
|
|
}
|
2025-12-09 13:56:46 +01:00
|
|
|
upsertSettings(appSettings);
|
2025-09-18 15:38:23 +02:00
|
|
|
ensureDemoUserExists();
|
2021-05-30 09:37:45 +02:00
|
|
|
} catch (err) {
|
2025-09-13 18:57:56 +02:00
|
|
|
logger.error(err);
|
2021-05-30 09:37:45 +02:00
|
|
|
res.send(new Error('Error while trying to write settings.'));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
res.send();
|
|
|
|
|
});
|
2023-03-13 13:42:43 +01:00
|
|
|
export { generalSettingsRouter };
|