format app.js to standard
This commit is contained in:
parent
a8b75ca4d2
commit
8caeeb6e54
2 changed files with 63 additions and 83 deletions
123
lib/app.js
123
lib/app.js
|
|
@ -1,63 +1,63 @@
|
||||||
'use strict';
|
'use strict'
|
||||||
|
|
||||||
var http = require('http');
|
var http = require('http')
|
||||||
var https = require('https');
|
var https = require('https')
|
||||||
var express = require('express');
|
var express = require('express')
|
||||||
var LamassuConfig = require('lamassu-config');
|
var LamassuConfig = require('lamassu-config')
|
||||||
var routes = require('./routes');
|
var routes = require('./routes')
|
||||||
var plugins = require('./plugins');
|
var plugins = require('./plugins')
|
||||||
var db = require('./postgresql_interface');
|
var db = require('./postgresql_interface')
|
||||||
var logger = require('./logger');
|
var logger = require('./logger')
|
||||||
|
|
||||||
module.exports = function(options) {
|
module.exports = function (options) {
|
||||||
var app = express();
|
var app = express()
|
||||||
var connectionString;
|
var connectionString
|
||||||
var server;
|
var server
|
||||||
var lamassuConfig;
|
var lamassuConfig
|
||||||
|
|
||||||
connectionString = options.postgres ||
|
connectionString = options.postgres ||
|
||||||
'postgres://lamassu:lamassu@localhost/lamassu';
|
'postgres://lamassu:lamassu@localhost/lamassu'
|
||||||
|
|
||||||
lamassuConfig = new LamassuConfig(connectionString);
|
lamassuConfig = new LamassuConfig(connectionString)
|
||||||
|
|
||||||
db.init(connectionString);
|
db.init(connectionString)
|
||||||
plugins.init(db);
|
plugins.init(db)
|
||||||
|
|
||||||
lamassuConfig.load(function(err, config) {
|
lamassuConfig.load(function (err, config) {
|
||||||
if (err) {
|
if (err) {
|
||||||
logger.error('Loading config failed');
|
logger.error('Loading config failed')
|
||||||
throw err;
|
throw err
|
||||||
}
|
}
|
||||||
|
|
||||||
plugins.configure(config);
|
plugins.configure(config)
|
||||||
plugins.startPolling();
|
plugins.startPolling()
|
||||||
});
|
})
|
||||||
|
|
||||||
lamassuConfig.on('configUpdate', function() {
|
lamassuConfig.on('configUpdate', function () {
|
||||||
lamassuConfig.load(function(err, config) {
|
lamassuConfig.load(function (err, config) {
|
||||||
if (err) {
|
if (err) {
|
||||||
return logger.error('Error while reloading config');
|
return logger.error('Error while reloading config')
|
||||||
}
|
}
|
||||||
|
|
||||||
plugins.configure(config);
|
plugins.configure(config)
|
||||||
logger.info('Config reloaded');
|
logger.info('Config reloaded')
|
||||||
});
|
})
|
||||||
});
|
})
|
||||||
|
|
||||||
app.use(express.bodyParser());
|
app.use(express.bodyParser())
|
||||||
|
|
||||||
var reloadConfigMiddleware = function(req, res, next) {
|
var reloadConfigMiddleware = function (req, res, next) {
|
||||||
lamassuConfig.load(function(err, config) {
|
lamassuConfig.load(function (err, config) {
|
||||||
if (err) {
|
if (err) {
|
||||||
logger.error('Error while reloading config');
|
logger.error('Error while reloading config')
|
||||||
return next(err)
|
return next(err)
|
||||||
}
|
}
|
||||||
plugins.configure(config);
|
plugins.configure(config)
|
||||||
next();
|
next()
|
||||||
});
|
})
|
||||||
};
|
}
|
||||||
|
|
||||||
var authMiddleware;
|
var authMiddleware
|
||||||
|
|
||||||
if (options.https) {
|
if (options.https) {
|
||||||
var serverOptions = {
|
var serverOptions = {
|
||||||
|
|
@ -67,35 +67,34 @@ module.exports = function(options) {
|
||||||
secureProtocol: 'TLSv1_method',
|
secureProtocol: 'TLSv1_method',
|
||||||
ciphers: 'AES128-GCM-SHA256:RC4:HIGH:!MD5:!aNULL:!EDH',
|
ciphers: 'AES128-GCM-SHA256:RC4:HIGH:!MD5:!aNULL:!EDH',
|
||||||
honorCipherOrder: true
|
honorCipherOrder: true
|
||||||
};
|
}
|
||||||
|
|
||||||
server = https.createServer(serverOptions, app);
|
server = https.createServer(serverOptions, app)
|
||||||
|
|
||||||
authMiddleware = function(req, res, next) {
|
authMiddleware = function (req, res, next) {
|
||||||
lamassuConfig.isAuthorized(routes.getFingerprint(req), function(err,
|
lamassuConfig.isAuthorized(routes.getFingerprint(req), function (err,
|
||||||
device) {
|
device) {
|
||||||
if (err) {
|
if (err) {
|
||||||
res.json({err: 'Internal Server Error'});
|
res.json({err: 'Internal Server Error'})
|
||||||
return next(err);
|
return next(err)
|
||||||
}
|
}
|
||||||
if (!device) {
|
if (!device) {
|
||||||
res.json(404, {err: 'Not Found'});
|
res.json(404, {err: 'Not Found'})
|
||||||
return next(new Error('Device is unpaired'));
|
return next(new Error('Device is unpaired'))
|
||||||
}
|
}
|
||||||
next();
|
next()
|
||||||
});
|
})
|
||||||
};
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
server = http.createServer(app);
|
server = http.createServer(app)
|
||||||
|
|
||||||
authMiddleware = function(req, res, next) {
|
authMiddleware = function (req, res, next) {
|
||||||
req.device = {};
|
req.device = {}
|
||||||
return next();
|
return next()
|
||||||
};
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (options.mock) logger.info('In mock mode');
|
if (options.mock) logger.info('In mock mode')
|
||||||
|
|
||||||
var localApp = express()
|
var localApp = express()
|
||||||
localApp.use(express.bodyParser())
|
localApp.use(express.bodyParser())
|
||||||
|
|
@ -110,11 +109,11 @@ module.exports = function(options) {
|
||||||
authMiddleware: authMiddleware,
|
authMiddleware: authMiddleware,
|
||||||
reloadConfigMiddleware: reloadConfigMiddleware,
|
reloadConfigMiddleware: reloadConfigMiddleware,
|
||||||
mock: options.mock
|
mock: options.mock
|
||||||
});
|
})
|
||||||
|
|
||||||
localServer.listen(7070, function () {
|
localServer.listen(7070, function () {
|
||||||
console.log('lamassu-server is listening on local port %d', localPort)
|
console.log('lamassu-server is listening on local port %d', localPort)
|
||||||
})
|
})
|
||||||
|
|
||||||
return server;
|
return server
|
||||||
};
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,3 @@
|
||||||
'use strict'
|
|
||||||
|
|
||||||
require('es6-promise').polyfill()
|
|
||||||
|
|
||||||
var R = require('ramda')
|
var R = require('ramda')
|
||||||
var db = null
|
var db = null
|
||||||
var getBalances = null
|
var getBalances = null
|
||||||
|
|
@ -10,6 +6,7 @@ function init (_db, _getBalances) {
|
||||||
db = _db
|
db = _db
|
||||||
getBalances = _getBalances
|
getBalances = _getBalances
|
||||||
}
|
}
|
||||||
|
exports.init = init
|
||||||
|
|
||||||
function toInt10 (str) { return parseInt(str, 10) }
|
function toInt10 (str) { return parseInt(str, 10) }
|
||||||
|
|
||||||
|
|
@ -114,20 +111,4 @@ function checkStatus () {
|
||||||
return alerts
|
return alerts
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
exports.checkStatus = checkStatus
|
||||||
var _db = require('./postgresql_interface')
|
|
||||||
var connectionString = 'postgres://lamassu:lamassu@localhost/lamassu'
|
|
||||||
|
|
||||||
var _getBalances = function () {
|
|
||||||
return [{cryptoCode: 'BTC', fiatBalance: 12}, {cryptoCode: 'ETH', fiatBalance: 8}]
|
|
||||||
}
|
|
||||||
|
|
||||||
_db.init(connectionString)
|
|
||||||
init(_db, _getBalances)
|
|
||||||
|
|
||||||
checkStatus()
|
|
||||||
.then(function (alerts) {
|
|
||||||
console.log('DEBUG1')
|
|
||||||
console.log('%j', alerts)
|
|
||||||
process.exit(0)
|
|
||||||
})
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue