Skip to content

Commit

Permalink
[Master] dynamic logging, ability to disable explorer (hyperledger-ar…
Browse files Browse the repository at this point in the history
…chives#4313)

* dynamic logging, ability to disable explorer

closes #4308
closes #4307

Signed-off-by: Dave Kelsey <[email protected]>

* fix test due to previous PR

Signed-off-by: Dave Kelsey <[email protected]>
  • Loading branch information
Dave Kelsey authored Sep 20, 2018
1 parent d7c295a commit b8bc414
Show file tree
Hide file tree
Showing 16 changed files with 579 additions and 21 deletions.
16 changes: 16 additions & 0 deletions packages/composer-rest-server/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ const yargs = require('yargs')
.option('t', { alias: 'tls', describe: 'Enable TLS security for the REST API', type: 'boolean', default: process.env.COMPOSER_TLS || false })
.option('e', { alias: 'tlscert', describe: 'File containing the TLS certificate', type: 'string', default: process.env.COMPOSER_TLS_CERTIFICATE || defaultTlsCertificate })
.option('k', { alias: 'tlskey', describe: 'File containing the TLS private key', type: 'string', default: process.env.COMPOSER_TLS_KEY || defaultTlsKey })
.option('u', { alias: 'explorer', describe: 'Enable the test explorer web interface', type: 'boolean', default: process.env.COMPOSER_USEEXPLORER || true })
.option('d', { alias: 'loggingkey', describe: 'Specify the key to enable dynamic logging for the rest server (just pressing enter will not enable this feature)', type: 'string', default: process.env.COMPOSER_LOGGINGKEY || undefined })
.alias('v', 'version')
.version(version)
.help('h')
Expand All @@ -65,6 +67,8 @@ if (interactive) {
apikey: answers.apikey,
authentication: answers.authentication,
multiuser: answers.multiuser,
explorer: answers.explorer,
loggingkey: answers.loggingkey,
websockets: answers.websockets,
tls: answers.tls,
tlscert: answers.tlscert,
Expand All @@ -79,6 +83,8 @@ if (interactive) {
'-y': 'apikey',
'-a': 'authentication',
'-m': 'multiuser',
'-u': 'explorer',
'-d': 'loggingkey',
'-w': 'websockets',
'-t': 'tls',
'-e': 'tlscert',
Expand Down Expand Up @@ -113,6 +119,8 @@ if (interactive) {
apikey: yargs.y,
authentication: yargs.a,
multiuser: yargs.m,
explorer: yargs.u,
loggingkey: yargs.d,
websockets: yargs.w,
tls: yargs.t,
tlscert: yargs.e,
Expand All @@ -135,15 +143,23 @@ module.exports = promise.then((composer) => {
return server.listen(app.get('port'), () => {
app.emit('started');
let baseUrl = app.get('url').replace(/\/$/, '');
// eslint-disable-next-line no-console
console.log('Web server listening at: %s', baseUrl);
if (app.get('loopback-component-explorer')) {
let explorerPath = app.get('loopback-component-explorer').mountPath;
// eslint-disable-next-line no-console
console.log('Browse your REST API at %s%s', baseUrl, explorerPath);
}
const composerConfig = app.get('composer');
if (composerConfig && composerConfig.loggingkey) {
// eslint-disable-next-line no-console
console.log('Rest Server dynamic logging is enabled');
}
});

})
.catch((error) => {
// eslint-disable-next-line no-console
console.error(error);
process.exit(1);
});
11 changes: 11 additions & 0 deletions packages/composer-rest-server/lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,17 @@ class Util {
return answers.authentication;
}
},
{
name: 'explorer',
type: 'confirm',
message: 'Specify if you want to enable the explorer test interface:',
default: true
},
{
name: 'loggingkey',
type: 'input',
message: 'Specify a key if you want to enable dynamic logging:'
},
{
name: 'websockets',
type: 'confirm',
Expand Down
97 changes: 97 additions & 0 deletions packages/composer-rest-server/server/boot/composer-discovery.js
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,68 @@ function registerGetHistorianRecordsByIDMethod(app, dataSource, System, connecto

}

/**
* Register the 'setLogLevel' Composer system method.
* @param {Object} app The LoopBack application.
* @param {Object} dataSource The LoopBack data source.
*/
function registerSetLogLevelMethod(app, dataSource) {
const Admin = app.models.Admin;
const connector = dataSource.connector;

// Define and register the method.
Admin.setLogLevel = (key, newlevel, outputToConsole, outputToFile, callback) => {
if (app.get('composer').loggingkey === key) {
connector.setLogLevel(newlevel, outputToConsole, outputToFile, callback);
} else {
throw new Error('Unauthorized');
}
};
Admin.remoteMethod(
'setLogLevel', {
description: 'set logging level on rest server',
accepts: [{
arg: 'loggingkey',
type: 'string',
required: true,
http: {
source: 'path'
}
}, {
arg: 'newlevel',
type: 'string',
required: true,
http: {
source: 'path'
}
}, {
arg: 'outputToConsole',
type: 'boolean',
required: true,
http: {
source: 'path'
}
}, {
arg: 'outputToFile',
type: 'boolean',
required: true,
http: {
source: 'path'
}
}],
returns: {
type: [ 'object' ],
root: true
},
http: {
verb: 'post',
path: '/loglevel/:loggingkey/:newlevel/:outputToConsole/:outputToFile'
}
}
);

}

/**
* Discover all of the model definitions in the specified LoopBack data source.
* @param {Object} dataSource The LoopBack data source.
Expand Down Expand Up @@ -638,6 +700,33 @@ function createSystemModel(app, dataSource) {

}

/**
* Create all of the Composer system models.
* @param {Object} app The LoopBack application.
* @param {Object} dataSource The LoopBack data source.
*/
function createAdminModel(app, dataSource) {

// Create the system model schema.
let modelSchema = {
name: 'Admin',
description: 'Rest server methods',
plural: '/admin',
base: 'Model'
};
modelSchema = updateModelSchema(modelSchema);

// Create the system model which is an anchor for all system methods.
const Admin = app.loopback.createModel(modelSchema);

// Register the system model.
app.model(Admin, {
dataSource: dataSource,
public: true
});

}

/**
* Create all of the Composer system models.
* @param {Object} app The LoopBack application.
Expand Down Expand Up @@ -687,6 +776,7 @@ function registerSystemMethods(app, dataSource) {
registerGetAllHistorianRecordsMethod,
registerGetHistorianRecordsByIDMethod
];

registerMethods.forEach((registerMethod) => {
registerMethod(app, dataSource, System, connector);
});
Expand Down Expand Up @@ -839,6 +929,13 @@ module.exports = function (app, callback) {
// Register the system methods.
registerSystemMethods(app, dataSource);

createAdminModel(app, dataSource);
// enable dynamic logging support if requested
if (app.settings.composer.loggingkey) {
registerSetLogLevelMethod(app, dataSource);
}


// Create the query model
createQueryModel(app, dataSource);

Expand Down
10 changes: 9 additions & 1 deletion packages/composer-rest-server/server/boot/root.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,15 @@
module.exports = function(server) {
let router = server.loopback.Router();
router.get('/', (req, res, next) => {
res.redirect('/explorer/');
const useExplorer = server.settings.composer.explorer === undefined ||
server.settings.composer.explorer === null ||
server.settings.composer.explorer === true;

if (useExplorer) {
res.redirect('/explorer/');
} else {
res.redirect('/status/');
}
});
router.get('/status', server.loopback.status());
server.use(router);
Expand Down
1 change: 1 addition & 0 deletions packages/composer-rest-server/server/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"port": 3000,
"authentication": false,
"multiuser": false,
"explorer": true,
"websockets": true,
"tls": false,
"tlscert": "/path/to/cert.pem",
Expand Down
26 changes: 20 additions & 6 deletions packages/composer-rest-server/server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ module.exports = function (composer) {
// model dependent on whether or not the multiple user option has been specified.
const models = require('./model-config.json');
const multiuser = !!composer.multiuser;
const useExplorer = composer.explorer === undefined || composer.explorer === null || composer.explorer === true;
models.Card.public = multiuser;

// Allow environment variable overrides for the datasources.json file.
Expand All @@ -54,17 +55,22 @@ module.exports = function (composer) {
}

// Call the boot process which will load all models and execute all boot scripts.
let loopbackExplorer = null;
if (useExplorer) {
loopbackExplorer = {
mountPath: '/explorer',
uiDirs: [
path.resolve(__dirname, '..', 'public')
]
};
}

const bootOptions = {
appRootDir: __dirname,
models: models,
dataSources: dataSources,
components: {
'loopback-component-explorer': {
mountPath: '/explorer',
uiDirs: [
path.resolve(__dirname, '..', 'public')
]
}
'loopback-component-explorer': loopbackExplorer
}
};
boot(app, bootOptions, (error) => {
Expand Down Expand Up @@ -221,15 +227,23 @@ if (require.main === module) {
return server.listen(() => {
app.emit('started');
let baseUrl = app.get('url').replace(/\/$/, '');
// eslint-disable-next-line no-console
console.log('Web server listening at: %s', baseUrl);
if (app.get('loopback-component-explorer')) {
let explorerPath = app.get('loopback-component-explorer').mountPath;
// eslint-disable-next-line no-console
console.log('Browse your REST API at %s%s', baseUrl, explorerPath);
}
const composerConfig = app.get('composer');
if (composerConfig && composerConfig.loggingkey) {
// eslint-disable-next-line no-console
console.log('Rest Server dynamic logging is enabled');
}
});

})
.catch((error) => {
// eslint-disable-next-line no-console
console.error(error);
process.exit(1);
});
Expand Down
7 changes: 7 additions & 0 deletions packages/composer-rest-server/server/servercmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,19 @@ function startRestServer(composer){
return app.listen(function () {
app.emit('started');
let baseUrl = app.get('url').replace(/\/$/, '');
// eslint-disable-next-line no-console
console.log('Web server listening at: %s', baseUrl);
/* istanbul ignore next */
if (app.get('loopback-component-explorer')) {
let explorerPath = app.get('loopback-component-explorer').mountPath;
// eslint-disable-next-line no-console
console.log('Browse your REST API at %s%s', baseUrl, explorerPath);
}
const composerConfig = app.get('composer');
if (composerConfig && composerConfig.loggingkey) {
// eslint-disable-next-line no-console
console.log('Rest Server dynamic logging is enabled');
}
});
});

Expand Down
Loading

0 comments on commit b8bc414

Please sign in to comment.