-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
68 lines (50 loc) · 1.72 KB
/
index.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
'use strict';
const restify = require('restify');
global.config = require('./config');
const routes = require('./src/routes');
const server = restify.createServer({
name: 'Terra wallet',
version: '1.0.0',
acceptable: [ "application/json" ],
});
server.pre(restify.pre.sanitizePath());
server.use(restify.plugins.authorizationParser());
server.use(restify.plugins.queryParser());
server.use(restify.plugins.bodyParser());
server.use(function (req, res, next) {
res.header('WWW-Authenticate', 'Basic realm="terraWallet"');
if (!req.authorization || !req.authorization.basic || !req.authorization.basic.password) {
res.send(401);
return next(false);
}
if (auth(req.authorization.basic.username, req.authorization.basic.password) === false) {
res.send(401);
return next(false);
}
return next();
});
let PORT = config.PORT || 8080;
process.argv.forEach(function (val, index, array) {
if (val === '--port') {
PORT = process.argv[index + 1];
}
});
server.listen(PORT, function () {
console.log('[%s] %s listening at %s', new Date(), server.name, server.url);
});
process.on('message', function (msg) {
if (msg === 'shutdown') {
console.log('[' + new Date() + ']', 'Closing all connections...');
setTimeout(function () {
console.log('[' + new Date() + ']', 'Finished closing connections');
process.exit(0);
}, 1500);
}
});
server.on('uncaughtException', function (req, res, route, err) {
console.log('[' + new Date() + ']', 'uncaughtException', err.stack);
});
function auth(username, password) {
return username === config.BASIC_AUTH_ID && password === config.BASIC_AUTH_PASSWORD;
}
routes(server);