From 8e9a35744837e8d50071e3371beb4ade6fed82ca Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Tue, 13 Mar 2018 06:24:07 -0400 Subject: [PATCH] move dashboard api into dashboard module --- lib/dashboard/dashboard.js | 30 +++++++++++++++++++ lib/modules/webserver/index.js | 2 +- lib/modules/webserver/server.js | 29 ++++-------------- .../contracts/another_storage.sol | 1 - 4 files changed, 36 insertions(+), 26 deletions(-) diff --git a/lib/dashboard/dashboard.js b/lib/dashboard/dashboard.js index 0bf7a7a5..ace882f3 100644 --- a/lib/dashboard/dashboard.js +++ b/lib/dashboard/dashboard.js @@ -27,6 +27,36 @@ class Dashboard { let console, monitor; let self = this; + let plugin = this.plugins.createPlugin('dashboard', {}); + plugin.registerAPICall( + 'ws', + '/embark/dashboard', + (ws, req) => { + let dashboardState = { contractsState: [], environment: "", status: "", availableServices: [] }; + + // TODO: doesn't feel quite right, should be refactored into a shared + // dashboard state + self.events.request('setDashboardState'); + + self.events.on('contractsState', (contracts) => { + dashboardState.contractsState = []; + + contracts.forEach(function (row) { + dashboardState.contractsState.push({contractName: row[0], address: row[1], status: row[2]}); + }); + ws.send(JSON.stringify(dashboardState)); + }); + self.events.on('status', (status) => { + dashboardState.status = status; + ws.send(JSON.stringify(dashboardState)); + }); + self.events.on('servicesState', (servicesState) => { + dashboardState.availableServices = servicesState; + ws.send(JSON.stringify(dashboardState)); + }); + } + ); + async.waterfall([ function startConsole(callback) { console = new Console({ diff --git a/lib/modules/webserver/index.js b/lib/modules/webserver/index.js index 40246a50..ee4f06c8 100644 --- a/lib/modules/webserver/index.js +++ b/lib/modules/webserver/index.js @@ -16,7 +16,7 @@ class WebServer { this.port = options.port || this.webServerConfig.port; this.events.emit("status", __("Starting Server")); - this.server = new Server({logger: this.logger, host: this.host, port: this.port, events: this.events}); + this.server = new Server({logger: this.logger, host: this.host, port: this.port, events: this.events, plugins: this.plugins}); this.setServiceCheck(); this.listenToCommands(); diff --git a/lib/modules/webserver/server.js b/lib/modules/webserver/server.js index cd542dd6..252e3f6f 100644 --- a/lib/modules/webserver/server.js +++ b/lib/modules/webserver/server.js @@ -13,6 +13,7 @@ class Server { this.port = options.port || 8000; this.hostname = options.host || 'localhost'; this.logger = options.logger; + this.plugins = options.plugins; } start(callback) { @@ -39,30 +40,10 @@ class Server { }); }); - app.ws('/embark/dashboard', function(ws, req) { - let dashboardState = { contractsState: [], environment: "", status: "", availableServices: [] }; - - // TODO: doesn't feel quite right, should be refactored into a shared - // dashboard state - self.events.request('setDashboardState'); - - self.events.on('contractsState', (contracts) => { - dashboardState.contractsState = []; - - contracts.forEach(function (row) { - dashboardState.contractsState.push({contractName: row[0], address: row[1], status: row[2]}); - }); - ws.send(JSON.stringify(dashboardState)); - }); - self.events.on('status', (status) => { - dashboardState.status = status; - ws.send(JSON.stringify(dashboardState)); - }); - self.events.on('servicesState', (servicesState) => { - dashboardState.availableServices = servicesState; - ws.send(JSON.stringify(dashboardState)); - }); - }); + let apiCalls = self.plugins.getPluginsProperty("apiCalls", "apiCalls"); + for (let apiCall of apiCalls) { + app[apiCall.method].apply(app, [apiCall.endpoint, apiCall.cb]); + } app.get('/embark', function (req, res) { res.send('Welcome to Embark') diff --git a/test_apps/contracts_app/contracts/another_storage.sol b/test_apps/contracts_app/contracts/another_storage.sol index a8c1c873..92233769 100644 --- a/test_apps/contracts_app/contracts/another_storage.sol +++ b/test_apps/contracts_app/contracts/another_storage.sol @@ -1,6 +1,5 @@ pragma solidity ^0.4.17; contract AnotherStorage { - address public simpleStorageAddress; address simpleStorageAddress2;