Skip to content

Commit a85ed60

Browse files
committed
refactor: move /admin/reseed to /test/reseed behind ENABLE_TEST_API env var
Test-only routes should not be available in production or dev. The reseed endpoint is now in controllers/test.js, conditionally mounted only when ENABLE_TEST_API=true. The env var is set in docker-compose for test runs.
1 parent 5fe3198 commit a85ed60

File tree

4 files changed

+33
-23
lines changed

4 files changed

+33
-23
lines changed

controllers/index.js

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
const express = require('express'),
22
jsonStore = require('../services/json-store'),
3-
mongodb = require('../services/mongodb'),
43
router = new express.Router();
54

65
router.use('/', require('./home'));
@@ -24,26 +23,8 @@ router.get('/subscriptions.json', (req, res) => {
2423
res.send(JSON.stringify(jsonStore.getData(), null, 2));
2524
});
2625

27-
router.post('/admin/reseed', async(req, res) => {
28-
try {
29-
jsonStore.clear();
30-
const db = mongodb.get('rsscloud');
31-
const resources = await db.collection('resources').find({}).toArray();
32-
const subscriptions = await db.collection('subscriptions').find({}).toArray();
33-
34-
for (const resource of resources) {
35-
jsonStore.setResource(resource._id, resource);
36-
}
37-
38-
for (const sub of subscriptions) {
39-
jsonStore.setSubscriptions(sub._id, sub.pleaseNotify || []);
40-
}
41-
42-
jsonStore.flush();
43-
res.json({ success: true });
44-
} catch (error) {
45-
res.status(500).json({ success: false, error: error.message });
46-
}
47-
});
26+
if (process.env.ENABLE_TEST_API === 'true') {
27+
router.use('/test', require('./test'));
28+
}
4829

4930
module.exports = router;

controllers/test.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
const express = require('express'),
2+
jsonStore = require('../services/json-store'),
3+
mongodb = require('../services/mongodb'),
4+
router = new express.Router();
5+
6+
router.post('/reseed', async(req, res) => {
7+
try {
8+
jsonStore.clear();
9+
const db = mongodb.get('rsscloud');
10+
const resources = await db.collection('resources').find({}).toArray();
11+
const subscriptions = await db.collection('subscriptions').find({}).toArray();
12+
13+
for (const resource of resources) {
14+
jsonStore.setResource(resource._id, resource);
15+
}
16+
17+
for (const sub of subscriptions) {
18+
jsonStore.setSubscriptions(sub._id, sub.pleaseNotify || []);
19+
}
20+
21+
jsonStore.flush();
22+
res.json({ success: true });
23+
} catch (error) {
24+
res.status(500).json({ success: false, error: error.message });
25+
}
26+
});
27+
28+
module.exports = router;

docker-compose.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ services:
1313
PORT: 5337
1414
MONGODB_URI: mongodb://mongodb:27017/rsscloud
1515
NODE_TLS_REJECT_UNAUTHORIZED: 0
16+
ENABLE_TEST_API: "true"
1617
expose:
1718
- 5337
1819
depends_on:

test/mongodb.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ async function upsertSubscriptions(subscriptions) {
2727
}
2828

2929
async function reseedServer() {
30-
await fetch(`${SERVER_URL}/admin/reseed`, { method: 'POST' });
30+
await fetch(`${SERVER_URL}/test/reseed`, { method: 'POST' });
3131
}
3232

3333
module.exports = {

0 commit comments

Comments
 (0)