From d027bba2c7937b613a58043b94bcb171c469d6b4 Mon Sep 17 00:00:00 2001 From: Manuel Araoz Date: Mon, 2 Mar 2015 17:43:41 -0300 Subject: [PATCH] refactor app into BitcoreHTTP --- api/app.js | 79 ++++++++++++++++++++++++++++++--- api/bin/www | 99 +++--------------------------------------- api/config/default.yml | 7 +++ api/routes/v1.js | 2 +- 4 files changed, 86 insertions(+), 101 deletions(-) create mode 100644 api/config/default.yml diff --git a/api/app.js b/api/app.js index 592cfce4..f56812ca 100644 --- a/api/app.js +++ b/api/app.js @@ -1,14 +1,35 @@ 'use strict'; +var http = require('http'); var cors = require('cors'); var express = require('express'); var compress = require('compression'); var bodyParser = require('body-parser'); var morgan = require('morgan'); +var bitcore = require('bitcore'); +var $ = bitcore.util.preconditions; +var BitcoreNode = require('../lib/node'); + var routes = require('./routes'); -function init(backend) { + +function BitcoreHTTP(node, opts) { + $.checkArgument(node); + opts = opts || {}; + this.node = node; + this.port = opts.port || 8000; + this.setupExpress(); +} + +BitcoreHTTP.create = function(opts) { + opts = opts || {}; + var node = BitcoreNode.create(opts.BitcoreNode); + return new BitcoreHTTP(node, opts); +}; + + +BitcoreHTTP.prototype.setupExpress = function() { var app = express(); // parse POST data @@ -21,7 +42,7 @@ function init(backend) { app.use(morgan('dev')); // install routes - app.use('/', routes(backend)); + app.use('/', routes(this.node)); // catch 404 and forward to error handler app.use(function(req, res, next) { @@ -31,7 +52,7 @@ function init(backend) { }); // production error handler - app.use(function(err, req, res, next) { + app.use(function(err, req, res) { res.status(err.status || 500); res.send({ message: err.message, @@ -39,7 +60,53 @@ function init(backend) { }); }); - return app; -} + app.set('port', this.port); -module.exports = init; + var server = http.createServer(app); + server.on('error', this.onError.bind(this)); + server.on('listening', this.onListening.bind(this)); + + this.app = app; + this.server = server; +}; + +/** + * Event listener for HTTP server "error" event. + */ +BitcoreHTTP.prototype.onError = function(error) { + if (error.syscall !== 'listen') { + throw error; + } + + var bind = typeof port === 'string' ? 'Pipe ' + this.port : 'Port ' + this.port; + + // handle specific listen errors with friendly messages + switch (error.code) { + case 'EACCES': + console.error(bind + ' requires elevated privileges'); + process.exit(1); + break; + case 'EADDRINUSE': + console.error(bind + ' is already in use'); + process.exit(1); + break; + default: + throw error; + } +}; + +/** + * Event listener for HTTP server "listening" event. + */ +BitcoreHTTP.prototype.onListening = function() { + var addr = this.server.address(); + var bind = typeof addr === 'string' ? 'pipe ' + addr : 'port ' + addr.port; + console.log('Listening on ' + bind); +}; + + +BitcoreHTTP.prototype.start = function() { + this.server.listen(this.port); +}; + +module.exports = BitcoreHTTP; diff --git a/api/bin/www b/api/bin/www index 55d52e15..5650961f 100755 --- a/api/bin/www +++ b/api/bin/www @@ -1,99 +1,10 @@ #!/usr/bin/env node -/** - * Module dependencies. - */ +'use strict'; -var API = require('../app'); -var http = require('http'); +var config = require('config'); +var BitcoreHTTP = require('../app'); -/** - * Init api with backing services - */ +var app = BitcoreHTTP.create(config.get('BitcoreHTTP')); +app.start(); -var Node = { - status: 'live', - nodes: 123 -} -var app = new API(Node); - -/** - * Get port from environment and store in Express. - */ - -var port = normalizePort(process.env.PORT || '8000'); -app.set('port', port); - -/** - * Create HTTP server. - */ - -var server = http.createServer(app); - -/** - * Listen on provided port, on all network interfaces. - */ - -server.listen(port); -server.on('error', onError); -server.on('listening', onListening); - -/** - * Normalize a port into a number, string, or false. - */ - -function normalizePort(val) { - var port = parseInt(val, 10); - - if (isNaN(port)) { - // named pipe - return val; - } - - if (port >= 0) { - // port number - return port; - } - - return false; -} - -/** - * Event listener for HTTP server "error" event. - */ - -function onError(error) { - if (error.syscall !== 'listen') { - throw error; - } - - var bind = typeof port === 'string' - ? 'Pipe ' + port - : 'Port ' + port - - // handle specific listen errors with friendly messages - switch (error.code) { - case 'EACCES': - console.error(bind + ' requires elevated privileges'); - process.exit(1); - break; - case 'EADDRINUSE': - console.error(bind + ' is already in use'); - process.exit(1); - break; - default: - throw error; - } -} - -/** - * Event listener for HTTP server "listening" event. - */ - -function onListening() { - var addr = server.address(); - var bind = typeof addr === 'string' - ? 'pipe ' + addr - : 'port ' + addr.port; - console.log('Listening on ' + bind); -} diff --git a/api/config/default.yml b/api/config/default.yml new file mode 100644 index 00000000..98713c7f --- /dev/null +++ b/api/config/default.yml @@ -0,0 +1,7 @@ +BitcoreHTTP: + port: 8080 + BitcoreNode: + NetworkMonitor: + network: livenet + host: localhost + port: 8333 diff --git a/api/routes/v1.js b/api/routes/v1.js index d8904b1f..252a8ecc 100644 --- a/api/routes/v1.js +++ b/api/routes/v1.js @@ -5,7 +5,7 @@ var express = require('express'); function initRouter(backend) { var router = express.Router(); - function mockResponse(req, res, next) { + function mockResponse(req, res) { res.send({'message': 'This is a mocked response'}); }