-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
183 lines (164 loc) · 4.76 KB
/
Copy pathserver.js
File metadata and controls
183 lines (164 loc) · 4.76 KB
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
const instana = require('@instana/collector');
// init tracing
// MUST be done before loading anything else!
instana({
tracing: {
enabled: true
}
});
const { MongoClient, ObjectId } = require('mongodb');
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
let db;
let collection;
let 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((req, res, next) => {
let dcs = [
"asia-northeast2",
"asia-south1",
"europe-west3",
"us-east1",
"us-west1"
];
let span = instana.currentSpan();
span.annotate('custom.sdk.tags.datacenter', dcs[Math.floor(Math.random() * dcs.length)]);
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 available');
}
});
// 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 available');
}
});
// 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');
}
});
// set up Mongo
async function mongoConnect() {
try {
const mongoURL = process.env.MONGO_URL || 'mongodb://mongodb:27017/catalogue';
const client = await MongoClient.connect(mongoURL, { useNewUrlParser: true, useUnifiedTopology: true });
db = client.db('catalogue');
collection = db.collection('products');
mongoConnected = true;
logger.info('MongoDB connected');
} catch (error) {
mongoConnected = false;
logger.error('ERROR', error);
setTimeout(mongoLoop, 2000);
}
}
// mongodb connection retry loop
function mongoLoop() {
mongoConnect().catch((e) => {
logger.error('ERROR', e);
setTimeout(mongoLoop, 2000);
});
}
mongoLoop();
// fire it up!
const port = process.env.CATALOGUE_SERVER_PORT || '8080';
app.listen(port, () => {
logger.info('Started on port', port);
});