-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
67 lines (55 loc) · 1.98 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
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
var request = require('request');
var crypto = require('crypto');
var config = require('config');
/**
* load configuration from config/default.json or from environment variables if any
*/
var kongUrl = (process.env.SANDBOX_URL ? process.env.SANDBOX_URL : config.get('kong.url')).replace(/\/$/, "");
var username = process.env.API_KEY ? process.env.API_KEY : config.get('kong.username')
var secret = process.env.API_SECRET ? process.env.API_SECRET : config.get('kong.secret');
var port = process.env.APP_PORT ? process.env.APP_PORT : config.get('port');
// load web app
app.use('/web', express.static(__dirname + '/web'));
app.use( bodyParser.json() ); // to support JSON-encoded bodies
app.use(bodyParser.urlencoded({ // to support URL-encoded bodies
extended: true
}));
app.use(express.static('web'));
app.listen(process.env.PORT || port, function () {
console.log('listening on port ' + port);
});
/**
* prepare auth headers for Kong server
* @returns {{date: string, Authorization: string}}
*/
var getAuthenticationHeaders = function () {
var date = new Date().toUTCString();
var stringToSign = 'date: ' + date.trim();
var encodedSignature = crypto.createHmac("sha1", secret).update(stringToSign).digest("base64");
var hmacAuth = 'hmac username="' + username + '",algorithm="hmac-sha1",headers="date",signature="' + encodedSignature + '"';
return {
'date': date,
'Authorization': hmacAuth
}
}
/**
* POST and GET service end-point proxy
*/
app.all('/marketplace/*', function(req, res) {
var options = {
method: req.method,
url: kongUrl + req.originalUrl,
headers: getAuthenticationHeaders()
};
if (req.method == 'POST') {
options['json'] = req.body;
options['content-type'] = 'application/json';
}
request(options, function(error, response, body) {
if (error) throw new Error(error);
res.status(response.statusCode).send(body);
});
});