diff --git a/README.md b/README.md index c9868152..ca751e31 100644 --- a/README.md +++ b/README.md @@ -48,7 +48,6 @@ $ npm install -g bower -## API ## Prerequisites Get bitcore from github repository: @@ -62,21 +61,23 @@ $ npm install -g bower check utils/sync.js --help for options. +## API + +A REST API is provided at /api. The entry points are: + ### Blocks ``` - /block/[:hash] - /block/00000000a967199a2fad0877433c93df785a8d8ce062e5f9b451cd1397bdbf62 + /api/block/[:hash] + /api/block/00000000a967199a2fad0877433c93df785a8d8ce062e5f9b451cd1397bdbf62 ``` ### Transactions ``` - /tx/[:txid] - /tx/525de308971eabd941b139f46c7198b5af9479325c2395db7f2fb5ae8562556c + /api/tx/[:txid] + /api/tx/525de308971eabd941b139f46c7198b5af9479325c2395db7f2fb5ae8562556c ``` - - ## Troubleshooting If you did not get all library during grunt command, please use the follow command: diff --git a/app/controllers/blocks.js b/app/controllers/blocks.js index fdef6655..d9c9f89c 100644 --- a/app/controllers/blocks.js +++ b/app/controllers/blocks.js @@ -1,15 +1,13 @@ 'use strict'; - -var Block = require('../models/Block'); -//, _ = require('lodash'); - - - /** * Module dependencies. */ +var mongoose = require('mongoose'), + Block = mongoose.model('Block'); +//, _ = require('lodash'); + /** * Find block by hash ... @@ -25,9 +23,47 @@ exports.block = function(req, res, next, hash) { /** - * Show block + * Show block */ exports.show = function(req, res) { res.jsonp(req.block); }; +/** + * List of blocks at HomePage + */ +exports.last_blocks = function(req, res) { + Block.find().sort({time:-1}).limit(7).exec(function(err, blocks) { + if (err) { + res.render('error', { + status: 500 + }); + } else { + res.jsonp(blocks); + } + }); +}; + +/** + * List of blocks by date + */ +exports.list = function(req, res) { + var findParam = {}; + + if (req.query.blockDate) { + findParam = {}; + } + + Block + .find(findParam) + .limit(5) + .exec(function(err, blocks) { + if (err) { + res.render('error', { + status: 500 + }); + } else { + res.jsonp(blocks); + } + }); +}; diff --git a/app/models/Block.js b/app/models/Block.js index 3ce94ef0..ee68b94d 100644 --- a/app/models/Block.js +++ b/app/models/Block.js @@ -19,6 +19,7 @@ var BlockSchema = new Schema({ unique: true, }, size: Number, + height: Number, confirmations: Number, version: Number, merkleroot: String, @@ -93,5 +94,4 @@ BlockSchema.statics.fromHash = function(hash, cb) { }).exec(cb); }; - module.exports = mongoose.model('Block', BlockSchema); diff --git a/app/views/includes/foot.jade b/app/views/includes/foot.jade index f7cc3e62..edca962e 100755 --- a/app/views/includes/foot.jade +++ b/app/views/includes/foot.jade @@ -23,9 +23,12 @@ script(type='text/javascript', src='/js/directives.js') script(type='text/javascript', src='/js/filters.js') //Application Services +script(type='text/javascript', src='/js/services/blocks.js') script(type='text/javascript', src='/js/services/global.js') +script(type='text/javascript', src='/js/services/index.js') //Application Controllers script(type='text/javascript', src='/js/controllers/index.js') script(type='text/javascript', src='/js/controllers/header.js') +script(type='text/javascript', src='/js/controllers/blocks.js') script(type='text/javascript', src='/js/init.js') diff --git a/app/views/index.jade b/app/views/index.jade index 7f337180..a74f1733 100755 --- a/app/views/index.jade +++ b/app/views/index.jade @@ -2,24 +2,3 @@ extends layouts/default block content section.container(data-ng-view) - - section.container - p ˈmɪst(ə)ri/' - | noun - audio(src="https://ssl.gstatic.com/dictionary/static/sounds/de/0/mystery.mp3",preload="auto",data-dobid="aud",id="aud") - button(onclick="document.getElementById('aud').play()") Play - - ol - li - strong something that is difficult or impossible to understand or explain. - p "the mysteries of outer space" - | synonyms: puzzle, enigma, conundrum, riddle, secret, unsolved problem, problem, question, question mark, closed book; secrecy or obscurity. - p "much of her past is shrouded in mystery" - | synonyms: secrecy, darkness, obscurity, ambiguity, ambiguousness, uncertainty, impenetrability, vagueness, nebulousness; More - li - strong a person or thing whose identity or nature is puzzling or unknown. - p "‘He's a bit of a mystery,’ said Nina" - li - strong a novel, play, or film dealing with a puzzling crime, especially a murder. - p "the 1920s murder mystery, The Ghost Train" - | synonyms: thriller, detective story/novel, murder story; More diff --git a/config/routes.js b/config/routes.js index 507378f0..ab4c9b18 100644 --- a/config/routes.js +++ b/config/routes.js @@ -8,12 +8,14 @@ module.exports = function(app) { //Block routes var blocks = require('../app/controllers/blocks'); - app.get('/block/:blockHash', blocks.show); + app.get('/api/blocks', blocks.list); + app.get('/api/block/:blockHash', blocks.show); app.param('blockHash', blocks.block); + app.get('/last_blocks', blocks.last_blocks); var transactions = require('../app/controllers/transactions'); app.get('/tx/:txid', transactions.show); - + app.param('txid', transactions.transaction); - + }; diff --git a/public/js/app.js b/public/js/app.js index 77b64cba..2b6bc682 100755 --- a/public/js/app.js +++ b/public/js/app.js @@ -1,5 +1,7 @@ 'use strict'; -angular.module('mystery', ['ngCookies', 'ngResource', 'ngRoute', 'ui.bootstrap', 'ui.route', 'mystery.system']); +angular.module('mystery', ['ngCookies', 'ngResource', 'ngRoute', 'ui.bootstrap', 'ui.route', 'mystery.system', 'mystery.index', 'mystery.blocks']); -angular.module('mystery.system', []); \ No newline at end of file +angular.module('mystery.system', []); +angular.module('mystery.index', []); +angular.module('mystery.blocks', []); diff --git a/public/js/config.js b/public/js/config.js index fe680064..35fe3836 100755 --- a/public/js/config.js +++ b/public/js/config.js @@ -4,9 +4,18 @@ angular.module('mystery').config(['$routeProvider', function($routeProvider) { $routeProvider. + when('/block/:blockHash', { + templateUrl: 'views/block.html' + }). when('/', { templateUrl: 'views/index.html' }). + when('/blocks', { + templateUrl: 'views/blocks/list.html' + }). + when('/blocks-date/:blockDate', { + templateUrl: 'views/blocks/list_date.html' + }). otherwise({ redirectTo: '/' }); diff --git a/public/js/controllers/blocks.js b/public/js/controllers/blocks.js new file mode 100644 index 00000000..98e88010 --- /dev/null +++ b/public/js/controllers/blocks.js @@ -0,0 +1,30 @@ +'use strict'; + +angular.module('mystery.blocks').controller('BlocksController', ['$scope', '$routeParams', '$location', 'Global', 'Block', 'Blocks', function ($scope, $routeParams, $location, Global, Block, Blocks) { + $scope.global = Global; + + $scope.list_blocks = function() { + Blocks.query(function(blocks) { + $scope.blocks = blocks; + }); + }; + + $scope.list_blocks_date = function() { + Blocks.query({ + blockDate: $routeParams.blockDate + }, function(blocks) { + $scope.blocks = blocks; + }); + }; + + $scope.findOne = function() { + Block.get({ + blockHash: $routeParams.blockHash + }, function(block) { + $scope.block = block; + }); + }; + + // for avoid warning. please remove when you use Blocks + $scope.blocks = Blocks; +}]); diff --git a/public/js/controllers/header.js b/public/js/controllers/header.js index 039a5ac2..8dddc143 100755 --- a/public/js/controllers/header.js +++ b/public/js/controllers/header.js @@ -4,11 +4,8 @@ angular.module('mystery.system').controller('HeaderController', ['$scope', 'Glob $scope.global = Global; $scope.menu = [{ - 'title': 'Articles', - 'link': 'articles' - }, { - 'title': 'Create New Article', - 'link': 'articles/create' + 'title': 'Blocks', + 'link': 'blocks' }]; $scope.isCollapsed = false; diff --git a/public/js/controllers/index.js b/public/js/controllers/index.js index 4cb2b6fd..bec0f0ad 100755 --- a/public/js/controllers/index.js +++ b/public/js/controllers/index.js @@ -1,5 +1,10 @@ 'use strict'; -angular.module('mystery.system').controller('IndexController', ['$scope', 'Global', function ($scope, Global) { +angular.module('mystery.system').controller('IndexController', ['$scope', 'Global', 'Index', function ($scope, Global, Index) { $scope.global = Global; -}]); \ No newline at end of file + $scope.last_blocks = function() { + Index.query(function(blocks) { + $scope.blocks = blocks; + }); + }; +}]); diff --git a/public/js/services/blocks.js b/public/js/services/blocks.js new file mode 100644 index 00000000..7747ff32 --- /dev/null +++ b/public/js/services/blocks.js @@ -0,0 +1,11 @@ +'use strict'; + +angular.module('mystery.blocks').factory('Block', ['$resource', function($resource) { + return $resource('/api/block/:blockHash', { + blockHash: '@blockHash' + }); +}]); + +angular.module('mystery.blocks').factory('Blocks', ['$resource', function($resource) { + return $resource('/api/blocks'); +}]); diff --git a/public/js/services/index.js b/public/js/services/index.js new file mode 100644 index 00000000..f961642e --- /dev/null +++ b/public/js/services/index.js @@ -0,0 +1,5 @@ +'use strict'; + +angular.module('mystery.index').factory('Index', ['$resource', function($resource) { + return $resource('/last_blocks'); +}]); diff --git a/public/views/block.html b/public/views/block.html new file mode 100644 index 00000000..686a6ade --- /dev/null +++ b/public/views/block.html @@ -0,0 +1,23 @@ +
+ + + + + + + + + + + + + + + + + + +
HeightAgeTransactionsConfirmationsSize (kB)
{{block.height}}{{block.time | date:'short'}}{{block.tx.length }}{{block.confirmations}}{{block.size / 1024}}
+
\ No newline at end of file diff --git a/public/views/blocks/list.html b/public/views/blocks/list.html new file mode 100644 index 00000000..f258705a --- /dev/null +++ b/public/views/blocks/list.html @@ -0,0 +1,10 @@ +
+ + +
\ No newline at end of file diff --git a/public/views/blocks/list_date.html b/public/views/blocks/list_date.html new file mode 100644 index 00000000..349857cf --- /dev/null +++ b/public/views/blocks/list_date.html @@ -0,0 +1,10 @@ +
+ + +
diff --git a/public/views/header.html b/public/views/header.html index 70189e79..fb034edb 100755 --- a/public/views/header.html +++ b/public/views/header.html @@ -6,13 +6,13 @@ - Mystery + Mystery diff --git a/public/views/index.html b/public/views/index.html index b92fe022..953dc31b 100644 --- a/public/views/index.html +++ b/public/views/index.html @@ -1,5 +1,23 @@ -
+
+ + + + + + + + + + + + + + + + + +
HeightAgeTransactionsConfirmationsSize (kB)
{{block.height}}{{block.time | date:'short'}}{{block.tx.length }}{{block.confirmations}}{{block.size / 1024}}