forked from RabbitMQSimulator/RabbitMQSimulator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
89 lines (76 loc) · 2.33 KB
/
app.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
var express = require('express');
var http = require("http");
var engine = require('ejs-locals');
var app = express();
app.set('mgmt-user', 'guest');
app.set('mgmt-pass', 'guest');
app.set('mgmt-host', 'localhost');
app.set('mgmt-port', '15672');
app.use(express.static(__dirname + '/web'));
app.use(express.bodyParser());
app.use(require('express-blocks'));
app.set('views', __dirname + '/views');
app.set('view engine', 'html');
app.engine('html', engine);
function get_auth(user, pass) {
return 'Basic ' + new Buffer(user + ':' + pass).toString('base64');
}
function get_base_req_opts() {
return {
'host': app.get('mgmt-host'),
'port': app.get('mgmt-port'),
'headers': {
'Content-Type': 'application/json',
'Authorization': get_auth(app.get('mgmt-user'), app.get('mgmt-pass'))
}
};
}
app.get('/definitions', function (req, res) {
var options = get_base_req_opts();
options.path = '/api/definitions';
options.method = 'GET';
var rest = http.request(options, function (response){
var output = '';
response.setEncoding('utf8');
response.on('data', function (chunk) {
output += chunk;
});
response.on('end', function() {
res.set('Content-Type', 'application/json');
console.log("sending output:", output);
res.send(output);
});
});
rest.end();
});
app.post('/definitions', function (req, res) {
var post_data = JSON.stringify(req.body);
var options = get_base_req_opts();
options.path = '/api/definitions';
options.method = 'POST';
options.headers['Content-Length'] = post_data.length;
var rest = http.request(options, function (response){
var output = '';
response.setEncoding('utf8');
response.on('data', function (chunk) {
output += chunk;
});
response.on('end', function() {
res.set('Content-Type', 'application/json');
res.send(output);
});
});
rest.write(post_data);
rest.end();
});
app.get('/', function (req, res) {
res.render('simulator');
});
app.get('/about', function (req, res) {
res.render('about');
});
app.get('/player', function (req, res) {
res.render('player');
});
app.listen(3000);
console.log('Listening on port 3000');