This repository has been archived by the owner on Jan 28, 2022. It is now read-only.
forked from cloudfoundry/overview-broker
-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
181 lines (167 loc) · 7.8 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
var express = require('express'),
bodyParser = require('body-parser'),
basicAuth = require('express-basic-auth'),
morgan = require('morgan'),
Logger = require('./logger'),
ServiceBrokerInterface = require('./service_broker_interface'),
serviceBrokerInterface = null;
function start(callback) {
let app = express();
app.use(bodyParser.json()); // support json encoded bodies
app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies
if (process.env.NODE_ENV != 'testing') {
app.use(morgan('tiny'));
}
app.set('view engine', 'pug');
logger = new Logger();
serviceBrokerInterface = new ServiceBrokerInterface();
/* Error modes */
process.env.errorMode = process.env.ERROR_MODE || ''; // Disabled by default
const supportedErrorModes = [
'', // Disabled
'timeout', // Do not respond to any request
'servererror', // Return HTTP 500 to every request
'notfound', // Return HTTP 404 to every request
'gone', // Return HTTP 410 to every request
'unprocessable', // Return HTTP 422 to every request
'concurrencyerror', // Return HTTP 422 with the "ConcurrencyError" error code
'maintenanceinfoconflict', // Return HTTP 422 with the "MaintenanceInfoConflict" error code
'invalidjson', // Return invalid JSON to every request
'failasync' // Fail asynchronous operations (after they have finished)
];
/* Response modes */
process.env.responseMode = process.env.RESPONSE_MODE || 'default';
const supportedResponseModes = [
'default', // Asynchronous responses where possible
'sync', // Synchronous responses always
'async' // Asynchronous responses always
];
/* Unauthenticated routes */
app.get('/', function(request, response) {
response.redirect(303, '/dashboard');
});
app.get('/dashboard', function(request, response) {
var data = serviceBrokerInterface.getDashboardData();
data.errorMode = process.env.errorMode;
data.responseMode = process.env.responseMode;
response.render('dashboard', data);
});
app.get('/data', function(request, response) {
var data = serviceBrokerInterface.getDashboardData();
data.errorMode = process.env.errorMode;
data.responseMode = process.env.responseMode;
response.json(data);
});
app.get('/health', function(request, response) {
response.sendFile('health.yaml', { root: 'extensions' });
});
app.get('/info', function(request, response) {
response.sendFile('info.yaml', { root: 'extensions' });
});
app.get('/logs', function(request, response) {
response.sendFile('logs.yaml', { root: 'extensions' });
});
app.post('/admin/clean', function(request, response) {
serviceBrokerInterface.clean(request, response);
});
app.post('/admin/updateCatalog', function(request, response) {
serviceBrokerInterface.updateCatalog(request, response);
});
app.post('/admin/setErrorMode', function(request, response) {
if (!supportedErrorModes.includes(request.body.mode)) {
response.status(400).send('Invalid error mode');
return;
}
process.env.errorMode = request.body.mode;
console.log(`Error mode is now ${process.env.errorMode || 'disabled'}`);
response.json({});
});
app.post('/admin/setResponseMode', function(request, response) {
if (!supportedResponseModes.includes(request.body.mode)) {
response.status(400).send('Invalid response mode');
return;
}
process.env.responseMode = request.body.mode;
console.log(`Response mode is now ${process.env.responseMode}`);
response.json({});
});
app.use('/images', express.static('images'));
/* Extensions (unauthenticated) */
app.get('/v2/service_instances/:instance_id/health', serviceBrokerInterface.getHealth());
app.get('/v2/service_instances/:instance_id/info', serviceBrokerInterface.getInfo());
/* Authenticated routes (uses Basic Auth) */
var users = {};
users[process.env.BROKER_USERNAME || 'admin'] = process.env.BROKER_PASSWORD || 'password';
app.use(basicAuth({
users: users
}));
app.all(
'*',
function(request, response, next) {
serviceBrokerInterface.saveRequest(request);
switch (process.env.errorMode) {
case 'timeout':
console.log('timing out');
return;
case 'servererror':
serviceBrokerInterface.sendJSONResponse(response, 500, {
error: 'ErrorMode',
description: 'Error mode enabled (servererror)'
});
return;
case 'notfound':
serviceBrokerInterface.sendJSONResponse(response, 404, {});
return;
case 'gone':
serviceBrokerInterface.sendJSONResponse(response, 410, {});
return;
case 'unprocessable':
serviceBrokerInterface.sendJSONResponse(response, 422, {
error: 'ErrorMode',
description: 'Error mode enabled (unprocessable)'
});
return;
case 'concurrencyerror':
serviceBrokerInterface.sendJSONResponse(response, 422, {
error: 'ConcurrencyError',
description: 'Error mode enabled (concurrencyerror)'
});
return;
case 'maintenanceinfoconflict':
serviceBrokerInterface.sendJSONResponse(response, 422, {
error: 'MaintenanceInfoConflict',
description: 'Error mode enabled (maintenanceinfoconflict)'
});
return;
case 'invalidjson':
serviceBrokerInterface.sendResponse(response, 200, '{ "invalidjson error mode enabled" }');
return;
default:
next();
}
},
serviceBrokerInterface.checkRequest()
);
app.get('/v2/catalog', function(request, response) {
serviceBrokerInterface.getCatalog(request, response);
});
app.put('/v2/service_instances/:instance_id', serviceBrokerInterface.createServiceInstance());
app.patch('/v2/service_instances/:instance_id', serviceBrokerInterface.updateServiceInstance());
app.delete('/v2/service_instances/:instance_id', serviceBrokerInterface.deleteServiceInstance());
app.put('/v2/service_instances/:instance_id/service_bindings/:binding_id', serviceBrokerInterface.createServiceBinding());
app.delete('/v2/service_instances/:instance_id/service_bindings/:binding_id', serviceBrokerInterface.deleteServiceBinding());
app.get('/v2/service_instances/:instance_id/last_operation', serviceBrokerInterface.getLastServiceInstanceOperation());
app.get('/v2/service_instances/:instance_id/service_bindings/:binding_id/last_operation', serviceBrokerInterface.getLastServiceBindingOperation());
app.get('/v2/service_instances/:instance_id', serviceBrokerInterface.getServiceInstance());
app.get('/v2/service_instances/:instance_id/service_bindings/:binding_id', serviceBrokerInterface.getServiceBinding());
/* Listing */
app.get('/v2/service_instances', function(request, response) {
serviceBrokerInterface.listInstances(request, response);
});
var port = process.env.PORT || 3000;
var server = app.listen(port, function() {
logger.debug(`Overview broker running on port ${server.address().port}`);
callback(server, serviceBrokerInterface);
});
}
exports.start = start;