-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
386 lines (352 loc) · 11.1 KB
/
server.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
const redis = require('redis');
const request = require('request');
const bodyParser = require('body-parser');
const express = require('express');
const pino = require('pino');
const expPino = require('express-pino-logger');
// Prometheus
const promClient = require('prom-client');
const Registry = promClient.Registry;
const register = new Registry();
const counter = new promClient.Counter({
name: 'items_added',
help: 'running count of items added to cart',
registers: [register]
});
var redisConnected = false;
var redisHost = process.env.REDIS_HOST || 'redis'
var catalogueHost = process.env.CATALOGUE_HOST || 'catalogue'
var cataloguePort = process.env.CATALOGUE_PORT || '8080'
const logger = pino({
level: 'info',
prettyPrint: false,
useLevelLabels: true
});
const expLogger = expPino({
logger: logger
});
const app = express();
app.use(expLogger);
app.use((req, res, next) => {
res.set('Timing-Allow-Origin', '*');
res.set('Access-Control-Allow-Origin', '*');
next();
});
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.get('/health', (req, res) => {
var stat = {
app: 'OK',
redis: redisConnected
};
res.json(stat);
});
// Prometheus
app.get('/metrics', (req, res) => {
res.header('Content-Type', 'text/plain');
res.send(register.metrics());
});
// get cart with id
app.get('/cart/:id', (req, res) => {
redisClient.get(req.params.id, (err, data) => {
if(err) {
req.log.error('ERROR', err);
res.status(500).send(err);
} else {
if(data == null) {
res.status(404).send('cart not found');
} else {
res.set('Content-Type', 'application/json');
res.send(data);
}
}
});
});
// delete cart with id
app.delete('/cart/:id', (req, res) => {
redisClient.del(req.params.id, (err, data) => {
if(err) {
req.log.error('ERROR', err);
res.status(500).send(err);
} else {
if(data == 1) {
res.send('OK');
} else {
res.status(404).send('cart not found');
}
}
});
});
// rename cart i.e. at login
app.get('/rename/:from/:to', (req, res) => {
redisClient.get(req.params.from, (err, data) => {
if(err) {
req.log.error('ERROR', err);
res.status(500).send(err);
} else {
if(data == null) {
res.status(404).send('cart not found');
} else {
var cart = JSON.parse(data);
saveCart(req.params.to, cart).then((data) => {
res.json(cart);
}).catch((err) => {
req.log.error(err);
res.status(500).send(err);
});
}
}
});
});
// update/create cart
app.get('/add/:id/:sku/:qty', (req, res) => {
// check quantity
var qty = parseInt(req.params.qty);
if(isNaN(qty)) {
req.log.warn('quantity not a number');
res.status(400).send('quantity must be a number');
return;
} else if(qty < 1) {
req.log.warn('quantity less than one');
res.status(400).send('quantity has to be greater than zero');
return;
}
// look up product details
getProduct(req.params.sku).then((product) => {
req.log.info('got product', product);
if(!product) {
res.status(404).send('product not found');
return;
}
// is the product in stock?
if(product.instock == 0) {
res.status(404).send('out of stock');
return;
}
// does the cart already exist?
redisClient.get(req.params.id, (err, data) => {
if(err) {
req.log.error('ERROR', err);
res.status(500).send(err);
} else {
var cart;
if(data == null) {
// create new cart
cart = {
total: 0,
tax: 0,
items: []
};
} else {
cart = JSON.parse(data);
}
req.log.info('got cart', cart);
// add sku to cart
var item = {
qty: qty,
sku: req.params.sku,
name: product.name,
price: product.price,
subtotal: qty * product.price
};
var list = mergeList(cart.items, item, qty);
cart.items = list;
cart.total = calcTotal(cart.items);
// work out tax
cart.tax = calcTax(cart.total);
// save the new cart
saveCart(req.params.id, cart).then((data) => {
counter.inc(qty);
res.json(cart);
}).catch((err) => {
req.log.error(err);
res.status(500).send(err);
});
}
});
}).catch((err) => {
req.log.error(err);
res.status(500).send(err);
});
});
// update quantity - remove item when qty == 0
app.get('/update/:id/:sku/:qty', (req, res) => {
// check quantity
var qty = parseInt(req.params.qty);
if(isNaN(qty)) {
req.log.warn('quanity not a number');
res.status(400).send('quantity must be a number');
return;
} else if(qty < 0) {
req.log.warn('quantity less than zero');
res.status(400).send('negative quantity not allowed');
return;
}
// get the cart
redisClient.get(req.params.id, (err, data) => {
if(err) {
req.log.error('ERROR', err);
res.status(500).send(err);
} else {
if(data == null) {
res.status(404).send('cart not found');
} else {
var cart = JSON.parse(data);
var idx;
var len = cart.items.length;
for(idx = 0; idx < len; idx++) {
if(cart.items[idx].sku == req.params.sku) {
break;
}
}
if(idx == len) {
// not in list
res.status(404).send('not in cart');
} else {
if(qty == 0) {
cart.items.splice(idx, 1);
} else {
cart.items[idx].qty = qty;
cart.items[idx].subtotal = cart.items[idx].price * qty;
}
cart.total = calcTotal(cart.items);
// work out tax
cart.tax = calcTax(cart.total);
saveCart(req.params.id, cart).then((data) => {
res.json(cart);
}).catch((err) => {
req.log.error(err);
res.status(500).send(err);
});
}
}
}
});
});
// add shipping
app.post('/shipping/:id', (req, res) => {
var shipping = req.body;
if(shipping.distance === undefined || shipping.cost === undefined || shipping.location == undefined) {
req.log.warn('shipping data missing', shipping);
res.status(400).send('shipping data missing');
} else {
// get the cart
redisClient.get(req.params.id, (err, data) => {
if(err) {
req.log.error('ERROR', err);
res.status(500).send(err);
} else {
if(data == null) {
req.log.info('no cart for', req.params.id);
res.status(404).send('cart not found');
} else {
var cart = JSON.parse(data);
var item = {
qty: 1,
sku: 'SHIP',
name: 'shipping to ' + shipping.location,
price: shipping.cost,
subtotal: shipping.cost
};
// check shipping already in the cart
var idx;
var len = cart.items.length;
for(idx = 0; idx < len; idx++) {
if(cart.items[idx].sku == item.sku) {
break;
}
}
if(idx == len) {
// not already in cart
cart.items.push(item);
} else {
cart.items[idx] = item;
}
cart.total = calcTotal(cart.items);
// work out tax
cart.tax = calcTax(cart.total);
// save the updated cart
saveCart(req.params.id, cart).then((data) => {
res.json(cart);
}).catch((err) => {
req.log.error(err);
res.status(500).send(err);
});
}
}
});
}
});
function mergeList(list, product, qty) {
var inlist = false;
// loop through looking for sku
var idx;
var len = list.length;
for(idx = 0; idx < len; idx++) {
if(list[idx].sku == product.sku) {
inlist = true;
break;
}
}
if(inlist) {
list[idx].qty += qty;
list[idx].subtotal = list[idx].price * list[idx].qty;
} else {
list.push(product);
}
return list;
}
function calcTotal(list) {
var total = 0;
for(var idx = 0, len = list.length; idx < len; idx++) {
total += list[idx].subtotal;
}
return total;
}
function calcTax(total) {
// tax @ 20%
return (total - (total / 1.2));
}
function getProduct(sku) {
return new Promise((resolve, reject) => {
request('http://' + catalogueHost + ':' + cataloguePort +'/product/' + sku, (err, res, body) => {
if(err) {
reject(err);
} else if(res.statusCode != 200) {
resolve(null);
} else {
// return object - body is a string
// TODO - catch parse error
resolve(JSON.parse(body));
}
});
});
}
function saveCart(id, cart) {
logger.info('saving cart', cart);
return new Promise((resolve, reject) => {
redisClient.setex(id, 3600, JSON.stringify(cart), (err, data) => {
if(err) {
reject(err);
} else {
resolve(data);
}
});
});
}
// connect to Redis
var redisClient = redis.createClient({
host: redisHost
});
redisClient.on('error', (e) => {
logger.error('Redis ERROR', e);
});
redisClient.on('ready', (r) => {
logger.info('Redis READY', r);
redisConnected = true;
});
// fire it up!
const port = process.env.CART_SERVER_PORT || '8080';
app.listen(port, () => {
logger.info('Started on port', port);
});