-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
216 lines (197 loc) · 5.64 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
var Bcrypt = require('bcrypt-nodejs');
var Hapi = require('hapi');
var Basic = require('hapi-auth-basic');
var Good = require('good');
var Joi = require('joi');
var oxr = require('open-exchange-rates');
oxr.set({ app_id: '84c877f978ef488ba62286eeafb18fe8' });
var fx = require('money');
var schema = {
baseCurrency: Joi.string().regex(/[a-zA-Z]{3}/)
};
var currencies = ["USD", "GBP", "CZK", "JPY", "EUR"];
var server = new Hapi.Server();
server.connection({ port: 3000 });
//Users
//john has user roles admin and user
//bob only user
//password for both users is 'secret'
var users = {
'john@test.com': {
username: 'john@test.com',
password: '$2a$10$iqJSHD.BGr0E2IxQwYgJmeP3NvhPrXAeLSaGCj6IR/XU5QtjVu5Tm', // 'secret'
name: 'John Doe',
id: '2133d32a',
roles: ['admin', 'user']
},
'bob@test.com': {
username: 'bob@test.com',
password: '$2a$10$iqJSHD.BGr0E2IxQwYgJmeP3NvhPrXAeLSaGCj6IR/XU5QtjVu5Tm', // 'secret'
name: 'Bob Doe',
id: '2133d32b',
roles: ['user']
}
};
//Orders
var orders = {
1: {
username: 'bob@test.com',
items: [1, 4, 5, 99],
createdAt: '2011-05-04'
},
2: {
username: 'bob@test.com',
items: [1, 4, 5, 99],
createdAt: '2012-05-21'
},
3: {
username: 'john@test.com',
items: [1, 3, 10, 54, 87],
createdAt: '2013-07-11'
},
4: {
username: 'xy@test.com',
items: [1, 4, 3],
createdAt: '2014-08-01'
}
};
var validate = function (request, username, password, callback) {
server.log('debug', 'Validatin user ' + username);
var user = users[username];
if (!user) {
return callback(null, false);
}
Bcrypt.compare(password, user.password, function (err, isValid) {
callback(err, isValid, { id: user.id, name: user.name, username: user.username});
});
};
var isAdmin = function (username){
server.log('debug', 'Checking user ' + username);
var currentUser = users[username];
if (currentUser.roles.indexOf('admin') > -1){
return true;
}
return false;
}
/**
* @api {get} /orders Get user orders.
* @apiName GetOrders
* @apiGroup Orders
* @apiSuccess {String} username Username of user, who created the order
* @apiSuccess {Number[]} items IDs of products in the order
* @apiSuccess {String} createdAt Date of creation of the order
* @apiSuccessExample Success-Response:
* HTTP/1.1 200 OK
* {
* "username": "john@test.com",
* "items": [1,2,3],
* "createdAt" : "2015-07-08"
* }
*/
server.register(Basic, function (err) {
server.auth.strategy('simple', 'basic', { validateFunc: validate });
server.route({
method: 'GET',
path: '/orders',
config: {
auth: 'simple',
handler: function (request, reply) {
var result = [];
if (isAdmin(request.auth.credentials.username)){
server.log('info', 'User is admin, returning all users info');
Object.keys(orders).forEach(function(orderKey){
result.push(orders[orderKey]);
});
reply(result);
} else {
server.log('info', 'User is not admin. Showing just his information');
Object.keys(orders).forEach(function(orderKey){
if (orders[orderKey].username == request.auth.credentials.username){
result.push(orders[orderKey]);
}
});
reply(result);
}
}
}
});
});
/**
* @api {get} /exchange-rates/{baseCurrency} Get exchange rates
* @apiName GetExchangeRates
* @apiGroup ExchangeRates
* @apiSuccess {Double} exchangeRate Exchange rate according to currency code
* @apiExample {curl} Example usage:
* curl -XGET http://localhost/exchange-rates/USD
* @apiSuccessExample Success-Response:
* HTTP/1.1 200 OK
* {
* "CZE": 1.15,
* "GBP": 0.45
* }
* @apiError {String} errorMessage Message with error description
*/
server.route({
method: 'GET',
path: '/exchange-rates/{baseCurrency}',
handler: function (request, reply) {
var result = [];
var validationResult = Joi.validate(request.params, schema);
if (!validationResult.error){
server.log('debug', 'Returning exchange rates for base currency: ' + request.params.baseCurrency);
var baseCurrency = request.params.baseCurrency;
console.log(validationResult);
var rates = {};
//Get latest exchange rates
oxr.latest(function() {
fx.rates = oxr.rates;
fx.base = oxr.base;
currencies.forEach(function(cur){
if (cur.indexOf(baseCurrency) == -1){
server.log('debug', 'Getting exchange rate for currency ' + cur);
var rate = fx(1).from(baseCurrency).to(cur);
var rateObj = {};
rateObj[cur] = rate;
result.push(rateObj);
}
});
reply(result);
});
} else {
reply({"errorMessage" : "Provide 3 character currency code. (GBP, USD, EUR, JPY, CZK)."});
}
}
});
server.register(require('inert'), function (err) {
if (err) {
throw err;
}
server.route({
method: 'GET',
path: '/{param*}',
handler: {
directory: {
path: 'apidoc'
}
}
});
});
server.register({
register: Good,
options: {
reporters: [{
reporter: require('good-console'),
events: {
response: '*',
log: '*'
}
}]
}
}, function (err) {
if (err) {
throw err; // something bad happened loading the plugin
}
server.start(function () {
server.log('info', 'Server running at: ' + server.info.uri);
});
});