vidrip/backend/src/routes/config.ts

79 lines
1.9 KiB
TypeScript

import { Router } from 'express';
import { configOperations } from '../db/database';
import { restartScheduler, getSchedulerStatus } from '../services/scheduler';
import { testConnection } from '../services/webdav';
const router = Router();
// Get all config
router.get('/', (req, res) => {
try {
const config = configOperations.getAll();
res.json(config);
} catch (error) {
res.status(500).json({ error: 'Failed to fetch config' });
}
});
// Update config
router.patch('/', (req, res) => {
try {
const updates = req.body;
for (const [key, value] of Object.entries(updates)) {
if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
configOperations.set(key, String(value));
}
}
// Restart scheduler if config changed
restartScheduler();
const config = configOperations.getAll();
res.json(config);
} catch (error) {
res.status(500).json({ error: 'Failed to update config' });
}
});
// Get scheduler status
router.get('/scheduler/status', (req, res) => {
try {
const status = getSchedulerStatus();
res.json(status);
} catch (error) {
res.status(500).json({ error: 'Failed to get scheduler status' });
}
});
// Test WebDAV connection
router.post('/webdav/test', async (req, res) => {
try {
const { url, username, password, path } = req.body;
if (!url || !username || !password) {
return res.status(400).json({
success: false,
message: 'URL, username, and password are required'
});
}
const result = await testConnection({
url,
username,
password,
path: path || '/vidrip/'
});
res.json(result);
} catch (error) {
res.status(500).json({
success: false,
message: error instanceof Error ? error.message : 'Failed to test connection'
});
}
});
export default router;