From fd953f7652e65a065c4ce98c08784fa2679d4658 Mon Sep 17 00:00:00 2001 From: cgeek <cem.moreau@gmail.com> Date: Thu, 29 Dec 2016 15:46:29 +0100 Subject: [PATCH] Preparing duniter-ui to be used as a Duniter module --- index.js | 61 +++++++++++++++++++++++++++++ package.json | 2 + server/controller/webmin.js | 37 ++++++++++-------- server/lib/routes.js | 6 +-- server/server.js | 77 ++++++++++++------------------------- 5 files changed, 112 insertions(+), 71 deletions(-) create mode 100644 index.js diff --git a/index.js b/index.js new file mode 100644 index 0000000..6a8b7cb --- /dev/null +++ b/index.js @@ -0,0 +1,61 @@ +"use strict"; + +const co = require('co'); +const bodyParser = require('body-parser'); +const http = require('http'); +const express = require('express'); +const path = require('path'); +const webminController = require('./server/controller/webmin.js'); + +module.exports = { + duniter: { + + 'cli': [{ + name: 'webstart', + desc: 'Do a webstart', + requires: ['service'], + promiseCallback: (duniterServer) => co(function*(){ + + try { + + /**************************************** + * SPECIALISATION + ***************************************/ + + const app = express(); + const HOTE = 'localhost'; + const PORT = 10500; + + /** + * Sur appel de l'URL /abc + */ + app.use(express.static(path.join(__dirname, '..', 'duniter-ui', 'public'))); + + app.use(bodyParser.urlencoded({ + extended: true + })); + app.use(bodyParser.json()); + + const wbmin = webminController(duniterServer); + const httpServer = http.createServer(app); + httpServer.listen(PORT, HOTE); + console.log("Serveur web disponible a l'adresse http://%s:%s", HOTE, PORT); + + require('./server/lib/routes').webmin(wbmin, app); + require('./server/lib/routes').webminWS(wbmin)(httpServer); + + yield wbmin.startHTTP(); + + // Never ending promise + return new Promise((resolve) => {}); + + /****************************************/ + + } catch (e) { + console.error(e); + process.exit(1); + } + }) + }] + } +}; diff --git a/package.json b/package.json index 500b099..5031f70 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,7 @@ { "name": "duniter-ui", "version": "0.1.25", + "main": "index.js", "dependencies": { "auto-reload-brunch": "2.0.0", "babel-brunch": "6.0.1", @@ -17,6 +18,7 @@ "json-brunch": "1.5.4", "materialize-css": "0.97.5", "moment": "2.6.0", + "node-pre-gyp": "^0.6.32", "sass-brunch": "2.0.0", "stylus-brunch": "2.0.0", "uglify-js-brunch": "2.0.1", diff --git a/server/controller/webmin.js b/server/controller/webmin.js index ccd1d88..a250519 100644 --- a/server/controller/webmin.js +++ b/server/controller/webmin.js @@ -8,18 +8,6 @@ const stream = require('stream'); const _ = require('underscore'); const Q = require('q'); const co = require('co'); -const keyring = require('duniter/app/lib/crypto/keyring'); -const Identity = require('duniter/app/lib/entity/identity'); -const rawer = require('duniter/app/lib/ucp/rawer'); -const logger = require('duniter/app/lib/logger')('webmin'); -const http2raw = require('duniter/app/lib/helpers/http2raw'); -const dos2unix = require('duniter/app/lib/system/dos2unix'); -const duniter = require('duniter'); -const contacter = require('duniter/app/lib/contacter'); -const bma = require('duniter/app/lib/streams/bma'); -const network = require('duniter/app/lib/system/network'); -const constants = require('duniter/app/lib/constants'); -const ucp = require('duniter/app/lib/ucp/buid'); module.exports = (duniterServer) => { return new WebAdmin(duniterServer); @@ -27,8 +15,20 @@ module.exports = (duniterServer) => { function WebAdmin (duniterServer) { + const logger = duniterServer.logger; + const keyring = duniterServer.lib.keyring; + const Identity = duniterServer.lib.Identity; + const rawer = duniterServer.lib.rawer; + const http2raw = duniterServer.lib.http2raw; + const dos2unix = duniterServer.lib.dos2unix; + const contacter = duniterServer.lib.contacter; + const bma = duniterServer.lib.bma; + const network = duniterServer.lib.network; + const constants = duniterServer.lib.constants; + const ucp = duniterServer.lib.ucp; + // Node instance: this is the object to be managed by the web admin - const server = duniterServer; + const server = this.server = duniterServer; let bmapi; const that = this; @@ -115,7 +115,14 @@ function WebAdmin (duniterServer) { }); this.startHTTP = () => co(function *() { + yield pluggedDALP; + try { + yield bmapi.openConnections(); return { success: true }; + } catch (e) { + logger.error(e); + return { success: false }; + } }); this.openUPnP = () => co(function *() { @@ -384,7 +391,7 @@ function WebAdmin (duniterServer) { }; yield that.applyNewKeyConf({ body: { conf :conf } }); } - yield startServicesP || (startServicesP = duniter.statics.startServices(server)); + yield startServicesP || (startServicesP = server.startServices()); that.push({ started: true }); return {}; }); @@ -392,7 +399,7 @@ function WebAdmin (duniterServer) { this.stopAllServices = () => co(function *() { // Allow services to be started startServicesP = null; - yield stopServicesP || (stopServicesP = duniter.statics.stopServices(server)); + yield stopServicesP || (stopServicesP = server.stopServices()); that.push({ stopped: true }); return {}; }); diff --git a/server/lib/routes.js b/server/lib/routes.js index 4a92382..1ec5dc1 100644 --- a/server/lib/routes.js +++ b/server/lib/routes.js @@ -2,9 +2,8 @@ const co = require('co'); const es = require('event-stream'); -const constants = require('duniter/app/lib/constants'); -const logger = require('duniter/app/lib/logger')('webmin'); const handleRequest = require('../lib/network').handleRequest; +const WEBMIN_LOGS_CACHE = 2000; const WebSocketServer = require('ws').Server; @@ -36,6 +35,7 @@ module.exports = { handleRequest(app.post.bind(app), '/webmin/data/duniter_import', webminCtrl.importData); }, webminWS: function(webminCtrl) { + const logger = webminCtrl.server.logger; return (httpServer) => { // Socket for synchronization events @@ -61,7 +61,7 @@ module.exports = { // The callback which write each new log message to websocket logger.addCallbackLogs((level, msg, timestamp) => { - lastLogs.splice(0, Math.max(0, lastLogs.length - constants.WEBMIN_LOGS_CACHE + 1)); + lastLogs.splice(0, Math.max(0, lastLogs.length - WEBMIN_LOGS_CACHE + 1)); lastLogs.push({ timestamp: timestamp, level: level, diff --git a/server/server.js b/server/server.js index ee12f77..0710843 100644 --- a/server/server.js +++ b/server/server.js @@ -1,62 +1,33 @@ #!/usr/bin/env node "use strict"; -const co = require('co'); +const co = require('co'); const duniter = require('duniter'); -const bodyParser = require('body-parser'); -const http = require('http'); -const express = require('express'); -const path = require('path'); -const webminController = require('./controller/webmin.js'); +const stack = duniter.statics.autoStack(); -const HOME_DUNITER_DATA_FOLDER = 'rml8'; +const modules = [ + require('../index') +]; -// Use netobs data folder -if (!process.argv.includes('--mdb')) { - process.argv.push('--mdb'); - process.argv.push(HOME_DUNITER_DATA_FOLDER); +for (const module of modules) { + stack.registerDependency(module); } -// Default action = start -if (process.argv.length === 4) process.argv.push('start'); +stack.registerDependency({ + duniter: { + cli: [{ + name: 'hello', + desc: 'Says hello to the world.', + requires: ['service'], + promiseCallback: (duniterServer) => co(function*(){ + console.log('Hello, world.'); + }) + }] + } +}); + +return co(function*(){ + yield stack.executeStack(); + console.log('Done'); +}); -// Disable Duniter logs -//duniter.statics.logger.mute(); - -duniter.statics.cli((duniterServer) => co(function*() { - - try { - - /**************************************** - * SPECIALISATION - ***************************************/ - - const app = express(); - const HOTE = 'localhost'; - const PORT = 10500; - - /** - * Sur appel de l'URL /abc - */ - app.use(express.static(path.join('..', 'public'))); - - app.use(bodyParser.urlencoded({ - extended: true - })); - app.use(bodyParser.json()); - - const wbmin = webminController(duniterServer); - const httpServer = http.createServer(app); - httpServer.listen(PORT, HOTE); - console.log("Serveur web disponible a l'adresse http://%s:%s", HOTE, PORT); - - require('./lib/routes').webmin(wbmin, app); - require('./lib/routes').webminWS(wbmin)(httpServer); - - /****************************************/ - - } catch (e) { - console.error(e); - process.exit(1); - } -})); -- GitLab