generated from solacecommunity/template-repo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlocalFeedsServer.js
60 lines (52 loc) · 1.94 KB
/
localFeedsServer.js
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
48
49
50
51
52
53
54
55
56
57
58
59
60
// server.js
const express = require('express');
const cors = require('cors');
const fs = require('fs');
const path = require('path');
const app = express();
const PORT = 8081;
app.use(cors());
const feedsPath = process.env.STM_HOME
? path.resolve(process.env.STM_HOME)
: path.resolve(process.env.HOME, '.stm/feeds');
app.use(cors()); // Enable CORS
app.get('/feeds', (req, res) => {
fs.readdir(feedsPath, { withFileTypes: true }, (err, entries) => {
if (err) {
res.status(500).json({ error: 'Failed to read directory' });
return;
}
const feedPromises = entries
.filter((entry) => entry.isDirectory())
.map((dir) => {
const feedInfoPath = path.join(feedsPath, dir.name, 'feedinfo.json');
const feedRulesPath = path.join(feedsPath, dir.name, 'feedrules.json');
return new Promise((resolve) => {
// Read feedinfo.json and feedrules.json in parallel
Promise.all([
fs.promises.readFile(feedInfoPath, 'utf8').catch(() => null),
fs.promises.readFile(feedRulesPath, 'utf8').catch(() => null),
]).then(([feedInfoData, feedRulesData]) => {
if (feedInfoData || feedRulesData) {
try {
const feedInfo = feedInfoData ? JSON.parse(feedInfoData) : null;
const feedRules = feedRulesData ? JSON.parse(feedRulesData) : null;
resolve({ directory: dir.name, feedinfo: feedInfo, feedrules: feedRules });
} catch (parseError) {
resolve(null); // Ignore parsing errors
}
} else {
resolve(null); // No data found for this directory
}
});
});
});
Promise.all(feedPromises).then((feeds) => {
const validFeeds = feeds.filter((feed) => feed !== null);
res.json(validFeeds);
});
});
});
app.listen(PORT, () => {
console.log(`Server is running on http://127.0.0.1:${PORT}`);
});