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
No related branches found
No related tags found
No related merge requests found
#!/usr/bin/env node #!/usr/bin/env node
"use strict"; "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"; "use strict";
const co = require('co'); const co = require('co');
const _ = require('underscore');
const Server = require('./server'); const Server = require('./server');
const bma = require('./app/lib/streams/bma');
const webmin = require('./app/lib/streams/webmin'); const webmin = require('./app/lib/streams/webmin');
const logger = require('./app/lib/logger')('duniter'); const logger = require('./app/lib/logger')('duniter');
...@@ -61,69 +61,76 @@ module.exports.statics = { ...@@ -61,69 +61,76 @@ module.exports.statics = {
ip: wmHost || 'localhost', ip: wmHost || 'localhost',
port: wmPort || 9220 port: wmPort || 9220
}], httpLogs !== false), }], httpLogs !== false),
autoStack: () => {
startNode: (server, conf) => co(function *() { const cli = require('./app/cli');
const stack = {
logger.info(">> NODE STARTING");
// Public http interface
let bmapi = yield bma(server, null, conf.httplogs);
// Routing documents
server.routing();
// Services registerDependency: (requiredObject) => {
yield module.exports.statics.startServices(server); for (const command of (requiredObject.duniter.cli || [])) {
yield bmapi.openConnections(); 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);
}
});
/*************** process.on('unhandledRejection', (reason) => {
* HTTP ROUTING logger.error('Unhandled rejection: ' + reason);
**************/ });
server.router(server.conf.routing);
/*************** return co(function*() {
* UPnP try {
**************/ // Prepare the command
if (server.conf.upnp) { const command = cli(process.argv);
try { // If ever the process gets interrupted
if (server.upnpAPI) { process.on('SIGINT', () => {
server.upnpAPI.stopRegular(); co(function*() {
} yield command.closeCommand();
yield server.upnp(); process.exit();
server.upnpAPI.startRegular(); });
} catch (e) { });
logger.warn(e); // 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
});
} }
} }
/******************* for (const duniterModule of duniterModules) {
* BLOCK COMPUTING console.log('Registering module %s...', duniterModule.name);
******************/ stack.registerDependency(duniterModule.required);
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();
} }
yield server.stop();
return {}; return stack;
}) }
}; };
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment