curren api finished. parser json var names normalized. Switched to express 4 router
This commit is contained in:
parent
b74e98c381
commit
7948a63875
@ -1,4 +1,5 @@
|
|||||||
const config = {
|
const config = {
|
||||||
|
full_node: true,
|
||||||
logging: 'debug',
|
logging: 'debug',
|
||||||
bcoin: {
|
bcoin: {
|
||||||
network: 'main',
|
network: 'main',
|
||||||
@ -7,6 +8,7 @@ const config = {
|
|||||||
workers: true,
|
workers: true,
|
||||||
logLevel: 'info',
|
logLevel: 'info',
|
||||||
'max-inbound': 100,
|
'max-inbound': 100,
|
||||||
|
'max-outbound': 100,
|
||||||
},
|
},
|
||||||
mongodb: {
|
mongodb: {
|
||||||
uri: 'mongodb://localhost/bitcore',
|
uri: 'mongodb://localhost/bitcore',
|
||||||
@ -16,6 +18,7 @@ const config = {
|
|||||||
},
|
},
|
||||||
api: {
|
api: {
|
||||||
port: 3000,
|
port: 3000,
|
||||||
|
json_spaces: 2,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
const config = {
|
const config = {
|
||||||
full_node: true,
|
full_node: false,
|
||||||
logging: 'debug',
|
logging: 'debug',
|
||||||
bcoin: {
|
bcoin: {
|
||||||
network: 'main',
|
network: 'main',
|
||||||
@ -8,6 +8,7 @@ const config = {
|
|||||||
workers: true,
|
workers: true,
|
||||||
logLevel: 'info',
|
logLevel: 'info',
|
||||||
'max-inbound': 100,
|
'max-inbound': 100,
|
||||||
|
'max-outbound': 100,
|
||||||
},
|
},
|
||||||
mongodb: {
|
mongodb: {
|
||||||
uri: 'mongodb://localhost/bitcore',
|
uri: 'mongodb://localhost/bitcore',
|
||||||
@ -18,6 +19,8 @@ const config = {
|
|||||||
api: {
|
api: {
|
||||||
port: 3000,
|
port: 3000,
|
||||||
json_spaces: 2,
|
json_spaces: 2,
|
||||||
|
currency_refresh: 60,
|
||||||
|
ticker_url: 'https://www.bitstamp.net/api/ticker/',
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -1,41 +1,41 @@
|
|||||||
module.exports = function addressAPI(app) {
|
module.exports = function addressrouter(router) {
|
||||||
app.get('/addr/:addr', (req, res) => {
|
router.get('/addr/:addr', (req, res) => {
|
||||||
res.send(req.params.addr);
|
res.send(req.params.addr);
|
||||||
});
|
});
|
||||||
|
|
||||||
app.get('/addr/:addr/utxo', (req, res) => {
|
router.get('/addr/:addr/utxo', (req, res) => {
|
||||||
res.send(req.params.addr);
|
res.send(req.params.addr);
|
||||||
});
|
});
|
||||||
|
|
||||||
app.get('/addr/:addr/balance', (req, res) => {
|
router.get('/addr/:addr/balance', (req, res) => {
|
||||||
res.send(req.params.addr);
|
res.send(req.params.addr);
|
||||||
});
|
});
|
||||||
|
|
||||||
app.get('/addr/:addr/totalReceived', (req, res) => {
|
router.get('/addr/:addr/totalReceived', (req, res) => {
|
||||||
res.send(req.params.addr);
|
res.send(req.params.addr);
|
||||||
});
|
});
|
||||||
|
|
||||||
app.get('/addr/:addr/totalSent', (req, res) => {
|
router.get('/addr/:addr/totalSent', (req, res) => {
|
||||||
res.send(req.params.addr);
|
res.send(req.params.addr);
|
||||||
});
|
});
|
||||||
|
|
||||||
app.get('/addr/:addr/unconfirmedBalance', (req, res) => {
|
router.get('/addr/:addr/unconfirmedBalance', (req, res) => {
|
||||||
res.send(req.params.addr);
|
res.send(req.params.addr);
|
||||||
});
|
});
|
||||||
|
|
||||||
app.get('/addrs/:addrs/utxo', (req, res) => {
|
router.get('/addrs/:addrs/utxo', (req, res) => {
|
||||||
res.send(req.params.addrs);
|
res.send(req.params.addrs);
|
||||||
});
|
});
|
||||||
|
|
||||||
app.post('/addrs/utxo', (req, res) => {
|
router.post('/addrs/utxo', (req, res) => {
|
||||||
res.send('post stub');
|
res.send('post stub');
|
||||||
});
|
});
|
||||||
|
|
||||||
app.get('/addrs/:addrs/txs', (req, res) => {
|
router.get('/addrs/:addrs/txs', (req, res) => {
|
||||||
res.send(req.params.addrs);
|
res.send(req.params.addrs);
|
||||||
});
|
});
|
||||||
|
|
||||||
app.post('/addrs/txs', (req, res) => {
|
router.post('/addrs/txs', (req, res) => {
|
||||||
res.send('post stub');
|
res.send('post stub');
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|||||||
@ -16,8 +16,8 @@ function getBlock(params, options, cb) {
|
|||||||
.limit(MAX_BLOCKS);
|
.limit(MAX_BLOCKS);
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = function BlockAPI(app) {
|
module.exports = function BlockAPI(router) {
|
||||||
app.get('/block/:blockHash', (req, res) => {
|
router.get('/block/:blockHash', (req, res) => {
|
||||||
getBlock(
|
getBlock(
|
||||||
{ hash: req.params.blockHash },
|
{ hash: req.params.blockHash },
|
||||||
{ rawBlock: 0 },
|
{ rawBlock: 0 },
|
||||||
@ -30,7 +30,7 @@ module.exports = function BlockAPI(app) {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
app.get('/blocks', (req, res) => {
|
router.get('/blocks', (req, res) => {
|
||||||
getBlock(
|
getBlock(
|
||||||
{},
|
{},
|
||||||
{ height: 1,
|
{ height: 1,
|
||||||
@ -59,7 +59,7 @@ module.exports = function BlockAPI(app) {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
app.get('/rawblock/:blockHash', (req, res) => {
|
router.get('/rawblock/:blockHash', (req, res) => {
|
||||||
getBlock(
|
getBlock(
|
||||||
{ hash: req.params.blockHash },
|
{ hash: req.params.blockHash },
|
||||||
{ rawBlock: 1 },
|
{ rawBlock: 1 },
|
||||||
@ -68,11 +68,11 @@ module.exports = function BlockAPI(app) {
|
|||||||
res.status(501).send();
|
res.status(501).send();
|
||||||
logger.log('err', err);
|
logger.log('err', err);
|
||||||
}
|
}
|
||||||
res.json(block);
|
res.json(block[0]);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
app.get('/block-index/:height', (req, res) => {
|
router.get('/block-index/:height', (req, res) => {
|
||||||
getBlock(
|
getBlock(
|
||||||
{ height: req.params.height },
|
{ height: req.params.height },
|
||||||
{},
|
{},
|
||||||
@ -81,7 +81,7 @@ module.exports = function BlockAPI(app) {
|
|||||||
res.status(501).send();
|
res.status(501).send();
|
||||||
logger.log('err', err);
|
logger.log('err', err);
|
||||||
}
|
}
|
||||||
res.json(block);
|
res.json(block[0]);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|||||||
@ -1,5 +1,41 @@
|
|||||||
|
const config = require('../../config');
|
||||||
|
const logger = require('../logger');
|
||||||
|
const request = require('request');
|
||||||
|
|
||||||
|
const refreshInterval = config.api.currency_refresh >= 1 ?
|
||||||
|
config.api.currency_refresh * 1000 :
|
||||||
|
60 * 1000;
|
||||||
|
let lastRate = 0;
|
||||||
|
|
||||||
|
getRate();
|
||||||
|
|
||||||
|
setInterval(() => {
|
||||||
|
getRate();
|
||||||
|
}, refreshInterval);
|
||||||
|
|
||||||
|
function getRate() {
|
||||||
|
request(config.api.ticker_url, (err, res, body) => {
|
||||||
|
if (err) {
|
||||||
|
logger.log('error',
|
||||||
|
`${err}`);
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
const ticker = JSON.parse(body);
|
||||||
|
lastRate = ticker.last;
|
||||||
|
logger.log('debug',
|
||||||
|
`getRate: ${lastRate}`);
|
||||||
|
} catch (err) {
|
||||||
|
logger.log('error',
|
||||||
|
`getRate: ${err}`);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = function currencyAPI(app) {
|
module.exports = function currencyAPI(app) {
|
||||||
app.get('/currency', (req, res) => {
|
app.get('/currency', (req, res) => {
|
||||||
res.send('currency');
|
res.json({
|
||||||
|
bitstamp: lastRate,
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|||||||
@ -2,15 +2,19 @@ const express = require('express');
|
|||||||
const config = require('../../config');
|
const config = require('../../config');
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
|
const api = express.Router();
|
||||||
|
|
||||||
app.set('json spaces', config.api.json_spaces);
|
app.set('json spaces', config.api.json_spaces);
|
||||||
|
|
||||||
// Pass express to register the routes
|
// Pass express to register the routes
|
||||||
const AddressAPI = require('./address')(app);
|
const AddressAPI = require('./address')(api);
|
||||||
const BlockAPI = require('./block')(app);
|
const BlockAPI = require('./block')(api);
|
||||||
const StatusAPI = require('./status')(app);
|
const CurrencyAPI = require('./currency')(api);
|
||||||
const TransactionAPI = require('./transaction')(app);
|
const StatusAPI = require('./status')(api);
|
||||||
const MessageAPI = require('./message')(app);
|
const TransactionAPI = require('./transaction')(api);
|
||||||
|
const MessageAPI = require('./message')(api);
|
||||||
|
|
||||||
|
app.use('/api', api);
|
||||||
|
|
||||||
// 404
|
// 404
|
||||||
app.use((req, res) => {
|
app.use((req, res) => {
|
||||||
|
|||||||
@ -1,13 +1,13 @@
|
|||||||
module.exports = function messageAPI(app) {
|
module.exports = function messageAPI(router) {
|
||||||
app.get('/messages/verify', (req, res) => {
|
router.get('/messages/verify', (req, res) => {
|
||||||
res.send('messages verify');
|
res.send('messages verify');
|
||||||
});
|
});
|
||||||
|
|
||||||
app.post('/messages/verify', (req, res) => {
|
router.post('/messages/verify', (req, res) => {
|
||||||
res.send('post messages verify');
|
res.send('post messages verify');
|
||||||
});
|
});
|
||||||
|
|
||||||
app.get('/utils/estimatefee', (req, res) => {
|
router.get('/utils/estimatefee', (req, res) => {
|
||||||
res.send('estimate fees');
|
res.send('estimate fees');
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|||||||
@ -1,17 +1,17 @@
|
|||||||
module.exports = function statusAPI(app) {
|
module.exports = function statusAPI(router) {
|
||||||
app.get('/status', (req, res) => {
|
router.get('/status', (req, res) => {
|
||||||
res.send('status');
|
res.send('status');
|
||||||
});
|
});
|
||||||
|
|
||||||
app.get('/sync', (req, res) => {
|
router.get('/sync', (req, res) => {
|
||||||
res.send('sync');
|
res.send('sync');
|
||||||
});
|
});
|
||||||
|
|
||||||
app.get('/peer', (req, res) => {
|
router.get('/peer', (req, res) => {
|
||||||
res.send('peer');
|
res.send('peer');
|
||||||
});
|
});
|
||||||
|
|
||||||
app.get('/version', (req, res) => {
|
router.get('/version', (req, res) => {
|
||||||
res.send('version');
|
res.send('version');
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|||||||
@ -15,8 +15,8 @@ function getTransaction(params, options, cb) {
|
|||||||
.limit(MAX_TXS);
|
.limit(MAX_TXS);
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = function transactionAPI(app) {
|
module.exports = function transactionAPI(router) {
|
||||||
app.get('/tx/:txid', (req, res) => {
|
router.get('/tx/:txid', (req, res) => {
|
||||||
Transaction.find({ txid: req.params.txid }, (err, tx) => {
|
Transaction.find({ txid: req.params.txid }, (err, tx) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
res.status(501).send();
|
res.status(501).send();
|
||||||
@ -25,7 +25,7 @@ module.exports = function transactionAPI(app) {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
app.get('/txs', (req, res) => {
|
router.get('/txs', (req, res) => {
|
||||||
getTransaction(
|
getTransaction(
|
||||||
{},
|
{},
|
||||||
{},
|
{},
|
||||||
@ -38,11 +38,11 @@ module.exports = function transactionAPI(app) {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
app.get('/rawtx/:txid', (req, res) => {
|
router.get('/rawtx/:txid', (req, res) => {
|
||||||
res.send(req.params.txid);
|
res.send(req.params.txid);
|
||||||
});
|
});
|
||||||
|
|
||||||
app.post('/tx/send', (req, res) => {
|
router.post('/tx/send', (req, res) => {
|
||||||
res.send('tx send stub');
|
res.send('tx send stub');
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|||||||
@ -19,6 +19,11 @@ function start() {
|
|||||||
BlockParser.parse(entry, block);
|
BlockParser.parse(entry, block);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
node.on('error', (err) => {
|
||||||
|
logger.log('error',
|
||||||
|
`${err}`);
|
||||||
|
});
|
||||||
|
|
||||||
// node.mempool.on('tx' ...)
|
// node.mempool.on('tx' ...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -6,19 +6,19 @@ const logger = require('../logger');
|
|||||||
|
|
||||||
function parse(entry, block) {
|
function parse(entry, block) {
|
||||||
const rawBlock = block.toRaw().toString('hex');
|
const rawBlock = block.toRaw().toString('hex');
|
||||||
const json = block.toJSON();
|
const blockJSON = block.toJSON();
|
||||||
const reward = util.calcBlockReward(entry.height);
|
const reward = util.calcBlockReward(entry.height);
|
||||||
|
|
||||||
const newBlock = new BlockModel({
|
const newBlock = new BlockModel({
|
||||||
hash: json.hash,
|
hash: blockJSON.hash,
|
||||||
height: entry.height,
|
height: entry.height,
|
||||||
version: json.version,
|
version: blockJSON.version,
|
||||||
size: block.size,
|
size: block.size,
|
||||||
prevBlock: json.prevBlock,
|
prevBlock: blockJSON.prevBlock,
|
||||||
merkleRoot: json.merkleRoot,
|
merkleRoot: blockJSON.merkleRoot,
|
||||||
ts: json.ts,
|
ts: blockJSON.ts,
|
||||||
bits: json.bits,
|
bits: blockJSON.bits,
|
||||||
nonce: json.nonce,
|
nonce: blockJSON.nonce,
|
||||||
txs: block.txs.map(tx => util.revHex(tx.hash().toString('hex'))),
|
txs: block.txs.map(tx => util.revHex(tx.hash().toString('hex'))),
|
||||||
chainwork: entry.chainwork,
|
chainwork: entry.chainwork,
|
||||||
reward: reward,
|
reward: reward,
|
||||||
|
|||||||
@ -7,9 +7,7 @@ const logger = require('../logger');
|
|||||||
|
|
||||||
function parse(entry, txs) {
|
function parse(entry, txs) {
|
||||||
txs.forEach((tx) => {
|
txs.forEach((tx) => {
|
||||||
const txHash = util.revHex(tx.hash().toString('hex'));
|
const txJSON = tx.toJSON();
|
||||||
const blockHash = util.revHex(entry.hash);
|
|
||||||
const txJSON = tx.totxJSON();
|
|
||||||
|
|
||||||
const t = new TxModel({
|
const t = new TxModel({
|
||||||
hash: txJSON.hash,
|
hash: txJSON.hash,
|
||||||
|
|||||||
@ -16,6 +16,7 @@
|
|||||||
"bcoin": "^1.0.0-beta.14",
|
"bcoin": "^1.0.0-beta.14",
|
||||||
"express": "^4.15.3",
|
"express": "^4.15.3",
|
||||||
"mongoose": "^4.11.5",
|
"mongoose": "^4.11.5",
|
||||||
|
"request": "^2.81.0",
|
||||||
"winston": "^2.3.1"
|
"winston": "^2.3.1"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user