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 https = require('https');
|
||||
var express = require('express');
|
||||
var LamassuConfig = require('lamassu-config');
|
||||
var routes = require('./routes');
|
||||
var plugins = require('./plugins');
|
||||
var db = require('./postgresql_interface');
|
||||
var logger = require('./logger');
|
||||
var http = require('http')
|
||||
var https = require('https')
|
||||
var express = require('express')
|
||||
var LamassuConfig = require('lamassu-config')
|
||||
var routes = require('./routes')
|
||||
var plugins = require('./plugins')
|
||||
var db = require('./postgresql_interface')
|
||||
var logger = require('./logger')
|
||||
|
||||
module.exports = function(options) {
|
||||
var app = express();
|
||||
var connectionString;
|
||||
var server;
|
||||
var lamassuConfig;
|
||||
module.exports = function (options) {
|
||||
var app = express()
|
||||
var connectionString
|
||||
var server
|
||||
var lamassuConfig
|
||||
|
||||
connectionString = options.postgres ||
|
||||
'postgres://lamassu:lamassu@localhost/lamassu';
|
||||
'postgres://lamassu:lamassu@localhost/lamassu'
|
||||
|
||||
lamassuConfig = new LamassuConfig(connectionString);
|
||||
lamassuConfig = new LamassuConfig(connectionString)
|
||||
|
||||
db.init(connectionString);
|
||||
plugins.init(db);
|
||||
db.init(connectionString)
|
||||
plugins.init(db)
|
||||
|
||||
lamassuConfig.load(function(err, config) {
|
||||
lamassuConfig.load(function (err, config) {
|
||||
if (err) {
|
||||
logger.error('Loading config failed');
|
||||
throw err;
|
||||
logger.error('Loading config failed')
|
||||
throw err
|
||||
}
|
||||
|
||||
plugins.configure(config);
|
||||
plugins.startPolling();
|
||||
});
|
||||
plugins.configure(config)
|
||||
plugins.startPolling()
|
||||
})
|
||||
|
||||
lamassuConfig.on('configUpdate', function() {
|
||||
lamassuConfig.load(function(err, config) {
|
||||
lamassuConfig.on('configUpdate', function () {
|
||||
lamassuConfig.load(function (err, config) {
|
||||
if (err) {
|
||||
return logger.error('Error while reloading config');
|
||||
return logger.error('Error while reloading config')
|
||||
}
|
||||
|
||||
plugins.configure(config);
|
||||
logger.info('Config reloaded');
|
||||
});
|
||||
});
|
||||
plugins.configure(config)
|
||||
logger.info('Config reloaded')
|
||||
})
|
||||
})
|
||||
|
||||
app.use(express.bodyParser());
|
||||
app.use(express.bodyParser())
|
||||
|
||||
var reloadConfigMiddleware = function(req, res, next) {
|
||||
lamassuConfig.load(function(err, config) {
|
||||
var reloadConfigMiddleware = function (req, res, next) {
|
||||
lamassuConfig.load(function (err, config) {
|
||||
if (err) {
|
||||
logger.error('Error while reloading config');
|
||||
logger.error('Error while reloading config')
|
||||
return next(err)
|
||||
}
|
||||
plugins.configure(config);
|
||||
next();
|
||||
});
|
||||
};
|
||||
plugins.configure(config)
|
||||
next()
|
||||
})
|
||||
}
|
||||
|
||||
var authMiddleware;
|
||||
var authMiddleware
|
||||
|
||||
if (options.https) {
|
||||
var serverOptions = {
|
||||
|
|
@ -67,35 +67,34 @@ module.exports = function(options) {
|
|||
secureProtocol: 'TLSv1_method',
|
||||
ciphers: 'AES128-GCM-SHA256:RC4:HIGH:!MD5:!aNULL:!EDH',
|
||||
honorCipherOrder: true
|
||||
};
|
||||
}
|
||||
|
||||
server = https.createServer(serverOptions, app);
|
||||
server = https.createServer(serverOptions, app)
|
||||
|
||||
authMiddleware = function(req, res, next) {
|
||||
lamassuConfig.isAuthorized(routes.getFingerprint(req), function(err,
|
||||
device) {
|
||||
authMiddleware = function (req, res, next) {
|
||||
lamassuConfig.isAuthorized(routes.getFingerprint(req), function (err,
|
||||
device) {
|
||||
if (err) {
|
||||
res.json({err: 'Internal Server Error'});
|
||||
return next(err);
|
||||
res.json({err: 'Internal Server Error'})
|
||||
return next(err)
|
||||
}
|
||||
if (!device) {
|
||||
res.json(404, {err: 'Not Found'});
|
||||
return next(new Error('Device is unpaired'));
|
||||
res.json(404, {err: 'Not Found'})
|
||||
return next(new Error('Device is unpaired'))
|
||||
}
|
||||
next();
|
||||
});
|
||||
};
|
||||
|
||||
next()
|
||||
})
|
||||
}
|
||||
} else {
|
||||
server = http.createServer(app);
|
||||
server = http.createServer(app)
|
||||
|
||||
authMiddleware = function(req, res, next) {
|
||||
req.device = {};
|
||||
return next();
|
||||
};
|
||||
authMiddleware = function (req, res, next) {
|
||||
req.device = {}
|
||||
return next()
|
||||
}
|
||||
}
|
||||
|
||||
if (options.mock) logger.info('In mock mode');
|
||||
if (options.mock) logger.info('In mock mode')
|
||||
|
||||
var localApp = express()
|
||||
localApp.use(express.bodyParser())
|
||||
|
|
@ -110,11 +109,11 @@ module.exports = function(options) {
|
|||
authMiddleware: authMiddleware,
|
||||
reloadConfigMiddleware: reloadConfigMiddleware,
|
||||
mock: options.mock
|
||||
});
|
||||
})
|
||||
|
||||
localServer.listen(7070, function () {
|
||||
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 db = null
|
||||
var getBalances = null
|
||||
|
|
@ -10,6 +6,7 @@ function init (_db, _getBalances) {
|
|||
db = _db
|
||||
getBalances = _getBalances
|
||||
}
|
||||
exports.init = init
|
||||
|
||||
function toInt10 (str) { return parseInt(str, 10) }
|
||||
|
||||
|
|
@ -114,20 +111,4 @@ function checkStatus () {
|
|||
return alerts
|
||||
})
|
||||
}
|
||||
|
||||
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)
|
||||
})
|
||||
exports.checkStatus = checkStatus
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue