-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
193 lines (174 loc) · 5.21 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
const mongoClient = require('mongodb').MongoClient;
const mongoObjectID = require('mongodb').ObjectID;
const bodyParser = require('body-parser');
const express = require('express');
const pino = require('pino');
const expPino = require('express-pino-logger');
const logger = pino({
level: 'info',
prettyPrint: false,
useLevelLabels: true
});
const expLogger = expPino({
logger: logger
});
// MongoDB
var db;
var collection;
var mongoConnected = false;
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',
mongo: mongoConnected
};
res.json(stat);
});
// all products
app.get('/products', (req, res) => {
if(mongoConnected) {
collection.find({}).toArray().then((products) => {
res.json(products);
}).catch((e) => {
req.log.error('ERROR', e);
res.status(500).send(e);
});
} else {
req.log.error('database not available');
res.status(500).send('database not avaiable');
}
});
// product by SKU
app.get('/product/:sku', (req, res) => {
if(mongoConnected) {
// optionally slow this down
const delay = process.env.GO_SLOW || 0;
setTimeout(() => {
collection.findOne({sku: req.params.sku}).then((product) => {
req.log.info('product', product);
if(product) {
res.json(product);
} else {
res.status(404).send('SKU not found');
}
}).catch((e) => {
req.log.error('ERROR', e);
res.status(500).send(e);
});
}, delay);
} else {
req.log.error('database not available');
res.status(500).send('database not available');
}
});
// products in a category
app.get('/products/:cat', (req, res) => {
if(mongoConnected) {
collection.find({ categories: req.params.cat }).sort({ name: 1 }).toArray().then((products) => {
if(products) {
res.json(products);
} else {
res.status(404).send('No products for ' + req.params.cat);
}
}).catch((e) => {
req.log.error('ERROR', e);
res.status(500).send(e);
});
} else {
req.log.error('database not available');
res.status(500).send('database not avaiable');
}
});
// all categories
app.get('/categories', (req, res) => {
if(mongoConnected) {
collection.distinct('categories').then((categories) => {
res.json(categories);
}).catch((e) => {
req.log.error('ERROR', e);
res.status(500).send(e);
});
} else {
req.log.error('database not available');
res.status(500).send('database not available');
}
});
// search name and description
app.get('/search/:text', (req, res) => {
if(mongoConnected) {
collection.find({ '$text': { '$search': req.params.text }}).toArray().then((hits) => {
res.json(hits);
}).catch((e) => {
req.log.error('ERROR', e);
res.status(500).send(e);
});
} else {
req.log.error('database not available');
res.status(500).send('database not available');
}
});
if (process.env.MONGO == 'true') {
// set up Mongo
function mongoConnect() {
return new Promise((resolve, reject) => {
var mongoURL = process.env.MONGO_URL || 'mongodb://mongodb:27017/catalogue';
mongoClient.connect(mongoURL, (error, client) => {
if(error) {
reject(error);
} else {
db = client.db('catalogue');
collection = db.collection('products');
resolve('connected');
}
});
});
}
}
if (process.env.DOCUMENTDB == 'true') {
function mongoConnect() {
return new Promise((resolve, reject) => {
var mongoURL = process.env.MONGO_URL || 'mongodb://username:password@mongodb:27017/catalogue?tls=true&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false';
var client = mongoClient.connect(mongoURL,
{
// Mutable & Immutable
//tlsCAFile: `/home/roboshop/catalogue/rds-combined-ca-bundle.pem` //Specify the DocDB; cert
// Container
tlsCAFile: `/app/rds-combined-ca-bundle.pem` //Specify the DocDB; cert
}, (error, client) => {
if(error) {
reject(error);
} else {
db = client.db('catalogue');
collection = db.collection('products');
resolve('connected');
}
});
});
}
}
// mongodb connection retry loop
function mongoLoop() {
mongoConnect().then((r) => {
mongoConnected = true;
logger.info('MongoDB connected');
}).catch((e) => {
logger.error('ERROR', e);
setTimeout(mongoLoop, 2000);
});
}
mongoLoop();
// fire it up!
const port = process.env.CATALOGUE_SERVER_PORT || '8080';
const MONGO = process.env.MONGO || 'false';
const DOCUMENTDB = process.env.DOCUMENTDB || 'false';
app.listen(port, () => {
logger.info('Started on port', port);
});