diff --git a/bin/duniter b/bin/duniter index 79b3b0da5010ce61147996973cc741bfe7c5c785..cc6a71a91ee649d1bace05fd873a9b323c4b4ee3 100755 --- a/bin/duniter +++ b/bin/duniter @@ -1,4 +1,24 @@ #!/usr/bin/env node "use strict"; -require('../index').statics.cli(); +const co = require('co'); +const duniter = require('../index'); +const stack = duniter.statics.autoStack(); + +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'); +}); diff --git a/index.js b/index.js index d49d33285f4b56fba5067f6a34367d8015c99888..b1b988df5f0b41bf9357b355678b38086566e2c7 100644 --- a/index.js +++ b/index.js @@ -1,8 +1,8 @@ "use strict"; const co = require('co'); +const _ = require('underscore'); const Server = require('./server'); -const bma = require('./app/lib/streams/bma'); const webmin = require('./app/lib/streams/webmin'); const logger = require('./app/lib/logger')('duniter'); @@ -61,69 +61,76 @@ module.exports.statics = { ip: wmHost || 'localhost', port: wmPort || 9220 }], httpLogs !== false), + autoStack: () => { - startNode: (server, conf) => co(function *() { - - logger.info(">> NODE STARTING"); - - // Public http interface - let bmapi = yield bma(server, null, conf.httplogs); - - // Routing documents - server.routing(); + const cli = require('./app/cli'); + const stack = { - // Services - yield module.exports.statics.startServices(server); - yield bmapi.openConnections(); + registerDependency: (requiredObject) => { + for (const command of (requiredObject.duniter.cli || [])) { + cli.addCommand({ name: command.name, desc: command.desc }, command.requires, command.promiseCallback); + } + }, - logger.info('>> Server ready!'); - }), + executeStack: () => { - startServices: (server) => co(function *() { + // Specific errors handling + process.on('uncaughtException', (err) => { + // Dunno why this specific exception is not caught + if (err.code !== "EADDRNOTAVAIL" && err.code !== "EINVAL") { + logger.error(err); + process.exit(1); + } + }); - /*************** - * HTTP ROUTING - **************/ - server.router(server.conf.routing); + process.on('unhandledRejection', (reason) => { + logger.error('Unhandled rejection: ' + reason); + }); - /*************** - * UPnP - **************/ - if (server.conf.upnp) { - try { - if (server.upnpAPI) { - server.upnpAPI.stopRegular(); - } - yield server.upnp(); - server.upnpAPI.startRegular(); - } catch (e) { - logger.warn(e); + return co(function*() { + try { + // Prepare the command + const command = cli(process.argv); + // If ever the process gets interrupted + process.on('SIGINT', () => { + co(function*() { + yield command.closeCommand(); + process.exit(); + }); + }); + // Executes the command + yield command.execute(); + process.exit(); + } catch (e) { + logger.error(e); + process.exit(1); + } + }); + } + }; + + const pjson = require('./package.json'); + const duniterModules = []; + + // Look for compliant packages + const prodDeps = Object.keys(pjson.dependencies); + const devDeps = Object.keys(pjson.devDependencies); + const duniterDeps = _.filter(prodDeps.concat(devDeps), (dep) => dep.match(/^duniter-/)); + for(const dep of duniterDeps) { + const required = require(dep); + if (required.duniter) { + duniterModules.push({ + name: dep, + required + }); } } - /******************* - * BLOCK COMPUTING - ******************/ - if (server.conf.participate) { - server.startBlockComputation(); - } - - /*********************** - * CRYPTO NETWORK LAYER - **********************/ - yield server.start(); - - return {}; - }), - - stopServices: (server) => co(function *() { - - server.router(false); - if (server.conf.participate) { - server.stopBlockComputation(); + for (const duniterModule of duniterModules) { + console.log('Registering module %s...', duniterModule.name); + stack.registerDependency(duniterModule.required); } - yield server.stop(); - return {}; - }) + return stack; + } };