Add configurable library path with settings API and UI
This commit is contained in:
64
src/routes/settings.js
Normal file
64
src/routes/settings.js
Normal file
@@ -0,0 +1,64 @@
|
||||
import express from 'express';
|
||||
import fs from 'fs/promises';
|
||||
import path from 'path';
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
const CONFIG_FILE = path.join(process.cwd(), 'config.json');
|
||||
|
||||
// Default config
|
||||
const DEFAULT_CONFIG = {
|
||||
libraryPath: './audio_library',
|
||||
port: 3000
|
||||
};
|
||||
|
||||
// Load config
|
||||
async function loadConfig() {
|
||||
try {
|
||||
const data = await fs.readFile(CONFIG_FILE, 'utf-8');
|
||||
return { ...DEFAULT_CONFIG, ...JSON.parse(data) };
|
||||
} catch (error) {
|
||||
return DEFAULT_CONFIG;
|
||||
}
|
||||
}
|
||||
|
||||
// Save config
|
||||
async function saveConfig(config) {
|
||||
await fs.writeFile(CONFIG_FILE, JSON.stringify(config, null, 2));
|
||||
}
|
||||
|
||||
// Get settings
|
||||
router.get('/', async (req, res) => {
|
||||
try {
|
||||
const config = await loadConfig();
|
||||
res.json(config);
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
// Update settings
|
||||
router.patch('/', async (req, res) => {
|
||||
try {
|
||||
const config = await loadConfig();
|
||||
const { libraryPath } = req.body;
|
||||
|
||||
if (libraryPath) {
|
||||
// Validate path exists
|
||||
try {
|
||||
await fs.access(libraryPath);
|
||||
config.libraryPath = libraryPath;
|
||||
} catch (error) {
|
||||
return res.status(400).json({ error: 'Library path does not exist' });
|
||||
}
|
||||
}
|
||||
|
||||
await saveConfig(config);
|
||||
res.json({ success: true, config });
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
export default router;
|
||||
export { loadConfig };
|
||||
Reference in New Issue
Block a user