forked from instana/robot-shop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
242 lines (224 loc) · 6.85 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
const instana = require('instana-nodejs-sensor');
// init tracing
// MUST be done before loading anything else!
instana({
tracing: {
enabled: true
}
});
const mongoClient = require('mongodb').MongoClient;
const mongoObjectID = require('mongodb').ObjectID;
const redis = require('redis');
const bodyParser = require('body-parser');
const express = require('express');
// MongoDB
var db;
var usersCollection;
var ordersCollection;
var mongoConnected = false;
const app = express();
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);
});
// use REDIS INCR to track anonymous users
app.get('/uniqueid', (req, res) => {
// get number from Redis
redisClient.incr('anonymous-counter', (err, r) => {
if(!err) {
res.json({
uuid: 'anonymous-' + r
});
} else {
console.log('ERROR', err);
res.status(500).send(err);
}
});
});
// return all users for debugging only
app.get('/users', (req, res) => {
if(mongoConnected) {
usersCollection.find().toArray().then((users) => {
res.json(users);
}).catch((e) => {
console.log('ERROR', e);
res.status(500).send(e);
});
} else {
res.status(500).send('database not available');
}
});
app.post('/login', (req, res) => {
console.log('login', req.body);
if(req.body.name === undefined || req.body.password === undefined) {
res.status(400).send('name or passowrd not supplied');
} else if(mongoConnected) {
usersCollection.findOne({
name: req.body.name,
}).then((user) => {
console.log('user', user);
if(user) {
if(user.password == req.body.password) {
res.json(user);
} else {
res.status(404).send('incorrect password');
}
} else {
res.status(404).send('name not found');
}
}).catch((e) => {
console.log('ERROR', e);
res.status(500).send(e);
});
} else {
res.status(500).send('database not available');
}
});
// TODO - validate email address format
app.post('/register', (req, res) => {
console.log('register', req.body);
if(req.body.name === undefined || req.body.password === undefined || req.body.email === undefined) {
res.status(400).send('insufficient data');
} else if(mongoConnected) {
// check if name already exists
usersCollection.findOne({name: req.body.name}).then((user) => {
if(user) {
res.status(400).send('name already exists');
} else {
// create new user
usersCollection.insertOne({
name: req.body.name,
password: req.body.password,
email: req.body.email
}).then((r) => {
console.log('inserted', r.result);
res.send('OK');
}).catch((e) => {
console.log('ERROR', e);
res.status(500).send(e);
});
}
}).catch((e) => {
console.log('ERROR', e);
res.status(500).send(e);
});
} else {
res.status(500).send('database not available');
}
});
app.post('/order/:id', (req, res) => {
console.log('order', req.body);
// only for registered users
if(mongoConnected) {
usersCollection.findOne({
name: req.params.id
}).then((user) => {
if(user) {
// found user record
// get orders
ordersCollection.findOne({
name: req.params.id
}).then((history) => {
if(history) {
var list = history.history;
list.push(req.body);
ordersCollection.updateOne(
{ name: req.params.id },
{ $set: { history: list }}
).then((r) => {
res.send('OK');
}).catch((e) => {
res.status(500).send(e);
});
} else {
// no history
ordersCollection.insertOne({
name: req.params.id,
history: [ req.body ]
}).then((r) => {
res.send('OK');
}).catch((e) => {
res.status(500).send(e);
});
}
}).catch((e) => {
res.status(500).send(e);
});
} else {
res.status(404).send('name not found');
}
}).catch((e) => {
res.status(500).send(e);
});
} else {
res.status(500).send('database not available');
}
});
app.get('/history/:id', (req, res) => {
if(mongoConnected) {
ordersCollection.findOne({
name: req.params.id
}).then((history) => {
if(history) {
res.json(history);
} else {
res.status(404).send('history not found');
}
}).catch((e) => {
res.status(500).send(e);
});
} else {
res.status(500).send('database not available');
}
});
// connect to Redis
var redisClient = redis.createClient({
host: process.env.REDIS_HOST || 'redis'
});
redisClient.on('error', (e) => {
console.log('Redis ERROR', e);
});
redisClient.on('ready', (r) => {
console.log('Redis READY', r);
});
// set up Mongo
function mongoConnect() {
return new Promise((resolve, reject) => {
var mongoURL = process.env.MONGO_URL || 'mongodb://mongodb:27017/users';
mongoClient.connect(mongoURL, (error, _db) => {
if(error) {
reject(error);
} else {
db = _db;
usersCollection = db.collection('users');
ordersCollection = db.collection('orders');
resolve('connected');
}
});
});
}
function mongoLoop() {
mongoConnect().then((r) => {
mongoConnected = true;
console.log('MongoDB connected');
}).catch((e) => {
console.error('ERROR', e);
setTimeout(mongoLoop, 2000);
});
}
mongoLoop();
// fire it up!
const port = process.env.USER_SERVER_PORT || '8080';
app.listen(port, () => {
console.log('Started on port', port);
});