updates wallet_balance to account_balance #908
2 changed files with 98 additions and 66 deletions
110
server/index.js
110
server/index.js
|
@ -13,7 +13,7 @@ const httpContext = require('express-http-context');
|
||||||
const db = require('./models');
|
const db = require('./models');
|
||||||
const requestLogger = require('./middleware/requestLogger');
|
const requestLogger = require('./middleware/requestLogger');
|
||||||
const createDatabaseIfNotExists = require('./models/utils/createDatabaseIfNotExists');
|
const createDatabaseIfNotExists = require('./models/utils/createDatabaseIfNotExists');
|
||||||
const { getWalletBalance } = require('./lbrynet/index');
|
const { getAccountBalance } = require('./lbrynet/index');
|
||||||
const configureLogging = require('./utils/configureLogging');
|
const configureLogging = require('./utils/configureLogging');
|
||||||
const configureSlack = require('./utils/configureSlack');
|
const configureSlack = require('./utils/configureSlack');
|
||||||
const { setupBlockList } = require('./utils/blockList');
|
const { setupBlockList } = require('./utils/blockList');
|
||||||
|
@ -27,10 +27,7 @@ const {
|
||||||
|
|
||||||
const {
|
const {
|
||||||
details: { port: PORT, blockListEndpoint },
|
details: { port: PORT, blockListEndpoint },
|
||||||
startup: {
|
startup: { performChecks, performUpdates },
|
||||||
performChecks,
|
|
||||||
performUpdates,
|
|
||||||
},
|
|
||||||
} = require('@config/siteConfig');
|
} = require('@config/siteConfig');
|
||||||
|
|
||||||
const { sessionKey } = require('@private/authConfig.json');
|
const { sessionKey } = require('@private/authConfig.json');
|
||||||
|
@ -38,7 +35,7 @@ const { sessionKey } = require('@private/authConfig.json');
|
||||||
// configure.js doesn't handle new keys in config.json files yet. Make sure it doens't break.
|
// configure.js doesn't handle new keys in config.json files yet. Make sure it doens't break.
|
||||||
let finalBlockListEndpoint;
|
let finalBlockListEndpoint;
|
||||||
|
|
||||||
function Server () {
|
function Server() {
|
||||||
this.initialize = () => {
|
this.initialize = () => {
|
||||||
// configure logging
|
// configure logging
|
||||||
configureLogging();
|
configureLogging();
|
||||||
|
@ -53,12 +50,16 @@ function Server () {
|
||||||
const webpack = require('webpack');
|
const webpack = require('webpack');
|
||||||
const webpackDevMiddleware = require('webpack-dev-middleware');
|
const webpackDevMiddleware = require('webpack-dev-middleware');
|
||||||
|
|
||||||
const webpackClientConfig = require('../webpack/webpack.client.config')(null, { mode: 'development' });
|
const webpackClientConfig = require('../webpack/webpack.client.config')(null, {
|
||||||
|
mode: 'development',
|
||||||
|
});
|
||||||
const clientCompiler = webpack(webpackClientConfig);
|
const clientCompiler = webpack(webpackClientConfig);
|
||||||
|
|
||||||
app.use(webpackDevMiddleware(clientCompiler, {
|
app.use(
|
||||||
publicPath: webpackClientConfig.output.publicPath,
|
webpackDevMiddleware(clientCompiler, {
|
||||||
}));
|
publicPath: webpackClientConfig.output.publicPath,
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
app.use(require('webpack-hot-middleware')(clientCompiler));
|
app.use(require('webpack-hot-middleware')(clientCompiler));
|
||||||
}
|
}
|
||||||
|
@ -67,8 +68,15 @@ function Server () {
|
||||||
app.enable('trust proxy');
|
app.enable('trust proxy');
|
||||||
|
|
||||||
app.use((req, res, next) => {
|
app.use((req, res, next) => {
|
||||||
if (req.get('User-Agent') === 'Mozilla/5.0 (Windows NT 5.1; rv:14.0) Gecko/20120405 Firefox/14.0a1') {
|
if (
|
||||||
res.status(403).send('<h1>Forbidden</h1>If you are seeing this by mistake, please contact us using <a href="https://chat.lbry.io/">https://chat.lbry.io/</a>');
|
req.get('User-Agent') ===
|
||||||
|
'Mozilla/5.0 (Windows NT 5.1; rv:14.0) Gecko/20120405 Firefox/14.0a1'
|
||||||
|
) {
|
||||||
|
res
|
||||||
|
.status(403)
|
||||||
|
.send(
|
||||||
|
'<h1>Forbidden</h1>If you are seeing this by mistake, please contact us using <a href="https://chat.lbry.io/">https://chat.lbry.io/</a>'
|
||||||
|
);
|
||||||
res.end();
|
res.end();
|
||||||
} else {
|
} else {
|
||||||
next();
|
next();
|
||||||
|
@ -101,38 +109,45 @@ function Server () {
|
||||||
app.use(requestLogger);
|
app.use(requestLogger);
|
||||||
|
|
||||||
// initialize passport
|
// initialize passport
|
||||||
app.use(cookieSession({
|
app.use(
|
||||||
name: 'session',
|
cookieSession({
|
||||||
keys: [sessionKey],
|
name: 'session',
|
||||||
}));
|
keys: [sessionKey],
|
||||||
|
})
|
||||||
|
);
|
||||||
app.use(speechPassport.initialize());
|
app.use(speechPassport.initialize());
|
||||||
app.use(speechPassport.session());
|
app.use(speechPassport.session());
|
||||||
|
|
||||||
// configure handlebars & register it with express app
|
// configure handlebars & register it with express app
|
||||||
const viewsPath = Path.resolve(process.cwd(), 'server/views');
|
const viewsPath = Path.resolve(process.cwd(), 'server/views');
|
||||||
app.engine('handlebars', expressHandlebars({
|
app.engine(
|
||||||
async : false,
|
'handlebars',
|
||||||
dataType : 'text',
|
expressHandlebars({
|
||||||
defaultLayout: 'embed',
|
async: false,
|
||||||
partialsDir : Path.join(viewsPath, '/partials'),
|
dataType: 'text',
|
||||||
layoutsDir : Path.join(viewsPath, '/layouts'),
|
defaultLayout: 'embed',
|
||||||
}));
|
partialsDir: Path.join(viewsPath, '/partials'),
|
||||||
|
layoutsDir: Path.join(viewsPath, '/layouts'),
|
||||||
|
})
|
||||||
|
);
|
||||||
app.set('views', viewsPath);
|
app.set('views', viewsPath);
|
||||||
app.set('view engine', 'handlebars');
|
app.set('view engine', 'handlebars');
|
||||||
|
|
||||||
// set the routes on the app
|
// set the routes on the app
|
||||||
const routes = require('./routes');
|
const routes = require('./routes');
|
||||||
|
|
||||||
Object.keys(routes).map((routePath) => {
|
Object.keys(routes).map(routePath => {
|
||||||
let routeData = routes[routePath];
|
let routeData = routes[routePath];
|
||||||
let routeMethod = routeData.hasOwnProperty('method') ? routeData.method : 'get';
|
let routeMethod = routeData.hasOwnProperty('method') ? routeData.method : 'get';
|
||||||
let controllers = Array.isArray(routeData.controller) ? routeData.controller : [routeData.controller];
|
let controllers = Array.isArray(routeData.controller)
|
||||||
|
? routeData.controller
|
||||||
|
: [routeData.controller];
|
||||||
|
|
||||||
app[routeMethod](
|
app[routeMethod](
|
||||||
routePath,
|
routePath,
|
||||||
logMetricsMiddleware,
|
logMetricsMiddleware,
|
||||||
setRouteDataInContextMiddleware(routePath, routeData),
|
setRouteDataInContextMiddleware(routePath, routeData),
|
||||||
...controllers,
|
...controllers
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -153,22 +168,18 @@ function Server () {
|
||||||
};
|
};
|
||||||
this.syncDatabase = () => {
|
this.syncDatabase = () => {
|
||||||
logger.info(`Syncing database...`);
|
logger.info(`Syncing database...`);
|
||||||
return createDatabaseIfNotExists()
|
return createDatabaseIfNotExists().then(() => {
|
||||||
.then(() => {
|
db.sequelize.sync();
|
||||||
db.sequelize.sync();
|
});
|
||||||
});
|
|
||||||
};
|
};
|
||||||
this.performChecks = () => {
|
this.performChecks = () => {
|
||||||
if (!performChecks) {
|
if (!performChecks) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
logger.info(`Performing checks...`);
|
logger.info(`Performing checks...`);
|
||||||
return Promise.all([
|
return Promise.all([getAccountBalance()]).then(([accountBalance]) => {
|
||||||
getWalletBalance(),
|
logger.info('Starting LBC balance:', accountBalance);
|
||||||
])
|
});
|
||||||
.then(([walletBalance]) => {
|
|
||||||
logger.info('Starting LBC balance:', walletBalance);
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
this.performUpdates = () => {
|
this.performUpdates = () => {
|
||||||
|
@ -178,27 +189,29 @@ function Server () {
|
||||||
if (blockListEndpoint) {
|
if (blockListEndpoint) {
|
||||||
finalBlockListEndpoint = blockListEndpoint;
|
finalBlockListEndpoint = blockListEndpoint;
|
||||||
} else if (!blockListEndpoint) {
|
} else if (!blockListEndpoint) {
|
||||||
if (typeof (blockListEndpoint) !== 'string') {
|
if (typeof blockListEndpoint !== 'string') {
|
||||||
logger.warn('blockListEndpoint is null due to outdated siteConfig file. \n' +
|
logger.warn(
|
||||||
'Continuing with default LBRY blocklist api endpoint. \n ' +
|
'blockListEndpoint is null due to outdated siteConfig file. \n' +
|
||||||
'(Specify /"blockListEndpoint" : ""/ to disable.');
|
'Continuing with default LBRY blocklist api endpoint. \n ' +
|
||||||
|
'(Specify /"blockListEndpoint" : ""/ to disable.'
|
||||||
|
);
|
||||||
finalBlockListEndpoint = 'https://api.lbry.io/file/list_blocked';
|
finalBlockListEndpoint = 'https://api.lbry.io/file/list_blocked';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
logger.info(`Peforming updates...`);
|
logger.info(`Peforming updates...`);
|
||||||
if (!finalBlockListEndpoint) {
|
if (!finalBlockListEndpoint) {
|
||||||
logger.info('Configured for no Block List');
|
logger.info('Configured for no Block List');
|
||||||
db.Tor.refreshTable().then((updatedTorList) => {
|
db.Tor.refreshTable().then(updatedTorList => {
|
||||||
logger.info('Tor list updated, length:', updatedTorList.length);
|
logger.info('Tor list updated, length:', updatedTorList.length);
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
return Promise.all([
|
return Promise.all([
|
||||||
db.Blocked.refreshTable(finalBlockListEndpoint),
|
db.Blocked.refreshTable(finalBlockListEndpoint),
|
||||||
db.Tor.refreshTable()])
|
db.Tor.refreshTable(),
|
||||||
.then(([updatedBlockedList, updatedTorList]) => {
|
]).then(([updatedBlockedList, updatedTorList]) => {
|
||||||
logger.info('Blocked list updated, length:', updatedBlockedList.length);
|
logger.info('Blocked list updated, length:', updatedBlockedList.length);
|
||||||
logger.info('Tor list updated, length:', updatedTorList.length);
|
logger.info('Tor list updated, length:', updatedTorList.length);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
this.start = () => {
|
this.start = () => {
|
||||||
|
@ -210,10 +223,7 @@ function Server () {
|
||||||
return this.startServerListening();
|
return this.startServerListening();
|
||||||
})
|
})
|
||||||
.then(() => {
|
.then(() => {
|
||||||
return Promise.all([
|
return Promise.all([this.performChecks(), this.performUpdates()]);
|
||||||
this.performChecks(),
|
|
||||||
this.performUpdates(),
|
|
||||||
]);
|
|
||||||
})
|
})
|
||||||
.then(() => {
|
.then(() => {
|
||||||
return setupBlockList();
|
return setupBlockList();
|
||||||
|
|
|
@ -8,7 +8,7 @@ const handleLbrynetResponse = require('./utils/handleLbrynetResponse.js');
|
||||||
const { publishing } = require('@config/siteConfig');
|
const { publishing } = require('@config/siteConfig');
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
publishClaim (publishParams) {
|
publishClaim(publishParams) {
|
||||||
logger.debug(`lbryApi >> Publishing claim to "${publishParams.name}"`);
|
logger.debug(`lbryApi >> Publishing claim to "${publishParams.name}"`);
|
||||||
const gaStartTime = Date.now();
|
const gaStartTime = Date.now();
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
|
@ -18,7 +18,13 @@ module.exports = {
|
||||||
params: publishParams,
|
params: publishParams,
|
||||||
})
|
})
|
||||||
.then(response => {
|
.then(response => {
|
||||||
sendGATimingEvent('lbrynet', 'publish', chooseGaLbrynetPublishLabel(publishParams), gaStartTime, Date.now());
|
sendGATimingEvent(
|
||||||
|
'lbrynet',
|
||||||
|
'publish',
|
||||||
|
chooseGaLbrynetPublishLabel(publishParams),
|
||||||
|
gaStartTime,
|
||||||
|
Date.now()
|
||||||
|
);
|
||||||
handleLbrynetResponse(response, resolve, reject);
|
handleLbrynetResponse(response, resolve, reject);
|
||||||
})
|
})
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
|
@ -26,7 +32,7 @@ module.exports = {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
getClaim (uri) {
|
getClaim(uri) {
|
||||||
logger.debug(`lbryApi >> Getting Claim for "${uri}"`);
|
logger.debug(`lbryApi >> Getting Claim for "${uri}"`);
|
||||||
const gaStartTime = Date.now();
|
const gaStartTime = Date.now();
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
|
@ -47,7 +53,7 @@ module.exports = {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
async abandonClaim ({claimId}) {
|
async abandonClaim({ claimId }) {
|
||||||
logger.debug(`lbryApi >> Abandon claim "${claimId}"`);
|
logger.debug(`lbryApi >> Abandon claim "${claimId}"`);
|
||||||
const gaStartTime = Date.now();
|
const gaStartTime = Date.now();
|
||||||
try {
|
try {
|
||||||
|
@ -62,7 +68,7 @@ module.exports = {
|
||||||
return error;
|
return error;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
getClaimList (claimName) {
|
getClaimList(claimName) {
|
||||||
logger.debug(`lbryApi >> Getting claim_list for "${claimName}"`);
|
logger.debug(`lbryApi >> Getting claim_list for "${claimName}"`);
|
||||||
const gaStartTime = Date.now();
|
const gaStartTime = Date.now();
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
|
@ -80,7 +86,7 @@ module.exports = {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
resolveUri (uri) {
|
resolveUri(uri) {
|
||||||
logger.debug(`lbryApi >> Resolving URI for "${uri}"`);
|
logger.debug(`lbryApi >> Resolving URI for "${uri}"`);
|
||||||
const gaStartTime = Date.now();
|
const gaStartTime = Date.now();
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
|
@ -97,9 +103,11 @@ module.exports = {
|
||||||
db.Claim.findOne({ where: { claimId: uri.split('#')[1] } })
|
db.Claim.findOne({ where: { claimId: uri.split('#')[1] } })
|
||||||
.then(() => reject('This claim has not yet been confirmed on the LBRY blockchain'))
|
.then(() => reject('This claim has not yet been confirmed on the LBRY blockchain'))
|
||||||
.catch(() => reject(`Claim ${uri} does not exist`));
|
.catch(() => reject(`Claim ${uri} does not exist`));
|
||||||
} else if (data.result[uri].error) { // check for errors
|
} else if (data.result[uri].error) {
|
||||||
|
// check for errors
|
||||||
reject(data.result[uri].error);
|
reject(data.result[uri].error);
|
||||||
} else { // if no errors, resolve
|
} else {
|
||||||
|
// if no errors, resolve
|
||||||
resolve(data.result[uri]);
|
resolve(data.result[uri]);
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
@ -108,7 +116,7 @@ module.exports = {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
getDownloadDirectory () {
|
getDownloadDirectory() {
|
||||||
logger.debug('lbryApi >> Retrieving the download directory path from lbry daemon...');
|
logger.debug('lbryApi >> Retrieving the download directory path from lbry daemon...');
|
||||||
const gaStartTime = Date.now();
|
const gaStartTime = Date.now();
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
|
@ -117,11 +125,19 @@ module.exports = {
|
||||||
method: 'settings_get',
|
method: 'settings_get',
|
||||||
})
|
})
|
||||||
.then(({ data }) => {
|
.then(({ data }) => {
|
||||||
sendGATimingEvent('lbrynet', 'getDownloadDirectory', 'SETTINGS_GET', gaStartTime, Date.now());
|
sendGATimingEvent(
|
||||||
|
'lbrynet',
|
||||||
|
'getDownloadDirectory',
|
||||||
|
'SETTINGS_GET',
|
||||||
|
gaStartTime,
|
||||||
|
Date.now()
|
||||||
|
);
|
||||||
if (data.result) {
|
if (data.result) {
|
||||||
resolve(data.result.download_directory);
|
resolve(data.result.download_directory);
|
||||||
} else {
|
} else {
|
||||||
return new Error('Successfully connected to lbry daemon, but unable to retrieve the download directory.');
|
return new Error(
|
||||||
|
'Successfully connected to lbry daemon, but unable to retrieve the download directory.'
|
||||||
|
);
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
|
@ -130,7 +146,7 @@ module.exports = {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
createChannel (name) {
|
createChannel(name) {
|
||||||
logger.debug(`lbryApi >> Creating channel for ${name}...`);
|
logger.debug(`lbryApi >> Creating channel for ${name}...`);
|
||||||
const gaStartTime = Date.now();
|
const gaStartTime = Date.now();
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
|
@ -139,7 +155,7 @@ module.exports = {
|
||||||
method: 'channel_new',
|
method: 'channel_new',
|
||||||
params: {
|
params: {
|
||||||
channel_name: name,
|
channel_name: name,
|
||||||
amount : publishing.channelClaimBidAmount,
|
amount: publishing.channelClaimBidAmount,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
.then(response => {
|
.then(response => {
|
||||||
|
@ -151,15 +167,21 @@ module.exports = {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
getWalletBalance () {
|
getAccountBalance() {
|
||||||
const gaStartTime = Date.now();
|
const gaStartTime = Date.now();
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
axios
|
axios
|
||||||
.post(lbrynetUri, {
|
.post(lbrynetUri, {
|
||||||
method: 'wallet_balance',
|
method: 'account_balance',
|
||||||
})
|
})
|
||||||
.then(response => {
|
.then(response => {
|
||||||
sendGATimingEvent('lbrynet', 'getWalletBalance', 'SETTINGS_GET', gaStartTime, Date.now());
|
sendGATimingEvent(
|
||||||
|
'lbrynet',
|
||||||
|
'getAccountBalance',
|
||||||
|
'SETTINGS_GET',
|
||||||
|
gaStartTime,
|
||||||
|
Date.now()
|
||||||
|
);
|
||||||
handleLbrynetResponse(response, resolve, reject);
|
handleLbrynetResponse(response, resolve, reject);
|
||||||
})
|
})
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
|
|
Loading…
Reference in a new issue