forked from sebauer/if-this-then-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
65 lines (49 loc) · 1.87 KB
/
server.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
61
62
63
64
65
/**
* IFTTT to NodeJS
*
* Use this little Node app to have a node server running which can be accessed
* by IFTTT's WordPress action. I needed this to have an easy way of using IFTTT
* to communicate with my Raspberry PI.
*
* Credits for the idea of faking the Wordpress XML RPC API as customizable
* IFTTT backend go to https://github.com/captn3m0/ifttt-webhook
*
*/
var express = require('express');
var xmlparser = require('express-xml-bodyparser');
var bunyan = require('bunyan');
var helper = require('./modules/helper');
helper.printStartupHeader();
var pluginManager = require('./modules/plugin-manager');
var responseGenerator = require('./modules/response-generator');
var xmlRpcApiHandler = require('./modules/xml-rpc-api-handler');
var config = require('./config.js').getConfig();
var log = bunyan.createLogger({name: 'IFTTN'});
var app = express();
app.use(express.json());
app.use(express.urlencoded());
app.use(xmlparser());
helper.setLogger(log);
helper.checkDefaultCredentials();
pluginManager.setLogger(log);
pluginManager.loadPlugins();
xmlRpcApiHandler.setLogger(log);
xmlRpcApiHandler.setPluginManager(pluginManager);
// Middleware to log every request
app.use(function (req, res, next) {
log.info('%s from %s on %s', req.method, req.ip, req.path);
next();
});
app.get('/xmlrpc.php', function(req, res, next){
res.send('<a href="https://github.com/sebauer/if-this-then-node" target="_blank">IFTTN - if-this-then-node</a> Version '+helper.getVersion()+' is up and running!');
});
app.post('/xmlrpc.php', function(req, res, next){
log.info('XMLRPC API request received');
log.info(req.rawBody);
var methodName = req.body.methodcall.methodname[0];
log.info('Method Name: %s', methodName);
xmlRpcApiHandler.handleMethod(methodName, req, res);
});
var server = app.listen(1337, function() {
log.info('Listening on port %d', server.address().port);
});