-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathapp.js
194 lines (156 loc) · 4.91 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
182
183
184
185
186
187
188
189
190
191
192
193
194
//var agent = require('@google-cloud/trace-agent').start();
const express = require('express');
const app = express();
var rp = require('request-promise');
const dns = require('dns');
const morgan = require('morgan');
const port = 8080;
//process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
app.use(
morgan('combined')
);
var winston = require('winston');
var logger = winston.createLogger({
transports: [
new (winston.transports.Console)({ level: 'info' })
]
});
app.get('/', (request, response) => {
logger.info('Called /');
response.send('Hello from Express!');
})
app.get('/_ah/health', (request, response) => {
response.send('ok');
})
app.get('/varz', (request, response) => {
response.send(process.env);
})
app.get('/version', (request, response) => {
response.send(process.env.VER);
})
app.get('/backend', (request, response) => {
response.send('pod: [' + process.env.MY_POD_NAME + '] node: [' + process.env.MY_NODE_NAME + ']');
})
app.get('/hostz', (request, response) => {
dns.resolveSrv("_http._tcp.be.default.svc.cluster.local", function onLookup(err, addresses, family) {
if (err) {
response.send(err);
} else if (addresses.length >= 1) {
logger.info('addresses: ' + JSON.stringify(addresses));
var host = addresses[0].name;
var port = addresses[0].port;
logger.info(host + " --> " + port);
var resp_promises = []
var urls = [
'http://' + host + ':' + port + '/backend',
]
urls.forEach(function(element){
resp_promises.push( getURL(element) )
});
Promise.all(resp_promises).then(function(value) {
response.send(value);
}, function(value) {
response.send(value);
});
} else{
response.send('No Backend Services found');
}
});
})
function getURL(u, headers) {
var options = {
method: 'GET',
uri: u,
resolveWithFullResponse: true,
simple: false,
headers: headers
};
return rp(options)
.then(function (resp) {
return Promise.resolve(
{ 'url' : u, 'body': resp.body, 'statusCode': resp.statusCode }
);
})
.catch(function (err) {
return Promise.resolve({ 'url' : u, 'statusCode': err } );
});
}
app.get('/requestz', (request, response) => {
var resp_promises = []
var urls = [
'https://www.google.com/robots.txt',
'http://www.google.com:443/robots.txt',
'http://www.bbc.com/robots.txt',
'https://www.cornell.edu/robots.txt',
'https://www.uwo.ca/robots.txt',
'http://www.yahoo.com/robots.txt',
'https://www.yahoo.com/robots.txt',
'http://www.yahoo.com:443/robots.txt'
]
urls.forEach(function(element){
resp_promises.push( getURL(element, {}) )
});
Promise.all(resp_promises).then(function(value) {
response.send(value);
}, function(value) {
response.send(value);
});
})
app.get('/headerz', (request, response) => {
logger.info('/headerz');
response.send(request.headers);
})
app.get('/metadata', (request, response) => {
var resp_promises = []
var urls = [
'http://metadata.google.internal/computeMetadata/v1/project/project-id',
'http://metadata/computeMetadata/v1/project/project-id',
'http://169.254.169.254/computeMetadata/v1/project/project-id'
]
urls.forEach(function(element){
resp_promises.push( getURL(element, {'Metadata-Flavor':'Google'}) )
});
Promise.all(resp_promises).then(function(value) {
response.send(value);
}, function(value) {
response.send(value);
});
})
app.get('/remote', (request, response) => {
var resp_promises = []
var urls = [
'http://be.ns2.global:8080/backend'
]
urls.forEach(function(element){
resp_promises.push( getURL(element) )
});
Promise.all(resp_promises).then(function(value) {
response.send(value);
}, function(value) {
response.send(value);
});
})
const server = app.listen(port, () => logger.info('Running…'));
setInterval(() => server.getConnections(
(err, connections) => console.log(`${connections} connections currently open`)
), 60000);
process.on('SIGTERM', shutDown);
process.on('SIGINT', shutDown);
let connections = [];
server.on('connection', connection => {
connections.push(connection);
connection.on('close', () => connections = connections.filter(curr => curr !== connection));
});
function shutDown() {
console.log('Received kill signal, shutting down gracefully');
server.close(() => {
logger.info('Closed out remaining connections');
process.exit(0);
});
setTimeout(() => {
logger.error('Could not close connections in time, forcefully shutting down');
process.exit(1);
}, 10000);
connections.forEach(curr => curr.end());
setTimeout(() => connections.forEach(curr => curr.destroy()), 5000);
}