-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebserver.js
57 lines (50 loc) · 1.17 KB
/
webserver.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
'use strict'
const http = require('http')
const os = require('os')
let counter = 0
let ip = '127.0.0.1'
let server = 'docker'
let candidates = []
const ifaces = os.networkInterfaces()
Object.keys(ifaces).forEach(ifname => {
ifaces[ifname].forEach(iface => {
// Skip over internal and non-ipv4 addresses.
if (iface.family !== 'IPv4' || iface.internal !== false) {
return
}
candidates.push(iface.address)
})
})
if (candidates.length > 0) {
ip = candidates[0]
}
if (process.argv.length > 2) {
server = process.argv[2]
}
function generatePage (req) {
let lines = []
lines.push(`server: ${server}`)
lines.push(`Container IP: ${ip}`)
lines.push(`Visits: ${counter}`)
lines.push('')
const host = req.headers['host']
if (host) {
lines.push(`Host: ${host}`)
}
const origin = req.headers['x-forwarded-for']
if (origin) {
lines.push(`X-Forwarded-For: ${origin}`)
}
lines.push('')
return lines.join('\r\n')
}
const webserver = http.createServer((req, res) => {
if (req.url === '/health') {
return res.end("I'm not dead yet")
}
if (req.url !== '/favicon.ico') {
++counter
}
res.end(generatePage(req))
})
webserver.listen(8080)