Skip to content

Commit 4b5fa4a

Browse files
committed
Implementing logs, stubbing xmlrpc
1 parent 58edd7b commit 4b5fa4a

25 files changed

+321
-131
lines changed

.env-sample

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
DOMAIN=localhost
22
PORT=5337
3+
MONGODB_URI=mongodb://localhost:27017/rsscloud

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@ npm start
1616

1717
### POST /pleaseNotify
1818

19-
Posting to /pleaseNotify is your way of alerting the server that you want to receive notifications when one or more resources are updated.
19+
Posting to /pleaseNotify is your way of alerting the server that you want to receive notifications when one or more resources are updated.
2020

2121
The POST parameters are:
2222

2323
1. domain -- optional, if omitted the requesting IP address is used
2424
2. port
2525
3. path
26-
4. registerProcedure -- required, but isn't used in this server as it only applies to xml-rpc or soap
27-
5. protocol -- the spec allows for http-post, xml-rpc or soap but this server only supports http-post
26+
4. registerProcedure -- required, but isn't used in this server as it only applies to xml-rpc or soap.
27+
5. protocol -- the spec allows for http-post, xml-rpc or soap but this server only supports http-post. This server also supports https-post which is identical to http-post except it notifies using https as the scheme instead of http.
2828
6. url1, url2, ..., urlN this is the resource you're requesting to be notified about. In the case of an RSS feed you would specify the URL of the RSS feed.
2929

3030
When you POST the server first checks if the urls you specifed are returning an [HTTP 2xx status code](http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2) then it attempts to notify the subscriber of an update to make sure it works. This is done in one of two ways.
@@ -52,7 +52,7 @@ Examples:
5252

5353
### POST /ping
5454

55-
Posting to /ping is your way of alerting the server that a resource has been updated.
55+
Posting to /ping is your way of alerting the server that a resource has been updated.
5656

5757
The POST parameters are:
5858

app.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
moment = require('moment'),
1212
mongodb = require('./services/mongodb'),
1313
morgan = require('morgan'),
14-
Promise = require('bluebird'),
1514
removeExpiredSubscriptions = require('./services/remove-expired-subscriptions');
1615

1716
let app,
@@ -60,7 +59,7 @@
6059
app.use(require('./controllers'));
6160

6261
// Start server
63-
mongodb.connect(config.mongodbUri)
62+
mongodb.connect('rsscloud', config.mongodbUri)
6463
.then(() => {
6564
server = app.listen(config.port, function () {
6665
app.locals.host = config.domain;

bin/import-data.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const config = require('../config'),
33
mongodb = require('../services/mongodb');
44

55
async function doImport() {
6-
const db = await mongodb.connect(config.mongodbUri);
6+
const db = await mongodb.connect('rsscloud', config.mongodbUri);
77

88
if (fs.existsSync('./data/data.json')) {
99
const data = JSON.parse(fs.readFileSync('./data/data.json', 'utf8'));
@@ -22,19 +22,27 @@ async function doImport() {
2222

2323
await db.collection('subscriptions').bulkWrite(
2424
Object.keys(data.subscriptions).map(id => {
25+
const subscriptions = {
26+
_id: id,
27+
pleaseNotify: Object.keys(data.subscriptions[id]).map(sid => {
28+
const subscription = data.subscriptions[id][sid];
29+
subscription.url = sid;
30+
return subscription;
31+
})
32+
}
2533
return {
2634
replaceOne: {
2735
filter: { _id: id },
28-
replacement: data.subscriptions[id],
36+
replacement: subscriptions,
2937
upsert: true
3038
}
3139
};
3240
})
3341
);
3442

35-
await mongodb.close();
43+
await mongodb.close('rsscloud');
3644
} else {
37-
await mongodb.close();
45+
await mongodb.close('rsscloud');
3846

3947
throw new Error('Cannot find ./data/data.json');
4048
}

controllers/docs.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
(function () {
2+
"use strict";
3+
4+
const express = require('express'),
5+
router = new express.Router(),
6+
md = require('markdown-it')(),
7+
fs = require('fs');
8+
9+
router.get('/', function (req, res) {
10+
switch (req.accepts('html')) {
11+
case 'html':
12+
const vals = {
13+
htmltext: md.render(fs.readFileSync('README.md', { encoding: 'utf8' }))
14+
};
15+
res.render('docs', vals);
16+
break;
17+
default:
18+
res.status(406).send('Not Acceptable');
19+
break;
20+
}
21+
});
22+
23+
module.exports = router;
24+
}());

controllers/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@
55
router = new express.Router();
66

77
router.use('/', require('./home'));
8+
router.use('/docs', require('./docs'));
89
router.use('/pleaseNotify', require('./please-notify'));
10+
router.use('/pleaseNotifyForm', require('./please-notify-form'));
911
router.use('/ping', require('./ping'));
1012
router.use('/pingForm', require('./ping-form'));
11-
// router.use('/viewLog', require('./view-log'));
13+
router.use('/viewLog', require('./view-log'));
14+
router.use('/RPC2', require('./rpc2'));
1215

1316
module.exports = router;
1417
}());

controllers/please-notify-form.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
(function () {
2+
"use strict";
3+
4+
const express = require('express'),
5+
router = new express.Router();
6+
7+
router.get('/', function (req, res) {
8+
switch (req.accepts('html')) {
9+
case 'html':
10+
res.render('please-notify-form');
11+
break;
12+
default:
13+
res.status(406).send('Not Acceptable');
14+
break;
15+
}
16+
});
17+
18+
module.exports = router;
19+
}());

controllers/rpc2.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
(function () {
2+
"use strict";
3+
4+
const express = require('express'),
5+
parseRpcParams = require('../services/parse-rpc-params'),
6+
router = new express.Router(),
7+
rpcReturnSuccess = require('../services/rpc-return-success');
8+
9+
function processResponse(req, res, result) {
10+
switch (req.accepts('xml')) {
11+
case 'xml':
12+
res.set('Content-Type', 'text/xml');
13+
res.send(rpcReturnSuccess());
14+
break;
15+
default:
16+
res.status(406).send('Not Acceptable');
17+
break;
18+
}
19+
}
20+
21+
router.post('/', function (req, res) {
22+
const params = parseRpcParams(req);
23+
console.dir(params);
24+
processResponse(req, res, params);
25+
});
26+
27+
module.exports = router;
28+
}());

controllers/view-log.js

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,34 @@
11
(function () {
22
"use strict";
33

4-
var async = require('async'),
5-
data = require('../services/data'),
6-
errorResult = require('../services/error-result'),
4+
const errorResult = require('../services/error-result'),
75
express = require('express'),
86
logEmitter = require('../services/log-emitter'),
7+
mongodb = require('../services/mongodb'),
98
router = new express.Router();
109

11-
function fetchVals(db, callback) {
12-
var vals = {
10+
async function fetchVals(db, callback) {
11+
const vals = {
1312
'eventlog': []
1413
};
1514

16-
db.serialize(() => {
17-
db.each("SELECT * FROM log_events ORDER BY time DESC LIMIT 1000", (err, row) => {
18-
row.headers = JSON.parse(row.headers);
19-
vals.eventlog.push(row);
20-
}, () => {
21-
callback(null, vals);
22-
});
15+
const res = await mongodb.get('rsscloud')
16+
.collection('events')
17+
.find()
18+
.sort({ time: -1 })
19+
.limit(1000)
20+
.toArray();
21+
22+
vals.eventlog = res.map(item => {
23+
item.id = item._id.toHexString();
24+
delete item._id;
25+
26+
item.headers = JSON.parse(item.headers);
27+
28+
return item;
2329
});
30+
31+
return vals;
2432
}
2533

2634
function processResponse(req, res, vals) {
@@ -39,23 +47,14 @@
3947
}
4048

4149
function handleError(req, res, errorMessage) {
42-
processResponse(req, res, errorResult(errorMessage));
50+
console.error(err);
51+
processResponse(req, res, errorResult(err.message));
4352
}
4453

4554
router.get('/', function (req, res) {
46-
async.waterfall([
47-
(callback) => {
48-
data.getDb(callback);
49-
},
50-
(db, callback) => {
51-
fetchVals(db, callback);
52-
},
53-
(vals) => {
54-
processResponse(req, res, vals);
55-
}
56-
], (errorMessage) => {
57-
handleError(req, res, errorMessage);
58-
});
55+
fetchVals()
56+
.then(vals => processResponse(req, res, vals))
57+
.catch(err => handleError(req, res, err));
5958
});
6059

6160
router.ws('/', (ws, req) => {

package-lock.json

Lines changed: 38 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)