-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·165 lines (150 loc) · 7.13 KB
/
cli.js
File metadata and controls
executable file
·165 lines (150 loc) · 7.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/usr/bin/env node
/*
* Licensed under the Apache License, Version 2.0 (the 'License');
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an 'AS IS' BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
process.env.SUPPRESS_NO_CONFIG_WARNING = true;
const version = 'v' + require('./package.json').version;
const path = require('path');
const server = require('./server/server');
const Util = require('./lib/util');
const defaultTlsCertificate = path.resolve(__dirname, 'cert.pem');
const defaultTlsKey = path.resolve(__dirname, 'key.pem');
const yargs = require('yargs')
.wrap(null)
.usage('Usage: $0 [options]')
.option('c', { alias: 'card', describe: 'The name of the business network card to use', type: 'string', default: process.env.COMPOSER_CARD || undefined })
.option('n', { alias: 'namespaces', describe: 'Use namespaces if conflicting types exist', type: 'string', default: process.env.COMPOSER_NAMESPACES || 'always', choices: ['always', 'required', 'never'] })
.option('p', { alias: 'port', describe: 'The port to serve the REST API on', type: 'number', default: process.env.COMPOSER_PORT || undefined })
.option('y', { alias: 'apikey', describe: 'The API key to get access to the REST API', type: 'string', default: process.env.COMPOSER_APIKEY || undefined })
.option('a', { alias: 'authentication', describe: 'Enable authentication for the REST API using Passport', type: 'boolean', default: process.env.COMPOSER_AUTHENTICATION || false })
.option('m', { alias: 'multiuser', describe: 'Enable multiple user and identity management using wallets (implies -a)', type: 'boolean', default: process.env.COMPOSER_MULTIUSER || false })
.option('w', { alias: 'websockets', describe: 'Enable event publication over WebSockets', type: 'boolean', default: process.env.COMPOSER_WEBSOCKETS || true })
.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')
.alias('h', 'help')
.argv;
// See if we need to run interactively.
// We check to see if no command line arguments have been supplied,
// and then check to see that none of the required arguments have
// been supplied via environment variables have been specified either.
const interactive = process.argv.slice(2).length === 0 && // No command line arguments supplied.
['c'].every((flag) => {
return yargs[flag] === undefined;
});
let promise;
if (interactive) {
// Get details of the server that we want to run
promise = Util.getConnectionSettings()
.then((answers) => {
// augment the app with the extra config that we've just collected
const composer = {
card: answers.card,
namespaces: answers.namespaces,
apikey: answers.apikey,
authentication: answers.authentication,
multiuser: answers.multiuser,
explorer: answers.explorer,
loggingkey: answers.loggingkey,
websockets: answers.websockets,
tls: answers.tls,
tlscert: answers.tlscert,
tlskey: answers.tlskey
};
console.log('\nTo restart the REST server using the same options, issue the following command:');
let cmd = [ 'composer-rest-server' ];
const args = {
'-c': 'card',
'-n': 'namespaces',
'-p': 'port',
'-y': 'apikey',
'-a': 'authentication',
'-m': 'multiuser',
'-u': 'explorer',
'-d': 'loggingkey',
'-w': 'websockets',
'-t': 'tls',
'-e': 'tlscert',
'-k': 'tlskey'
};
for (let arg in args) {
const propName = args[arg];
if (composer[propName]) {
cmd.push(arg, composer[propName]);
}
}
console.log(' ', cmd.join(' '));
console.log();
return composer;
});
} else {
// if -m (multiuser) was specified, it implies -a (authentication)
if (yargs.m) {
yargs.a = true;
}
// make sure we have args for all required parms otherwise error
if (yargs.c === undefined) {
promise = Promise.reject('Missing parameter. Please run composer-rest-server -h to see usage details');
} else {
promise = Promise.resolve({
card: yargs.c,
namespaces: yargs.n,
port: yargs.p,
apikey: yargs.y,
authentication: yargs.a,
multiuser: yargs.m,
explorer: yargs.u,
loggingkey: yargs.d,
websockets: yargs.w,
tls: yargs.t,
tlscert: yargs.e,
tlskey: yargs.k
});
}
}
// Now start the REST server.
module.exports = promise.then((composer) => {
// Create the LoopBack application.
return server(composer);
})
.then((result) => {
// Start the LoopBack application.
const app = result.app, server = result.server;
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);
});