From eb95ae7fd62340096a52af3eaef6792a28750aea Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Sun, 21 Aug 2016 11:02:50 -0400 Subject: [PATCH] refactor server --- lib/index.js | 63 ++++++++++++++++++++++----------------------------- lib/server.js | 22 ++++++++++++++++++ 2 files changed, 49 insertions(+), 36 deletions(-) create mode 100644 lib/server.js diff --git a/lib/index.js b/lib/index.js index 5966c9d0..c5873082 100644 --- a/lib/index.js +++ b/lib/index.js @@ -32,6 +32,33 @@ var Embark = { return this.contractsManager; }, + run: function(env) { + Embark.deploy(function(abi) { + Embark.buildAssets(abi); + var server = new Server(); + server.start(function() { + Embark.watch(); + }); + }); + }, + + build: function(env) { + Embark.deploy(function(abi) { + Embark.buildAssets(abi); + }); + }, + + blockchain: function(env, client) { + var blockchain = Blockchain(client); + blockchain.run({env: env}); + }, + + + + + + + deploy: function(done) { async.waterfall([ function loadConfig(callback) { @@ -90,22 +117,6 @@ var Embark = { }, server: function(callback) { - var finalhandler = require('finalhandler'); - var http = require('http'); - var serveStatic = require('serve-static'); - - // Serve up public/ftp folder - var serve = serveStatic('dist/', {'index': ['index.html', 'index.htm']}); - - // Create server - var server = http.createServer(function onRequest (req, res) { - serve(req, res, finalhandler(req, res)); - }); - - // Listen - console.log("listening on port 8000".underline.green); - server.listen(8000) ; - callback(); }, watch: function() { @@ -131,26 +142,6 @@ var Embark = { .on('unlink', path => console.log(`File ${path} has been removed`)) .on('ready', () => console.log('ready to watch changes')); console.log("done!"); - }, - - run: function(env) { - Embark.deploy(function(abi) { - Embark.buildAssets(abi); - Embark.server(function() { - Embark.watch(); - }); - }); - }, - - build: function(env) { - Embark.deploy(function(abi) { - Embark.buildAssets(abi); - }); - }, - - blockchain: function(env, client) { - var blockchain = Blockchain(client); - blockchain.run({env: env}); } }; diff --git a/lib/server.js b/lib/server.js new file mode 100644 index 00000000..095599af --- /dev/null +++ b/lib/server.js @@ -0,0 +1,22 @@ +var finalhandler = require('finalhandler'); +var http = require('http'); +var serveStatic = require('serve-static'); + +var Server = function(options) { + this.dist = options.dist || 'dist/'; + this.port = options.port || 8000; +}; + +Server.prototype.start = function(callback) { + var serve = serveStatic(this.dist, {'index': ['index.html', 'index.htm']}); + + var server = http.createServer(function onRequest (req, res) { + serve(req, res, finalhandler(req, res)); + }); + + console.log(("listening on port " + this.port).underline.green); + server.listen(this.port) ; + callback(); +}; + +module.exports = Server;