WIP
This commit is contained in:
parent
0aa457680c
commit
47db8e9ead
7 changed files with 136 additions and 114 deletions
|
|
@ -2,6 +2,9 @@
|
||||||
|
|
||||||
const app = require('../lib/app')
|
const app = require('../lib/app')
|
||||||
|
|
||||||
process.on('unhandledRejection', err => console.log(err.stack))
|
process.on('unhandledRejection', err => {
|
||||||
|
console.log('Unhandled rejection')
|
||||||
|
console.log(err.stack)
|
||||||
|
})
|
||||||
|
|
||||||
app.run()
|
app.run()
|
||||||
|
|
|
||||||
80
lib/app.js
80
lib/app.js
|
|
@ -2,6 +2,8 @@ const fs = require('fs')
|
||||||
const http = require('http')
|
const http = require('http')
|
||||||
const https = require('https')
|
const https = require('https')
|
||||||
const express = require('express')
|
const express = require('express')
|
||||||
|
const loop = require('reoccur')
|
||||||
|
|
||||||
const routes = require('./routes')
|
const routes = require('./routes')
|
||||||
const plugins = require('./plugins')
|
const plugins = require('./plugins')
|
||||||
const logger = require('./logger')
|
const logger = require('./logger')
|
||||||
|
|
@ -13,6 +15,20 @@ const options = require('./options')
|
||||||
const devMode = argv.dev || argv.http || options.http
|
const devMode = argv.dev || argv.http || options.http
|
||||||
|
|
||||||
function run () {
|
function run () {
|
||||||
|
let count = 0
|
||||||
|
return loop((recur, resolve, reject) => {
|
||||||
|
return runOnce()
|
||||||
|
.then(resolve)
|
||||||
|
.catch(err => {
|
||||||
|
count += 1
|
||||||
|
logger.debug(err)
|
||||||
|
logger.debug('[%d] Retrying in 10s...', count)
|
||||||
|
setTimeout(recur, 10000)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
function runOnce () {
|
||||||
const app = express()
|
const app = express()
|
||||||
const localApp = express()
|
const localApp = express()
|
||||||
|
|
||||||
|
|
@ -21,47 +37,45 @@ function run () {
|
||||||
|
|
||||||
return configManager.load()
|
return configManager.load()
|
||||||
.then(config => {
|
.then(config => {
|
||||||
plugins.configure(config)
|
return plugins.configure(config)
|
||||||
plugins.startPolling()
|
.then(() => {
|
||||||
plugins.startCheckingNotification()
|
plugins.startPolling()
|
||||||
|
plugins.startCheckingNotification()
|
||||||
|
|
||||||
const httpsServerOptions = {
|
const httpsServerOptions = {
|
||||||
key: fs.readFileSync(options.keyPath),
|
key: fs.readFileSync(options.keyPath),
|
||||||
cert: fs.readFileSync(options.certPath),
|
cert: fs.readFileSync(options.certPath),
|
||||||
requestCert: true
|
requestCert: true
|
||||||
}
|
}
|
||||||
|
|
||||||
const server = devMode
|
const server = devMode
|
||||||
? http.createServer(app)
|
? http.createServer(app)
|
||||||
: https.createServer(httpsServerOptions, app)
|
: https.createServer(httpsServerOptions, app)
|
||||||
|
|
||||||
const port = 3000
|
const port = 3000
|
||||||
const localPort = 3030
|
const localPort = 3030
|
||||||
const localServer = http.createServer(localApp)
|
const localServer = http.createServer(localApp)
|
||||||
|
|
||||||
if (options.devMode) logger.info('In dev mode')
|
if (options.devMode) logger.info('In dev mode')
|
||||||
|
|
||||||
const opts = {
|
const opts = {
|
||||||
app,
|
app,
|
||||||
localApp,
|
localApp,
|
||||||
devMode,
|
devMode,
|
||||||
plugins
|
plugins
|
||||||
}
|
}
|
||||||
|
|
||||||
routes.init(opts)
|
routes.init(opts)
|
||||||
|
|
||||||
server.listen(port, () => {
|
server.listen(port, () => {
|
||||||
console.log('lamassu-server listening on port ' +
|
console.log('lamassu-server listening on port ' +
|
||||||
port + ' ' + (devMode ? '(http)' : '(https)'))
|
port + ' ' + (devMode ? '(http)' : '(https)'))
|
||||||
|
})
|
||||||
|
|
||||||
|
localServer.listen(localPort, 'localhost', () => {
|
||||||
|
console.log('lamassu-server listening on local port ' + localPort)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
localServer.listen(localPort, 'localhost', () => {
|
|
||||||
console.log('lamassu-server listening on local port ' + localPort)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
.catch(err => {
|
|
||||||
console.error(err)
|
|
||||||
process.exit(1)
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -12,8 +12,9 @@ function loadAccounts () {
|
||||||
const toFields = fieldArr => R.fromPairs(R.map(r => [r.code, r.value], fieldArr))
|
const toFields = fieldArr => R.fromPairs(R.map(r => [r.code, r.value], fieldArr))
|
||||||
const toPairs = r => [r.code, toFields(r.fields)]
|
const toPairs = r => [r.code, toFields(r.fields)]
|
||||||
|
|
||||||
return db.one('select data from user_config where type=$1', 'accounts')
|
return db.oneOrNone('select data from user_config where type=$1', 'accounts')
|
||||||
.then(function (data) {
|
.then(function (data) {
|
||||||
|
if (!data) return {}
|
||||||
return R.fromPairs(R.map(toPairs, data.data.accounts))
|
return R.fromPairs(R.map(toPairs, data.data.accounts))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
||||||
151
lib/plugins.js
151
lib/plugins.js
|
|
@ -165,96 +165,95 @@ exports.loadOrConfigPlugin = loadOrConfigPlugin
|
||||||
exports.configure = function configure (config) {
|
exports.configure = function configure (config) {
|
||||||
cachedConfig = config
|
cachedConfig = config
|
||||||
|
|
||||||
const accounts = configManager.loadAccounts()
|
|
||||||
|
|
||||||
const cryptoCodes = getCryptoCodes()
|
const cryptoCodes = getCryptoCodes()
|
||||||
|
|
||||||
console.log('DEBUG30')
|
console.log('DEBUG30')
|
||||||
|
|
||||||
cryptoCodes.forEach(cryptoCode => {
|
return configManager.loadAccounts()
|
||||||
console.log('DEBUG31')
|
.then(accounts => {
|
||||||
const cryptoScopedConfig = configManager.cryptoScoped(cryptoCode, cachedConfig)
|
cryptoCodes.forEach(cryptoCode => {
|
||||||
|
console.log('DEBUG31')
|
||||||
|
const cryptoScopedConfig = configManager.cryptoScoped(cryptoCode, cachedConfig)
|
||||||
|
|
||||||
console.log('DEBUG31.1')
|
console.log('DEBUG31.1')
|
||||||
|
|
||||||
// TICKER [required] configure (or load)
|
// TICKER [required] configure (or load)
|
||||||
loadOrConfigPlugin(
|
loadOrConfigPlugin(
|
||||||
tickerPlugins[cryptoCode],
|
tickerPlugins[cryptoCode],
|
||||||
'ticker',
|
'ticker',
|
||||||
cryptoCode,
|
cryptoCode,
|
||||||
cryptoScopedConfig,
|
cryptoScopedConfig,
|
||||||
accounts,
|
accounts,
|
||||||
{currency: deviceCurrency},
|
{currency: deviceCurrency},
|
||||||
function onTickerChange (newTicker) {
|
function onTickerChange (newTicker) {
|
||||||
tickerPlugins[cryptoCode] = newTicker
|
tickerPlugins[cryptoCode] = newTicker
|
||||||
pollRate(cryptoCode)
|
pollRate(cryptoCode)
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
console.log('DEBUG31.2')
|
console.log('DEBUG31.2')
|
||||||
|
|
||||||
// Give each crypto a different derived seed so as not to allow any
|
// Give each crypto a different derived seed so as not to allow any
|
||||||
// plugin to spend another plugin's funds
|
// plugin to spend another plugin's funds
|
||||||
const cryptoSeed = hkdf.derive(cryptoCode, 32)
|
const cryptoSeed = hkdf.derive(cryptoCode, 32)
|
||||||
|
|
||||||
loadOrConfigPlugin(
|
loadOrConfigPlugin(
|
||||||
walletPlugins[cryptoCode],
|
walletPlugins[cryptoCode],
|
||||||
'wallet',
|
'wallet',
|
||||||
cryptoCode,
|
cryptoCode,
|
||||||
cryptoScopedConfig,
|
cryptoScopedConfig,
|
||||||
accounts,
|
accounts,
|
||||||
{masterSeed: cryptoSeed},
|
{masterSeed: cryptoSeed},
|
||||||
function onWalletChange (newWallet) {
|
function onWalletChange (newWallet) {
|
||||||
walletPlugins[cryptoCode] = newWallet
|
walletPlugins[cryptoCode] = newWallet
|
||||||
pollBalance(cryptoCode)
|
pollBalance(cryptoCode)
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
tradesQueues[cryptoCode] = tradesQueues[cryptoCode] || []
|
tradesQueues[cryptoCode] = tradesQueues[cryptoCode] || []
|
||||||
|
|
||||||
loadOrConfigPlugin(
|
loadOrConfigPlugin(
|
||||||
traderPlugins[cryptoCode],
|
traderPlugins[cryptoCode],
|
||||||
'trader',
|
'trader',
|
||||||
cryptoCode,
|
cryptoCode,
|
||||||
cryptoScopedConfig,
|
cryptoScopedConfig,
|
||||||
accounts,
|
accounts,
|
||||||
|
null,
|
||||||
|
function onTraderChange (newTrader) {
|
||||||
|
traderPlugins[cryptoCode] = newTrader
|
||||||
|
if (newTrader === null) stopTrader(cryptoCode)
|
||||||
|
else startTrader(cryptoCode)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
const unscopedCfg = configManager.unscoped(cachedConfig)
|
||||||
|
|
||||||
|
// ID VERIFIER [optional] configure (or load)
|
||||||
|
idVerifierPlugin = loadOrConfigPlugin(
|
||||||
|
idVerifierPlugin,
|
||||||
|
'idVerifier',
|
||||||
null,
|
null,
|
||||||
function onTraderChange (newTrader) {
|
unscopedCfg,
|
||||||
traderPlugins[cryptoCode] = newTrader
|
accounts
|
||||||
if (newTrader === null) stopTrader(cryptoCode)
|
)
|
||||||
else startTrader(cryptoCode)
|
|
||||||
}
|
emailPlugin = loadOrConfigPlugin(
|
||||||
|
emailPlugin,
|
||||||
|
'email',
|
||||||
|
null,
|
||||||
|
unscopedCfg,
|
||||||
|
accounts
|
||||||
|
)
|
||||||
|
|
||||||
|
smsPlugin = loadOrConfigPlugin(
|
||||||
|
smsPlugin,
|
||||||
|
'sms',
|
||||||
|
null,
|
||||||
|
unscopedCfg,
|
||||||
|
accounts
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
const unscopedCfg = configManager.unscoped(cachedConfig)
|
|
||||||
|
|
||||||
// ID VERIFIER [optional] configure (or load)
|
|
||||||
idVerifierPlugin = loadOrConfigPlugin(
|
|
||||||
idVerifierPlugin,
|
|
||||||
'idVerifier',
|
|
||||||
null,
|
|
||||||
unscopedCfg,
|
|
||||||
accounts
|
|
||||||
)
|
|
||||||
|
|
||||||
emailPlugin = loadOrConfigPlugin(
|
|
||||||
emailPlugin,
|
|
||||||
'email',
|
|
||||||
null,
|
|
||||||
unscopedCfg,
|
|
||||||
accounts
|
|
||||||
)
|
|
||||||
|
|
||||||
smsPlugin = loadOrConfigPlugin(
|
|
||||||
smsPlugin,
|
|
||||||
'sms',
|
|
||||||
null,
|
|
||||||
unscopedCfg,
|
|
||||||
accounts
|
|
||||||
)
|
|
||||||
|
|
||||||
console.log('DEBUG33')
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function getConfig (machineId) {
|
function getConfig (machineId) {
|
||||||
|
|
|
||||||
|
|
@ -87,7 +87,7 @@ function poll (req, res) {
|
||||||
console.log('DEBUG2: %j, %s, %s', reboots, reboot, pid)
|
console.log('DEBUG2: %j, %s, %s', reboots, reboot, pid)
|
||||||
const langs = config.languages.machineLanguages
|
const langs = config.languages.machineLanguages
|
||||||
|
|
||||||
console.log('DEBUG33')
|
console.log('DEBUG33.1')
|
||||||
|
|
||||||
const locale = {
|
const locale = {
|
||||||
currency: config.currencies.fiatCurrency,
|
currency: config.currencies.fiatCurrency,
|
||||||
|
|
@ -411,8 +411,8 @@ function init (opts) {
|
||||||
localApp.post('/dbChange', (req, res) => {
|
localApp.post('/dbChange', (req, res) => {
|
||||||
return configManager.load()
|
return configManager.load()
|
||||||
.then(config => {
|
.then(config => {
|
||||||
plugins.configure(config)
|
return plugins.configure(config)
|
||||||
logger.info('Config reloaded')
|
.then(() => logger.info('Config reloaded'))
|
||||||
})
|
})
|
||||||
.catch(logger.error)
|
.catch(logger.error)
|
||||||
})
|
})
|
||||||
|
|
|
||||||
|
|
@ -35,6 +35,7 @@
|
||||||
"pify": "^2.3.0",
|
"pify": "^2.3.0",
|
||||||
"pretty-ms": "^2.1.0",
|
"pretty-ms": "^2.1.0",
|
||||||
"ramda": "^0.21.0",
|
"ramda": "^0.21.0",
|
||||||
|
"reoccur": "^1.0.0",
|
||||||
"winston": "^2.3.0"
|
"winston": "^2.3.0"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
|
|
|
||||||
|
|
@ -1301,6 +1301,10 @@ referrer-policy@1.0.0:
|
||||||
version "1.0.0"
|
version "1.0.0"
|
||||||
resolved "https://registry.yarnpkg.com/referrer-policy/-/referrer-policy-1.0.0.tgz#f60eedc92f942b01a6118121ec932d66e8fd7e14"
|
resolved "https://registry.yarnpkg.com/referrer-policy/-/referrer-policy-1.0.0.tgz#f60eedc92f942b01a6118121ec932d66e8fd7e14"
|
||||||
|
|
||||||
|
reoccur:
|
||||||
|
version "1.0.0"
|
||||||
|
resolved "https://registry.yarnpkg.com/reoccur/-/reoccur-1.0.0.tgz#62578914654cfe7583caf7a609ed30a3e9a4197d"
|
||||||
|
|
||||||
repeating@^2.0.0:
|
repeating@^2.0.0:
|
||||||
version "2.0.1"
|
version "2.0.1"
|
||||||
resolved "https://registry.yarnpkg.com/repeating/-/repeating-2.0.1.tgz#5214c53a926d3552707527fbab415dbc08d06dda"
|
resolved "https://registry.yarnpkg.com/repeating/-/repeating-2.0.1.tgz#5214c53a926d3552707527fbab415dbc08d06dda"
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue