update to src folder and npm scripts

Signed-off-by: Nakul Manchanda <nakul.manchanda@ibm.com>
This commit is contained in:
Nakul Manchanda
2020-09-03 10:50:22 -04:00
parent 42479d2e62
commit 9a777c2abc
22 changed files with 1461 additions and 483 deletions

View File

@@ -32,7 +32,7 @@ echo 'load sample-node-api config'
echo "start sample-node-api app on port ${MY_API_PORT}"
#start component
$NODE_BIN $COMPONENT_DIR/server/app.js \
$NODE_BIN $COMPONENT_DIR/src/index.js \
--service ${MY_API_NAME} \
--port ${MY_API_PORT} \
--key ${KEYSTORE_KEY} \

Binary file not shown.

1566
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -1,12 +1,16 @@
{
"name": "sample-api",
"name": "sample-node-api",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "node server/app.js --service sample-node-api --port 8080 --key sslcert/server.key --cert sslcert/server.cert",
"build": "npm run clean && cp -r server dist/server && cp -r bin dist/bin && cp *.yml dist && cp *.json dist && cp *.js dist && rimraf dist/package-lock.json",
"clean": "rimraf dist && mkdirp dist"
"dev": "nodemon src/index.js",
"dev:https": "nodemon src/index.js --key sslcert/server.key --cert sslcert/server.cert",
"start": "node src/index.js",
"build": "npm run clean && cp -r src dist/src && cp -r bin dist/bin && cp *.yml dist && cp *.json dist && cp README.md dist",
"dist": "npm run clean && npm install && npm run build && npm run pack",
"clean": "rimraf dist && mkdirp dist",
"pack": "copy-node-modules . dist"
},
"author": "",
"license": "EPL-2.0",
@@ -14,11 +18,13 @@
"cors": "^2.8.5",
"express": "^4.16.4",
"swagger-ui-express": "^4.1.4",
"yargs": "^8.0.2"
"yargs": "^14.2.1"
},
"devDependencies": {
"copy-node-modules": "^1.1.1",
"mkdirp": "^1.0.3",
"node-fetch": "^2.3.0",
"nodemon": "^2.0.4",
"rimraf": "^3.0.2"
}
}

View File

@@ -1,168 +0,0 @@
/*
This program and the accompanying materials are
made available under the terms of the Eclipse Public License v2.0 which accompanies
this distribution, and is available at https://www.eclipse.org/legal/epl-v20.html
SPDX-License-Identifier: EPL-2.0
Copyright IBM Corporation 2020
*/
const express = require('express');
const http = require('http');
const https = require('https');
const fs = require('fs');
const path = require("path");
const cors = require('cors');
function buildConfig(argv) {
const config = {
'port': argv.port /*|| port*/,
'https': {
'key': argv.key /*|| key*/,
'cert': argv.cert /*|| cert*/,
'pfx': argv.pfx,
'passphrase': argv.pass,
}
};
return config;
}
function loadCertificateFiles(config) {
// load https certs file content
if (config && config.https) {
['key', 'cert', 'pfx'].forEach(key => {
if (config.https[key]) {
let file = config.https[key];
config.https[key] = fs.readFileSync(file);
}
});
}
return config;
};
function validateParams (argv) {
let isValid = true;
const serviceFor=argv.s;
if((argv.p==='' || !argv.p) && isValid) {
isValid = false;
process.stderr.write(`[${serviceFor}] port configuration is missing\n`);
}
if( (argv.k==='' && argv.c==='' && argv.x==='' && argv.w==='') && isValid) {
isValid = false;
process.stderr.write(`[${serviceFor}] https configuration is missing\n`);
}
if( ( (argv.k==='' && argv.c>'') || (argv.k>'' && argv.c==='')
|| (argv.x==='' && argv.w>'' && argv.k==='' && argv.c==='')
|| (argv.x==='' && argv.w>'' && !(argv.k>'' && argv.c>''))
|| (argv.x>'' && argv.w==='') ) && isValid) {
isValid = false;
process.stderr.write(`[${serviceFor}] https configuration is missing\n`);
}
if(!isValid) {
process.stderr.write(`[${serviceFor}] is failed to start, error:\n`);
process.exit(1);
return false;
}
return true;
}
var argv = require('yargs')
.usage('Usage: $0 [options]')
.option('s', {
alias: 'service',
description: 'service-for path',
default: ''
})
.option('p', {
alias: 'port',
description: 'listening port'
})
.option('k', {
alias: 'key',
default: '',
description: 'server key'
})
.option('c', {
alias: 'cert',
default: '',
description: 'server cert',
})
.option('x', {
alias: 'pfx',
default: '',
description: 'server pfx',
})
.option('w', {
alias: 'pass',
default: '',
description: 'server pfx passphrase',
})
.option('v', {
alias: 'verbose',
default: false,
description: 'show request logs',
type: 'boolean'
})
.help('h')
.alias('h', 'help')
.check(validateParams)
.argv;
let config = buildConfig(argv);
config = loadCertificateFiles(config);
const {https:{key, cert}} = config;
const credentials = { key, cert };
const app = express();
const swaggerUi = require('swagger-ui-express');
const swaggerDocument = require('./swagger.json');
var options = {
explorer: false
};
// app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument, options));
app.use('/api-docs-ui', function(req, res, next){
swaggerDocument.host = req.get('host');
req.swaggerDoc = swaggerDocument;
next();
}, swaggerUi.serve, swaggerUi.setup());
//TODO: use for whitelist only
app.use(cors());
const routes = require('./routes/index.route');
app.get('/', (req, res) => res.send('Hello World!'));
app.get('/health', (req, res) => {
const healthcheck = {
uptime: process.uptime(),
message: 'OK',
timestamp: Date.now()
};
res.send(JSON.stringify(healthcheck));
});
app.use(routes);
app.use('/api-docs',function(req, res, next){
swaggerDocument.host = req.get('host');
res.send(swaggerDocument);
});
const httpServer = http.createServer(app);
const httpsServer = https.createServer(credentials, app);
httpServer.listen(config.port);
httpsServer.listen(config.port+1);
console.log(`http server listening at port ${config.port}`);
console.log(`https server listening at port ${config.port + 1}`);
module.exports = { app };

107
src/config.js Normal file
View File

@@ -0,0 +1,107 @@
const fs = require('fs');
const path = require("path");
function buildConfig(argv) {
let config = {
'serviceName': argv.service,
'port': argv.port,
'https': {
'key': argv.key ,
'cert': argv.cert,
},
};
config.isHttps = isHttps(config);
if(config.isHttps) {
config = loadCertificateFiles(config);
}
return config;
}
function isHttps(config) {
return config.https.key>'' && config.https.cert>''
}
function loadCertificateFiles(config) {
// load https certs file content
if (config && config.https) {
['key', 'cert'].forEach(key => {
if (config.https[key]) {
let file = config.https[key];
config.https[key] = fs.readFileSync(file);
}
});
}
return config;
};
function validateParams (argv) {
let isValid = true;
const serviceName=argv.s;
if((argv.p==='' || !argv.p) && isValid) {
isValid = false;
process.stderr.write(`[${serviceName}] port configuration is missing\n`);
}
if( ( (argv.k==='' && argv.c>'') || (argv.k>'' && argv.c==='') ) && isValid) {
isValid = false;
process.stderr.write(`[${serviceName}] https configuration is missing\n`);
}
if(!isValid) {
process.stderr.write(`[${serviceName}] is failed to start, error:\n`);
process.exit(1);
return false;
}
return true;
}
function setUpParams() {
var argv = require('yargs')
.usage('Usage: $0 [options]')
.option('s', {
alias: 'service',
description: 'service-for path',
default: 'sample-node-api'
})
.option('p', {
alias: 'port',
description: 'listening port',
default: 18000
})
.option('k', {
alias: 'key',
default: '',
description: 'server key'
})
.option('c', {
alias: 'cert',
default: '',
description: 'server cert',
})
.option('v', {
alias: 'verbose',
default: false,
description: 'show request logs',
type: 'boolean'
})
.help('h')
.alias('h', 'help')
.check(validateParams)
.argv;
return argv;
}
function buildConfigFromParams() {
let argv = setUpParams();
let config = buildConfig(argv);
return config;
}
const config = buildConfigFromParams();
module.exports = config;

39
src/index.js Normal file
View File

@@ -0,0 +1,39 @@
/*
This program and the accompanying materials are
made available under the terms of the Eclipse Public License v2.0 which accompanies
this distribution, and is available at https://www.eclipse.org/legal/epl-v20.html
SPDX-License-Identifier: EPL-2.0
Copyright IBM Corporation 2020
*/
const express = require('express');
const http = require('http');
const https = require('https');
const cors = require('cors');
//build config from params
const config = require('./config');
const {https:{ key, cert}, port, isHttps, serviceName} = config;
const credentials = {key, cert};
//setup app & its routes
const app = express();
app.use(cors());
const routes = require('./routes/index.route');
app.use(routes);
//start http server
const httpServer = http.createServer(app);
httpServer.listen(port);
console.log(`[${serviceName}] http server listening at port ${port}`);
//start https server
if(isHttps) {
const httpsServer = https.createServer(credentials, app);
httpsServer.listen(port+1);
console.log(`[${serviceName}] https server listening at port ${port + 1}`);
}
module.exports = { app };

View File

@@ -11,10 +11,22 @@
const express = require('express');
const cars = require('./cars.route');
const accounts = require('./accounts.route');
const swagger = require('./swagger.route');
const router = express.Router();
router.use('/cars', cars);
router.use('/accounts', accounts);
router.use('/', swagger);
router.get('/', (req, res) => res.send('Sample Node API Version1'));
router.get('/health', (req, res) => {
const healthcheck = {
uptime: process.uptime(),
message: 'OK',
timestamp: Date.now()
};
res.send(JSON.stringify(healthcheck));
});
module.exports = router;

View File

@@ -0,0 +1,19 @@
const express = require('express');
const router = express.Router({ mergeParams: true });
const swaggerUi = require('swagger-ui-express');
const swaggerDocument = require('../swagger.json');
router.use('/api-docs-ui', function(req, res, next){
swaggerDocument.host = req.get('host');
req.swaggerDoc = swaggerDocument;
next();
}, swaggerUi.serve, swaggerUi.setup());
router.use('/api-docs',function(req, res, next){
swaggerDocument.host = req.get('host');
res.send(swaggerDocument);
});
module.exports = router;

View File

@@ -8,7 +8,7 @@
Copyright IBM Corporation 2020
*/
const data = require('../../data');
const data = require('../data');
const get = function(_id){
return getAll().find(account => account._id == _id);

View File

@@ -8,7 +8,7 @@
Copyright IBM Corporation 2020
*/
const data = require('../../data');
const data = require('../data');
const carsService = require('./cars.service');

View File

@@ -8,7 +8,7 @@
Copyright IBM Corporation 2020
*/
const data = require('../../data');
const data = require('../data');
const get = function(_id){
return getAll().find(car => car._id == _id);

View File

@@ -3,7 +3,7 @@
,"info":{"description":"Sample Node API"
,"version":"1.0"
,"title":"Sample Node API"}
,"host":"localhost:8080"
,"host":"localhost:18000"
,"basePath":"/"
,"tags":[{"name":"Accounts","description":"Accounts API"},{"name":"Cars","description":"Cars API"}]
,"schemes":["http"]

View File

@@ -1,5 +1,5 @@
## start locally
node server/app.js --service sample-node-api --port 8080 --key sslcert/server.key --cert sslcert/server.cert -v
node src/index.js --service sample-node-api --port 8080 --key sslcert/server.key --cert sslcert/server.cert -v
cd ~/zowe/extenders/sample-node-api
## start on z/os uss
@@ -8,9 +8,8 @@ KEYSTORE_ALIAS=localhost
KEYSTORE_PREFIX="${KEYSTORE_DIRECTORY}/${KEYSTORE_ALIAS}/${KEYSTORE_ALIAS}.keystore"
KEYSTORE_KEY=${KEYSTORE_PREFIX}.key
KEYSTORE_CERTIFICATE=${KEYSTORE_PREFIX}.cer-ebcdic
node server/app.js --service sample-node-api --port 18000 --key ${KEYSTORE_KEY} --cert ${KEYSTORE_CERTIFICATE} -v
node src/index.js --service sample-node-api --port 18000 --key ${KEYSTORE_KEY} --cert ${KEYSTORE_CERTIFICATE} -v
# register with API ML layer
cd ~/zowe/extenders/sample-node-api
iconv -f IBM-1047 -t IBM-850 sample-node-api.http.yml > bin/sample-node-api-http.yml
iconv -f IBM-1047 -t IBM-850 sample-node-api.http.yml > ~/zowe/instance/workspace/api-mediation/api-defs/sample-node-api.yml
iconv -f IBM-1047 -t IBM-850 sample-node-api.yml > ~/zowe/instance/workspace/api-mediation/api-defs/sample-node-api.yml