Skip to content
This repository was archived by the owner on Mar 8, 2020. It is now read-only.

Commit b8bc414

Browse files
authored
[Master] dynamic logging, ability to disable explorer (#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]>
1 parent d7c295a commit b8bc414

File tree

16 files changed

+579
-21
lines changed

16 files changed

+579
-21
lines changed

packages/composer-rest-server/cli.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ const yargs = require('yargs')
3939
.option('t', { alias: 'tls', describe: 'Enable TLS security for the REST API', type: 'boolean', default: process.env.COMPOSER_TLS || false })
4040
.option('e', { alias: 'tlscert', describe: 'File containing the TLS certificate', type: 'string', default: process.env.COMPOSER_TLS_CERTIFICATE || defaultTlsCertificate })
4141
.option('k', { alias: 'tlskey', describe: 'File containing the TLS private key', type: 'string', default: process.env.COMPOSER_TLS_KEY || defaultTlsKey })
42+
.option('u', { alias: 'explorer', describe: 'Enable the test explorer web interface', type: 'boolean', default: process.env.COMPOSER_USEEXPLORER || true })
43+
.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 })
4244
.alias('v', 'version')
4345
.version(version)
4446
.help('h')
@@ -65,6 +67,8 @@ if (interactive) {
6567
apikey: answers.apikey,
6668
authentication: answers.authentication,
6769
multiuser: answers.multiuser,
70+
explorer: answers.explorer,
71+
loggingkey: answers.loggingkey,
6872
websockets: answers.websockets,
6973
tls: answers.tls,
7074
tlscert: answers.tlscert,
@@ -79,6 +83,8 @@ if (interactive) {
7983
'-y': 'apikey',
8084
'-a': 'authentication',
8185
'-m': 'multiuser',
86+
'-u': 'explorer',
87+
'-d': 'loggingkey',
8288
'-w': 'websockets',
8389
'-t': 'tls',
8490
'-e': 'tlscert',
@@ -113,6 +119,8 @@ if (interactive) {
113119
apikey: yargs.y,
114120
authentication: yargs.a,
115121
multiuser: yargs.m,
122+
explorer: yargs.u,
123+
loggingkey: yargs.d,
116124
websockets: yargs.w,
117125
tls: yargs.t,
118126
tlscert: yargs.e,
@@ -135,15 +143,23 @@ module.exports = promise.then((composer) => {
135143
return server.listen(app.get('port'), () => {
136144
app.emit('started');
137145
let baseUrl = app.get('url').replace(/\/$/, '');
146+
// eslint-disable-next-line no-console
138147
console.log('Web server listening at: %s', baseUrl);
139148
if (app.get('loopback-component-explorer')) {
140149
let explorerPath = app.get('loopback-component-explorer').mountPath;
150+
// eslint-disable-next-line no-console
141151
console.log('Browse your REST API at %s%s', baseUrl, explorerPath);
142152
}
153+
const composerConfig = app.get('composer');
154+
if (composerConfig && composerConfig.loggingkey) {
155+
// eslint-disable-next-line no-console
156+
console.log('Rest Server dynamic logging is enabled');
157+
}
143158
});
144159

145160
})
146161
.catch((error) => {
162+
// eslint-disable-next-line no-console
147163
console.error(error);
148164
process.exit(1);
149165
});

packages/composer-rest-server/lib/util.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,17 @@ class Util {
8686
return answers.authentication;
8787
}
8888
},
89+
{
90+
name: 'explorer',
91+
type: 'confirm',
92+
message: 'Specify if you want to enable the explorer test interface:',
93+
default: true
94+
},
95+
{
96+
name: 'loggingkey',
97+
type: 'input',
98+
message: 'Specify a key if you want to enable dynamic logging:'
99+
},
89100
{
90101
name: 'websockets',
91102
type: 'confirm',

packages/composer-rest-server/server/boot/composer-discovery.js

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,68 @@ function registerGetHistorianRecordsByIDMethod(app, dataSource, System, connecto
482482

483483
}
484484

485+
/**
486+
* Register the 'setLogLevel' Composer system method.
487+
* @param {Object} app The LoopBack application.
488+
* @param {Object} dataSource The LoopBack data source.
489+
*/
490+
function registerSetLogLevelMethod(app, dataSource) {
491+
const Admin = app.models.Admin;
492+
const connector = dataSource.connector;
493+
494+
// Define and register the method.
495+
Admin.setLogLevel = (key, newlevel, outputToConsole, outputToFile, callback) => {
496+
if (app.get('composer').loggingkey === key) {
497+
connector.setLogLevel(newlevel, outputToConsole, outputToFile, callback);
498+
} else {
499+
throw new Error('Unauthorized');
500+
}
501+
};
502+
Admin.remoteMethod(
503+
'setLogLevel', {
504+
description: 'set logging level on rest server',
505+
accepts: [{
506+
arg: 'loggingkey',
507+
type: 'string',
508+
required: true,
509+
http: {
510+
source: 'path'
511+
}
512+
}, {
513+
arg: 'newlevel',
514+
type: 'string',
515+
required: true,
516+
http: {
517+
source: 'path'
518+
}
519+
}, {
520+
arg: 'outputToConsole',
521+
type: 'boolean',
522+
required: true,
523+
http: {
524+
source: 'path'
525+
}
526+
}, {
527+
arg: 'outputToFile',
528+
type: 'boolean',
529+
required: true,
530+
http: {
531+
source: 'path'
532+
}
533+
}],
534+
returns: {
535+
type: [ 'object' ],
536+
root: true
537+
},
538+
http: {
539+
verb: 'post',
540+
path: '/loglevel/:loggingkey/:newlevel/:outputToConsole/:outputToFile'
541+
}
542+
}
543+
);
544+
545+
}
546+
485547
/**
486548
* Discover all of the model definitions in the specified LoopBack data source.
487549
* @param {Object} dataSource The LoopBack data source.
@@ -638,6 +700,33 @@ function createSystemModel(app, dataSource) {
638700

639701
}
640702

703+
/**
704+
* Create all of the Composer system models.
705+
* @param {Object} app The LoopBack application.
706+
* @param {Object} dataSource The LoopBack data source.
707+
*/
708+
function createAdminModel(app, dataSource) {
709+
710+
// Create the system model schema.
711+
let modelSchema = {
712+
name: 'Admin',
713+
description: 'Rest server methods',
714+
plural: '/admin',
715+
base: 'Model'
716+
};
717+
modelSchema = updateModelSchema(modelSchema);
718+
719+
// Create the system model which is an anchor for all system methods.
720+
const Admin = app.loopback.createModel(modelSchema);
721+
722+
// Register the system model.
723+
app.model(Admin, {
724+
dataSource: dataSource,
725+
public: true
726+
});
727+
728+
}
729+
641730
/**
642731
* Create all of the Composer system models.
643732
* @param {Object} app The LoopBack application.
@@ -687,6 +776,7 @@ function registerSystemMethods(app, dataSource) {
687776
registerGetAllHistorianRecordsMethod,
688777
registerGetHistorianRecordsByIDMethod
689778
];
779+
690780
registerMethods.forEach((registerMethod) => {
691781
registerMethod(app, dataSource, System, connector);
692782
});
@@ -839,6 +929,13 @@ module.exports = function (app, callback) {
839929
// Register the system methods.
840930
registerSystemMethods(app, dataSource);
841931

932+
createAdminModel(app, dataSource);
933+
// enable dynamic logging support if requested
934+
if (app.settings.composer.loggingkey) {
935+
registerSetLogLevelMethod(app, dataSource);
936+
}
937+
938+
842939
// Create the query model
843940
createQueryModel(app, dataSource);
844941

packages/composer-rest-server/server/boot/root.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,15 @@
1717
module.exports = function(server) {
1818
let router = server.loopback.Router();
1919
router.get('/', (req, res, next) => {
20-
res.redirect('/explorer/');
20+
const useExplorer = server.settings.composer.explorer === undefined ||
21+
server.settings.composer.explorer === null ||
22+
server.settings.composer.explorer === true;
23+
24+
if (useExplorer) {
25+
res.redirect('/explorer/');
26+
} else {
27+
res.redirect('/status/');
28+
}
2129
});
2230
router.get('/status', server.loopback.status());
2331
server.use(router);

packages/composer-rest-server/server/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"port": 3000,
88
"authentication": false,
99
"multiuser": false,
10+
"explorer": true,
1011
"websockets": true,
1112
"tls": false,
1213
"tlscert": "/path/to/cert.pem",

packages/composer-rest-server/server/server.js

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ module.exports = function (composer) {
4545
// model dependent on whether or not the multiple user option has been specified.
4646
const models = require('./model-config.json');
4747
const multiuser = !!composer.multiuser;
48+
const useExplorer = composer.explorer === undefined || composer.explorer === null || composer.explorer === true;
4849
models.Card.public = multiuser;
4950

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

5657
// Call the boot process which will load all models and execute all boot scripts.
58+
let loopbackExplorer = null;
59+
if (useExplorer) {
60+
loopbackExplorer = {
61+
mountPath: '/explorer',
62+
uiDirs: [
63+
path.resolve(__dirname, '..', 'public')
64+
]
65+
};
66+
}
67+
5768
const bootOptions = {
5869
appRootDir: __dirname,
5970
models: models,
6071
dataSources: dataSources,
6172
components: {
62-
'loopback-component-explorer': {
63-
mountPath: '/explorer',
64-
uiDirs: [
65-
path.resolve(__dirname, '..', 'public')
66-
]
67-
}
73+
'loopback-component-explorer': loopbackExplorer
6874
}
6975
};
7076
boot(app, bootOptions, (error) => {
@@ -221,15 +227,23 @@ if (require.main === module) {
221227
return server.listen(() => {
222228
app.emit('started');
223229
let baseUrl = app.get('url').replace(/\/$/, '');
230+
// eslint-disable-next-line no-console
224231
console.log('Web server listening at: %s', baseUrl);
225232
if (app.get('loopback-component-explorer')) {
226233
let explorerPath = app.get('loopback-component-explorer').mountPath;
234+
// eslint-disable-next-line no-console
227235
console.log('Browse your REST API at %s%s', baseUrl, explorerPath);
228236
}
237+
const composerConfig = app.get('composer');
238+
if (composerConfig && composerConfig.loggingkey) {
239+
// eslint-disable-next-line no-console
240+
console.log('Rest Server dynamic logging is enabled');
241+
}
229242
});
230243

231244
})
232245
.catch((error) => {
246+
// eslint-disable-next-line no-console
233247
console.error(error);
234248
process.exit(1);
235249
});

packages/composer-rest-server/server/servercmd.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,19 @@ function startRestServer(composer){
4040
return app.listen(function () {
4141
app.emit('started');
4242
let baseUrl = app.get('url').replace(/\/$/, '');
43+
// eslint-disable-next-line no-console
4344
console.log('Web server listening at: %s', baseUrl);
4445
/* istanbul ignore next */
4546
if (app.get('loopback-component-explorer')) {
4647
let explorerPath = app.get('loopback-component-explorer').mountPath;
48+
// eslint-disable-next-line no-console
4749
console.log('Browse your REST API at %s%s', baseUrl, explorerPath);
4850
}
51+
const composerConfig = app.get('composer');
52+
if (composerConfig && composerConfig.loggingkey) {
53+
// eslint-disable-next-line no-console
54+
console.log('Rest Server dynamic logging is enabled');
55+
}
4956
});
5057
});
5158

0 commit comments

Comments
 (0)