Skip to content
Snippets Groups Projects
Commit 807d8230 authored by Cédric Moreau's avatar Cédric Moreau
Browse files

[fix] #739 Create a module API for specialization

parent b176a8c7
Branches
Tags
No related merge requests found
#!/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');
});
"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) {
return co(function*() {
try {
if (server.upnpAPI) {
server.upnpAPI.stopRegular();
}
yield server.upnp();
server.upnpAPI.startRegular();
// 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.warn(e);
logger.error(e);
process.exit(1);
}
});
}
};
/*******************
* BLOCK COMPUTING
******************/
if (server.conf.participate) {
server.startBlockComputation();
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
});
}
}
/***********************
* 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;
}
};
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment