diff --git a/server/config/index.js b/server/config/index.js index 714f954..2dd1526 100644 --- a/server/config/index.js +++ b/server/config/index.js @@ -26,6 +26,9 @@ const config = { json_spaces: 2, currency_refresh: 60, ticker_url: 'https://www.bitstamp.net/api/ticker/', + ticker_prop: 'bitstamp', + max_blocks: 72, + max_txs: 10, }, }; diff --git a/server/lib/api/block.js b/server/lib/api/block.js index fb64178..9c3af3c 100644 --- a/server/lib/api/block.js +++ b/server/lib/api/block.js @@ -3,6 +3,7 @@ const db = require('../db'); module.exports = function BlockAPI(router) { router.get('/block/:blockHash', (req, res) => { + // Pass Mongo params, fields and limit to db api. db.blocks.getBlock( { hash: req.params.blockHash }, { rawBlock: 0 }, @@ -12,7 +13,7 @@ module.exports = function BlockAPI(router) { logger.log('err', err); return res.status(404).send(); } - + // Format the request for insight ui return res.json({ hash: block.hash, size: block.size, @@ -37,7 +38,7 @@ module.exports = function BlockAPI(router) { router.get('/blocks', (req, res) => { const limit = parseInt(req.query.limit) || 100; - + // Pass Mongo params, fields and limit to db api. db.blocks.getBlocks( {}, { height: 1, @@ -54,7 +55,7 @@ module.exports = function BlockAPI(router) { `/blocks: ${err}`); return res.status(404).send(); } - + // Format the request for insight ui return res.json({ blocks: blocks.map(block => ({ hash: block.hash, @@ -72,7 +73,7 @@ module.exports = function BlockAPI(router) { router.get('/rawblock/:blockHash', (req, res) => { const blockHash = req.params.blockHash || ''; - + // Pass Mongo params, fields and limit to db api. db.blocks.getBlock( { hash: blockHash }, { rawBlock: 1 }, @@ -89,7 +90,7 @@ module.exports = function BlockAPI(router) { router.get('/block-index/:height', (req, res) => { const blockHeight = parseInt(req.params.height) || 1; - + // Pass Mongo params, fields and limit to db api. db.blocks.getBlock( { height: blockHeight }, { hash: 1 }, diff --git a/server/lib/api/currency.js b/server/lib/api/currency.js index 1c6279d..0e55496 100644 --- a/server/lib/api/currency.js +++ b/server/lib/api/currency.js @@ -2,6 +2,9 @@ const config = require('../../config'); const logger = require('../logger'); const request = require('request'); +// Retrieve the configured endpoint's ticker rate at a +// set interval + const refreshInterval = config.api.currency_refresh >= 1 ? config.api.currency_refresh * 1000 : 60 * 1000; @@ -32,11 +35,13 @@ function getRate() { } module.exports = function currencyAPI(app) { + // Return the ticker price app.get('/currency', (req, res) => { + const data = {} + data[config.api.ticker_prop] = lastRate; + res.json({ - data: { - bitstamp: lastRate, - }, + data, }); }); }; diff --git a/server/lib/api/transaction.js b/server/lib/api/transaction.js index fef4efc..80bcac3 100644 --- a/server/lib/api/transaction.js +++ b/server/lib/api/transaction.js @@ -4,7 +4,7 @@ const config = require('../../config'); const db = require('../db'); const API_URL = `http://${config.bcoin_http}:${config.bcoin['http-port']}`; -const MAX_TXS = 50; +const MAX_TXS = config.api.max_txs; module.exports = function transactionAPI(router) { router.get('/tx/:txid', (req, res) => { diff --git a/server/lib/db/blocks.js b/server/lib/db/blocks.js index a417194..e3da8fe 100644 --- a/server/lib/db/blocks.js +++ b/server/lib/db/blocks.js @@ -2,14 +2,14 @@ const Block = require('../../models/block.js'); const logger = require('../logger'); const config = require('../../config'); -// move to config -const MAX_BLOCKS = 72; // ~ 12 hours +const MAX_BLOCKS = config.api.max_blocks; // ~ 12 hours function getBlocks(params, options, limit, cb) { + // Do not return mongo ids const defaultOptions = { _id: 0 }; - + // Copy over mongo options Object.assign(defaultOptions, options); - + // Simple sanitizing if (!Number.isInteger(limit)) { limit = 1; } @@ -21,7 +21,7 @@ function getBlocks(params, options, limit, cb) { if (limit < 1) { limit = 1; } - + // Query mongo Block.find( params, defaultOptions, @@ -39,7 +39,7 @@ function getBlocks(params, options, limit, cb) { .sort({ height: -1 }) .limit(limit); } - +// Retrieve a single block. For convenience mostly function getBlock(params, options, limit, cb) { getBlocks(params, options, limit, (err, blocks) => { if (err) { diff --git a/server/lib/db/transactions.js b/server/lib/db/transactions.js index 2d160ab..455727d 100644 --- a/server/lib/db/transactions.js +++ b/server/lib/db/transactions.js @@ -2,14 +2,14 @@ const Transactions = require('../../models/transaction.js'); const logger = require('../logger'); const config = require('../../config'); -// move to config -const MAX_TXS = 50; +const MAX_TXS = config.api.max_txs; function getTransactions(params, options, limit, cb) { + // Do not return mongo ids const defaultOptions = { _id: 0 }; - + // Copy over mongo options Object.assign(defaultOptions, options); - + // Simple sanitizing if (!Number.isInteger(limit)) { limit = 1; } @@ -21,7 +21,7 @@ function getTransactions(params, options, limit, cb) { if (limit < 1) { limit = 1; } - + // Query mongo Transactions.find( params, defaultOptions, @@ -44,7 +44,7 @@ function getTransaction(params, options, limit, cb) { getTransactions(params, options, limit, (err, tx) => { if (err) { logger.log('error', - `getBlock: ${err.err}`); + `getTransaction: ${err.err}`); return cb(err); } if (!tx.length > 0) { diff --git a/server/lib/logger/index.js b/server/lib/logger/index.js index 7db8f9f..2674161 100644 --- a/server/lib/logger/index.js +++ b/server/lib/logger/index.js @@ -1,7 +1,7 @@ const winston = require('winston'); const config = require('../../config'); -const logfile = new Date().toISOString(); +const logfile = new Date().toISOString().split('T')[0]; const logger = new (winston.Logger)({ transports: [ diff --git a/server/lib/node/index.js b/server/lib/node/index.js index 533262d..2c9cc88 100644 --- a/server/lib/node/index.js +++ b/server/lib/node/index.js @@ -6,6 +6,8 @@ const addrParser = require('../parser').Address; const config = require('../../config'); const io = require('../api').io; +// Reverse how sockets are working + const node = new FullNode(config.bcoin); // Hacky move this to config diff --git a/server/public/src/js/services/socket.js b/server/public/src/js/services/socket.js index 1c1ff8f..b4c2987 100644 --- a/server/public/src/js/services/socket.js +++ b/server/public/src/js/services/socket.js @@ -55,7 +55,6 @@ ScopedSocket.prototype.emit = function(event, data, callback) { angular.module('insight.socket').factory('getSocket', function($rootScope) { - console.log('init my socket'); var socket = io.connect('http://localhost', { 'reconnect': true, 'reconnection delay': 500,