forked from devops-training/node-sample
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
247 lines (203 loc) · 4.5 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
var express = require('express');
var bodyParser = require('body-parser');
var logger = require('morgan');
var request = require('request');
var sync = require('synchronize');
var apikey = require('./apikey.js');
var fs = require('fs');
var contacts = {
test1: {
id: 'test1',
name: 'Test User 1',
email: 'test1@example.com'
},
test2: {
id: 'test2',
name: 'Test User 2',
email: 'test2@example.com'
}
};
function getLambdaURL() {
var config;
if (process.env.NODE_ENV === 'heroku') {
config = JSON.parse(process.env.LAMBDA_CONFIG);
} else {
config = JSON.parse(fs.readFileSync('./lambda.json', 'utf-8'));
}
return config.url;
}
var lambdaURL;
function requestGlobalContactID() {
if (!lambdaURL) {
lambdaURL = getLambdaURL();
}
var opts = {
url: lambdaURL,
json: true,
};
return sync.await(request.post(opts, sync.defer()));
}
function generateGlobalContactID() {
var maxRetryCnt = 3,
retryCnt = 0;
var gid, res;
do {
res = requestGlobalContactID();
console.log(JSON.stringify(res));
if (parseInt(res.statusCode) === 200) {
// request succeeded; break
gid = res.body.id;
break;
}
// request failed; retry
retryCnt++;
console.log("retrying: " + retryCnt);
} while (retryCnt <= maxRetryCnt);
return gid;
}
var contactID = 1;
function generateLocalContactID() {
return contactID++;
}
function getContact(id) {
if (!contacts[id]) {
return null;
}
return contacts[id];
}
function addContact(req) {
// input validation
if (typeof req.body.name !== "string" || typeof req.body.email !== "string") {
return null;
}
// request for a new global id
var id = generateGlobalContactID();
if (typeof id !== "string") {
return null;
}
// create a new contact
var contact = {
id: id.toString(),
name: req.body.name,
email: req.body.email
};
// add to the global container
contacts[id] = contact;
return contact;
}
function deleteContact(id) {
if (!contacts[id]) {
return null;
}
delete contacts[id];
return {};
}
var app = module.exports = express();
app.set('port', (process.env.PORT || 5000));
app.use(logger('dev'));
app.use(express.static(__dirname + '/public'));
app.use(bodyParser.json({
extended: true
}));
app.use('/api/*', apikey.verify({
APIKEY1: 'secure_api_key'
}));
app.use(function (req, res, next) {
sync.fiber(next);
});
app.get('/contacts/:id', function (req, res) {
if (req.params.id.match(/[^0-9a-zA-Z\-]+/)) {
res.send('invalid contact id');
return;
}
res.sendFile(__dirname + '/public/contact.html');
});
app.get('/api/1/test', function (req, res) {
res.json({
message: 'api test succeeded'
});
});
app.get('/api/1/contacts', function (req, res) {
var result = {
contacts: []
};
for (var key in contacts) {
result.contacts.push(contacts[key]);
}
res.json({
contacts: contacts
});
});
app.get('/api/1/contacts/:id', function (req, res) {
var result = getContact(req.params.id);
if (!result) {
res.status(404).json({
error: 'user id not found'
});
return;
}
res.json({
contact : result
})
});
app.post('/api/1/contacts', function (req, res) {
var contact = addContact(req);
if (!contact) {
res.status(503).json({
error: 'cannot create contact'
});
return;
}
res.status(201).json(contact);
});
app.delete('/api/1/contacts/:id', function (req, res) {
var result = deleteContact(req.params.id);
if (!result) {
res.status(404).json({
error: 'cannot delete the contact'
});
return;
}
res.status(204).send();
});
app.put('/api/1/contacts/:id', function (req, res) {
var id = req.params.id;
// example of input validation
if (typeof id !== "string") {
res.status(400).json({
error: 'invalid id'
});
return;
}
if (typeof req.body.name !== "string") {
res.status(400).json({
error: 'invalid name'
});
return;
}
if (typeof req.body.email !== "string") {
res.status(400).json({
error: 'invalid email'
});
return;
}
var result = deleteContact(id);
if (!result) {
res.status(404).json({
error: 'cannot update the contact'
});
return;
}
var contact = {
id: id,
name: req.body.name,
email: req.body.email
};
contacts[id] = contact;
res.status(200).json(contact);
});
if (!module.parent) {
app.listen(app.get('port'), function () {
console.log('node app is running at localhost:' + app.get('port'));
});
}