-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
179 lines (152 loc) · 4.21 KB
/
index.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
const express = require('express')
const http = require('http')
const faker = require('faker')
const _ = require('lodash')
const path = require('path')
const fs = require('fs')
/* eslint-disable no-undef */
let web = express()
let server = http.createServer(web)
const debug = process.env.DEBUG
? console.debug.bind(console)
: Function.prototype // javascript for 'noop'
// DEFAULT CONFIGURATION
global.defaultConfig = {
port: 9900,
perc300: 15,
perc400: 10,
perc500: 5
}
global.envOverRides = {
port: process.env.PORT || undefined
}
// load config filie and merge configurations
function getConfig() {
let arg = process.argv[2]
if (arg && !arg.endsWith('json')) {
console.error(
'ERROR: Supplied argument is not a JSON file. ' +
'Continuing with default config file.'
)
arg = 'config.json'
}
let name = arg || 'config.json'
let found = false
let files = []
let userConfig = {}
const paths = [ './', '../', '' ]
paths.forEach(pathlet => {
files.push( path.resolve(pathlet, name) )
})
files.forEach(file => {
if (!found && fs.existsSync(file)) {
userConfig = require(file)
if ((userConfig instanceof Object) && (Object.keys(userConfig).length > 0)) {
debug(`Using configuration from file ${file}`)
found = true
} else {
userConfig = {}
}
}
})
if (!found) {
debug(
`File ${name} is not a proper config file. ` +
'Continuing with default config.'
)
}
let config = _.merge(global.defaultConfig, userConfig, global.envOverRides)
config.perc200 = 100 - (config.perc300 + config.perc400 + config.perc500)
return config
}
// stats globals
global.numReq = 0
global.reqs = {
200: 0,
300: 0,
400: 0,
500: 0
}
// non-200 response texts
global.respTable = {
302: 'Found',
400: 'Bad Request',
401: 'Unauthorized',
403: 'Forbidden',
404: 'Not Found',
500: 'Internal Server Error',
501: 'Not Implemented'
}
// supported 40x errors
global.errors400 = [0, 1, 3, 4]
global.config = getConfig()
// setup morgan for logging
const formatter = (tokens, req, res) => {
return [
'[', tokens.date(req, res, 'clf'), ']',
tokens.method(req, res),
tokens.url(req, res),
tokens.status(req, res),
tokens.res(req, res, 'content-length'), 'bytes -',
tokens['response-time'](req, res), 'ms'
].join(' ')
}
// fake json generator
const genData = path => {
let data = faker.helpers.userCard()
data['path'] = path
return data
}
// calculate and return stats
const stats = (req, res) => {
reqs[200] += 1
let str = 'Response statistics\n'
str += '-------------------\n\n'
str += `Total requests: ${numReq}\n\n`
str += 'Responses by type:\n'
str += `2xx: ${(100 * reqs[200] / numReq).toFixed(2)}%\n`
str += `3xx: ${(100 * reqs[300] / numReq).toFixed(2)}%\n`
str += `4xx: ${(100 * reqs[400] / numReq).toFixed(2)}%\n`
str += `5xx: ${(100 * reqs[500] / numReq).toFixed(2)}%\n`
res.type('txt').send(str)
}
const defaultResponse = (config) => {
let { perc200, perc300, perc400, perc500 } = config
debug('Defining default responses with the following ratios:')
debug(`
200: ${perc200}%
300: ${perc300}%
400: ${perc400}%
500: ${perc500}%
`)
return (req, res) => {
numReq += 1
if (req.path === '/stats') stats(req, res)
else {
let dice = _.random(1, 100)
if (dice < perc200) {
reqs[200] += 1
res.send(genData(req.path.slice(1)))
} else if (dice < perc200 + perc300) {
reqs[300] += 1
res.redirect(302, path.join(...faker.random.words().toLowerCase().split(' ')))
} else if (dice < perc200 + perc300 + perc400) {
reqs[400] += 1
let status = 400 + faker.random.arrayElement(errors400)
res.type('txt').status(status).send(respTable[status])
} else {
reqs[500] += 1
let status = _.random(500, 501, false)
res.type('txt').status(status).send(respTable[status])
}
}
}
}
// inject morgan into express
if (process.env.DEBUG) web.use(require('morgan')(formatter, process.stdout))
// čdefault route handler
web.use(defaultResponse(config))
// lift server, start listening
server.listen(config.port, () => {
console.log(`Listening on port ${config.port}`)
})