This commit is contained in:
Josh Harvey 2016-10-10 14:53:01 +01:00
parent c057fe3df8
commit 259b527214
6 changed files with 68 additions and 81 deletions

View file

@ -4,15 +4,14 @@ var http = require('http')
var https = require('https')
var express = require('express')
var bodyParser = require('body-parser')
var LamassuConfig = require('lamassu-config')
var routes = require('./routes')
var plugins = require('./plugins')
var logger = require('./logger')
var configManager = require('./config-manager')
module.exports = function (options) {
var app = express()
var server
var lamassuConfig
const psqlUrl = options.postgresql
if (!psqlUrl) {
@ -20,33 +19,19 @@ module.exports = function (options) {
process.exit(1)
}
lamassuConfig = new LamassuConfig(psqlUrl)
const seedPath = options.seedPath || './seeds/seed.txt'
plugins.init(psqlUrl, seedPath)
lamassuConfig.load(function (err, config) {
if (err) {
logger.error('Loading config failed')
throw err
}
console.log('DEBUG6')
configManager.load()
.then(config => {
console.log('DEBUG5: %j', config)
plugins.configure(config)
plugins.startPolling()
plugins.startCheckingNotification()
})
lamassuConfig.on('configUpdate', function () {
lamassuConfig.load(function (err, config) {
if (err) {
return logger.error('Error while reloading config')
}
plugins.configure(config)
logger.info('Config reloaded')
})
})
app.use(bodyParser.json())
var authMiddleware
@ -64,24 +49,15 @@ module.exports = function (options) {
server = https.createServer(serverOptions, app)
authMiddleware = function (req, res, next) {
lamassuConfig.isAuthorized(routes.getDeviceId(req), function (err,
device) {
if (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'))
}
next()
})
next() // TODO: authentication
}
} else {
server = http.createServer(app)
authMiddleware = function (req, res, next) {
req.device = {}
console.log('DEBUG2')
console.log(req.route)
return next()
}
}
@ -96,7 +72,6 @@ module.exports = function (options) {
routes.init({
app: app,
localApp: localApp,
lamassuConfig: lamassuConfig,
plugins: plugins,
authMiddleware: authMiddleware,
// reloadConfigMiddleware: reloadConfigMiddleware,