Spaces:
Running
Running
File size: 1,321 Bytes
e4f1db2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
const express = require('express');
const fs = require('fs');
const path = require('path');
const router = express.Router();
const ALGORITHMS_PATH = path.join(__dirname, '../../data/algorithms.json');
router.get('/', (req, res) => {
try {
const data = fs.readFileSync(ALGORITHMS_PATH, 'utf8');
const algorithms = JSON.parse(data);
res.json(algorithms);
} catch (error) {
res.status(500).json({ error: 'Failed to load algorithms' });
}
});
router.put('/', (req, res) => {
try {
const updatedAlgorithms = req.body;
fs.writeFileSync(ALGORITHMS_PATH, JSON.stringify(updatedAlgorithms, null, 2));
res.json({ message: 'Algorithms updated successfully' });
} catch (error) {
res.status(500).json({ error: 'Failed to update algorithms' });
}
});
router.get('/categories', (req, res) => {
try {
const data = fs.readFileSync(ALGORITHMS_PATH, 'utf8');
const algorithms = JSON.parse(data);
const categories = {};
Object.values(algorithms.algorithms).forEach(algo => {
if (!categories[algo.category]) {
categories[algo.category] = [];
}
categories[algo.category].push(algo.name);
});
res.json(categories);
} catch (error) {
res.status(500).json({ error: 'Failed to get categories' });
}
});
module.exports = router; |