-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
executable file
·124 lines (109 loc) · 3.66 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
// MIT License
// Copyright (c) 2016-2018 David Betz
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
"use strict"
process.env['DEBUG'] = process.env['DEBUG'] || 'trace'
const debug = require('debug')('trace')
const express = require("express")
const request = require('request')
const port = process.env.PORT || 3000
const app = express()
app.set('port', port)
let addressSuffix = process.env.ADDRESS_SUFFIX || ''
if(addressSuffix.length > 0) {
addressSuffix = "." + addressSuffix
}
const abc = Array.from(Array(26).keys()).map(p => String.fromCharCode(97 + p))
const call = (address) => {
return new Promise((resolve, reject) => {
const uri = 'http://' + address + addressSuffix + ':' + port
debug('+CALLING ' + uri)
request.get(uri, async (error, response, body) => {
debug('-CALL RETURNED ' + body)
resolve(body)
})
})
}
app.get('/:id?', async (req, res) => {
//+ if id in path, use it
let id = req.url.substring(1)
if(id.length != 1 ) {
id = getId(req.hostname)
}
const index = abc.indexOf(id)
debug(`+CALLED id:${id}|index:${index}`)
let value = '$'
let start = +new Date()
if (index > 0) {
value = await call(abc[index - 1])
}
if (process.env.TIMER) {
let duration = +(new Date()) - start
value += ',' + id + ':' + duration
}
else {
value += ',' + id
}
debug('-RETURNING ' + value)
res.send(value)
})
const getId = host => {
debug("host", host)
let id
//+ if . in host, use first part
let withSuffixParts = host.split(".")
if(withSuffixParts.length > 0) {
id = withSuffixParts[0]
}
//+ if : in host, use host
if(id.length == 0) {
let parts = host.split(":")
id = parts[0]
}
//+ if len(host) > 0, default to g
if(id.length != 1) {
debug("Argument length != 1. Defaulting to g.")
id = "g"
}
return id
}
const check = letter => {
return new Promise((resolve, reject) => {
const uri = 'http://' + letter + addressSuffix + ':' + port
debug('+CALLING ' + uri)
request.get(uri, async (error, response, body) => {
if(error) {
throw new Error(error)
}
debug('-CALL RETURNED ' + body)
resolve(body)
})
})
}
(async () => {
const arg = process.argv[2]
switch(arg) {
case "check":
await check(getId(process.argv[3]))
break
default:
const server = require('http').Server(app)
server.listen(app.get('port'), async () => {
debug('started')
})
}
})()