-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.js
More file actions
68 lines (62 loc) · 1.84 KB
/
lib.js
File metadata and controls
68 lines (62 loc) · 1.84 KB
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
66
67
68
/* global process */
var express = require('express');
var bodyParser = require('body-parser');
var https = require('https');
var rpmUtil = require('integration-common/util');
process.env.NODE_TLS_REJECT_UNAUTHORIZED = 0;
function normalizePath(path) {
var slash = '/';
if (path) {
path = path.trim();
if (path[0] !== slash) {
path = slash + path;
}
} else {
path = slash;
}
return path;
}
function herokuEnsureHttps(req, res, next) {
if (req.headers['x-forwarded-proto'] === 'https') {
return next();
}
res.status(404).send('https please');
}
function startPostServer(port, path, options, callback) {
if (typeof port === 'object') {
callback = path;
path = port.path;
}
var app = createExpressApp(port, options);
if (typeof callback === 'object') {
for (path in callback) {
app.post(normalizePath(path), callback[path]);
}
} else {
app.post(normalizePath(path), callback);
}
return app.startServer();
}
function createExpressApp(port, options) {
if (!options) {
options = port;
port = options.port;
}
var app = express();
var heroku = rpmUtil.isHeroku();
if (heroku) {
app.use(herokuEnsureHttps);
port = process.env.PORT;
}
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.text({ type: '*/*' }));
app.startServer = function () {
var srv = (heroku ? app : https.createServer(options, app)).listen(port);
console.info('WebHooks server is listening on port', port);
return srv;
};
return app;
}
exports.startPostServer = startPostServer;
exports.createExpressApp = createExpressApp;
exports.normalizePath = normalizePath;