This repository was archived by the owner on May 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathapp.js
More file actions
104 lines (95 loc) · 3.32 KB
/
Copy pathapp.js
File metadata and controls
104 lines (95 loc) · 3.32 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
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
/**
* Copyright 2017 IBM Corp. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the “License”);
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an “AS IS” BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
if (process.env.VCAP_SERVICES) {
console.log('Enabling IBM Cloud autoscaling agent');
require('bluemix-autoscaling-agent');
}
const winston = require('winston');
const express = require('express');
const cors = require('cors');
const logger = winston.createLogger({
transports: [
new (winston.transports.Console)({
timestamp: () => new Date(),
formatter: options =>
// Return string will be passed to logger.
options.timestamp().toISOString() + ' ' + // eslint-disable-line
options.level.toUpperCase() + ' ' +
(options.message ? options.message : '') +
(options.meta && Object.keys(options.meta).length ?
'\n\t' + JSON.stringify(options.meta) : '') // eslint-disable-line
})
]
});
const app = express();
const fibonacci = require('./lib/fibonacci')();
app.use(cors());
app.disable('etag');
app.get('/fibonacci', (req, res) => {
if (req.query.iteration) {
logger.info(`GET iteration=${req.query.iteration}`);
const aborter = fibonacci.compute({
iteration: req.query.iteration,
parallel: req.query.parallel,
callback: (result) => {
logger.info(`Completed iteration=${req.query.iteration} ${result.ms}ms`);
res.send(result);
}
});
req.on('close', () => {
aborter.cancel();
logger.info(`Aborted iteration=${req.query.iteration}`);
});
aborter.do();
} else if (req.query.duration) {
logger.info(`GET duration=${req.query.duration}`);
const aborter = fibonacci.compute({
duration: req.query.duration,
parallel: req.query.parallel,
callback: (result) => {
logger.info(`Completed duration=${req.query.duration} ${result.ms}ms`);
res.send(result);
}
});
req.on('close', () => {
aborter.cancel();
logger.info(`Aborted iteration=${req.query.duration}`);
});
aborter.do();
} else {
res.status(500).send('Unknown operation');
}
});
app.post('/fibonacci', (req, res) => {
if (req.query.crash === 'true') {
logger.info('POST crash');
process.exit(1);
} else {
res.status(500).send('Unknown operation');
}
});
app.get('/', (req, res) => {
logger.info('GET /');
const name = process.env.VCAP_SERVICES ? 'One Instance' : 'One Replica';
const icon = process.env.VCAP_SERVICES ? '/images/cloudfoundry.png' : '/images/kubernetes.svg';
const pingEndpoint = `${req.protocol}://${req.headers.host}/fibonacci?iteration=500`;
const crashEndpoint = `${req.protocol}://${req.headers.host}/fibonacci?crash=true`;
res.send(fibonacci.addEnpointHtml(name, icon, pingEndpoint, crashEndpoint));
});
const port = process.env.PORT || 8080;
app.listen(port, () => {
logger.info(`App is listening on port ${port}.`);
});