-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
152 lines (136 loc) · 3.84 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
// Locally reguired
const dotenv = require('dotenv').config({ path: './.env' })
// end of Locally required
const express = require('express')
const app = express()
const pexels = require('pexels')
const photoCli = pexels.createClient(process.env.API_KEY || dotenv.API_KEY)
const port = process.env.PORT || dotenv.PORT
const HTTPStatusCode = require('http-status-code')
const fortune = require('random-fortune')
var phrase = fortune.fortune()
var statusId = 200
var ignoreDrop = false
var allStatus = HTTPStatusCode.getProtocolDefinitions()
// Favicon - because I don't like unnecessary errors on log
app.use('/favicon.ico', express.static('favicon.ico'));
// Points to API URL, based on platform (local or Heroku)
app.get('/', (req, res) => {
// Trick: if newHost header was present, use it
let newUrl = req.get('newHost')
if(typeof req.get('newHost') === 'undefined') {
newUrl = req.get('host')
}
res.json({
'newUrl': newUrl + '/api'
})
})
// Get random HTTP code from allStatus object
var randomProperty = function (obj) {
var keys = Object.keys(obj);
var newKey = keys[ keys.length * Math.random() << 0]
return parseInt(newKey);
};
// Randomly drops 5xx connections
var dropConnection = function (statusId, res, jsonReturn) {
if(statusId > 499 && statusId < 600){
if((parseInt(Math.random() * 100) % 2) > 0){
res.status(statusId).end()
} else {
res.status(statusId).json(jsonReturn)
}
} else {
// Bypass if statusId < 500
res.status(statusId).json(jsonReturn)
}
}
/**
* Default API request.
* Returns random HTTP Status code from HTTPStatusCode list
*/
app.get('/api', (req, res, next) => {
phrase = fortune.fortune()
statusId = randomProperty(allStatus)
var jsonReturn = {
'code': statusId,
'status': HTTPStatusCode.getMessage(statusId),
'message': phrase
}
if(statusId >= 100 && statusId < 400) {
// Async to obtain image uri
photoCli.photos.random()
.then (photo => {
jsonReturn.image = photo.src.medium
res.status(statusId).json(jsonReturn)
} )
.catch(next);
} else {
dropConnection(statusId, res, jsonReturn)
}
})
/**
* Request with parameters, ex.: /api/418
* Returns this HTTP status code
*/
// Read parameter and set to statusId
app.param('id', function (req, res, next, id) {
// Prevent invalid codes
if(typeof allStatus[id] === 'undefined') {
statusId = 500
} else {
statusId = parseInt(id)
}
next()
})
// Read OPTIONAL parameter to drop 500 connection
app.param('drop', function (req, res, next, drop) {
if(drop) {
ignoreDrop = true
}
next()
})
// Run Request
app.get('/api/:id', function (req, res, next) {
phrase = fortune.fortune()
var jsonReturn = {
'code': statusId,
'status': HTTPStatusCode.getMessage(statusId),
'message': phrase
}
if(statusId >= 100 && statusId < 400) {
// Async to obtain image uri
photoCli.photos.random()
.then (photo => {
jsonReturn.image = photo.src.medium
res.status(statusId).json(jsonReturn)
} )
.catch(next);
} else {
dropConnection(statusId, res, jsonReturn)
}
})
app.get('/api/:id/:drop', function (req, res, next) {
phrase = fortune.fortune()
var jsonReturn = {
'code': statusId,
'status': HTTPStatusCode.getMessage(statusId),
'message': phrase
}
if(statusId >= 100 && statusId < 400) {
// Async to obtain image uri
photoCli.photos.random()
.then (photo => {
jsonReturn.image = photo.src.medium
res.status(statusId).json(jsonReturn)
} )
.catch(next);
} else {
res.status(statusId).json(jsonReturn)
}
})
// Indicate running app
app.listen(port, () => {
console.log('Our app is running on port ' + port + ', with current status: \n\n' + phrase)
});
// Export modules for Tests
module.exports = app