mirror of
https://github.com/jlengrand/sample-node-api.git
synced 2026-03-10 08:41:23 +00:00
update to src folder and npm scripts
Signed-off-by: Nakul Manchanda <nakul.manchanda@ibm.com>
This commit is contained in:
@@ -32,7 +32,7 @@ echo 'load sample-node-api config'
|
|||||||
|
|
||||||
echo "start sample-node-api app on port ${MY_API_PORT}"
|
echo "start sample-node-api app on port ${MY_API_PORT}"
|
||||||
#start component
|
#start component
|
||||||
$NODE_BIN $COMPONENT_DIR/server/app.js \
|
$NODE_BIN $COMPONENT_DIR/src/index.js \
|
||||||
--service ${MY_API_NAME} \
|
--service ${MY_API_NAME} \
|
||||||
--port ${MY_API_PORT} \
|
--port ${MY_API_PORT} \
|
||||||
--key ${KEYSTORE_KEY} \
|
--key ${KEYSTORE_KEY} \
|
||||||
|
|||||||
Binary file not shown.
1574
package-lock.json
generated
1574
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
16
package.json
16
package.json
@@ -1,12 +1,16 @@
|
|||||||
{
|
{
|
||||||
"name": "sample-api",
|
"name": "sample-node-api",
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"description": "",
|
"description": "",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"start": "node server/app.js --service sample-node-api --port 8080 --key sslcert/server.key --cert sslcert/server.cert",
|
"dev": "nodemon src/index.js",
|
||||||
"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",
|
"dev:https": "nodemon src/index.js --key sslcert/server.key --cert sslcert/server.cert",
|
||||||
"clean": "rimraf dist && mkdirp dist"
|
"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": "",
|
"author": "",
|
||||||
"license": "EPL-2.0",
|
"license": "EPL-2.0",
|
||||||
@@ -14,11 +18,13 @@
|
|||||||
"cors": "^2.8.5",
|
"cors": "^2.8.5",
|
||||||
"express": "^4.16.4",
|
"express": "^4.16.4",
|
||||||
"swagger-ui-express": "^4.1.4",
|
"swagger-ui-express": "^4.1.4",
|
||||||
"yargs": "^8.0.2"
|
"yargs": "^14.2.1"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
"copy-node-modules": "^1.1.1",
|
||||||
"mkdirp": "^1.0.3",
|
"mkdirp": "^1.0.3",
|
||||||
"node-fetch": "^2.3.0",
|
"node-fetch": "^2.3.0",
|
||||||
|
"nodemon": "^2.0.4",
|
||||||
"rimraf": "^3.0.2"
|
"rimraf": "^3.0.2"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
168
server/app.js
168
server/app.js
@@ -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
107
src/config.js
Normal 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
39
src/index.js
Normal 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 };
|
||||||
@@ -11,10 +11,22 @@
|
|||||||
const express = require('express');
|
const express = require('express');
|
||||||
const cars = require('./cars.route');
|
const cars = require('./cars.route');
|
||||||
const accounts = require('./accounts.route');
|
const accounts = require('./accounts.route');
|
||||||
|
const swagger = require('./swagger.route');
|
||||||
|
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
|
|
||||||
router.use('/cars', cars);
|
router.use('/cars', cars);
|
||||||
router.use('/accounts', accounts);
|
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;
|
module.exports = router;
|
||||||
19
src/routes/swagger.route.js
Normal file
19
src/routes/swagger.route.js
Normal 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;
|
||||||
|
|
||||||
@@ -8,7 +8,7 @@
|
|||||||
Copyright IBM Corporation 2020
|
Copyright IBM Corporation 2020
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const data = require('../../data');
|
const data = require('../data');
|
||||||
|
|
||||||
const get = function(_id){
|
const get = function(_id){
|
||||||
return getAll().find(account => account._id == _id);
|
return getAll().find(account => account._id == _id);
|
||||||
@@ -8,7 +8,7 @@
|
|||||||
Copyright IBM Corporation 2020
|
Copyright IBM Corporation 2020
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const data = require('../../data');
|
const data = require('../data');
|
||||||
|
|
||||||
const carsService = require('./cars.service');
|
const carsService = require('./cars.service');
|
||||||
|
|
||||||
@@ -8,7 +8,7 @@
|
|||||||
Copyright IBM Corporation 2020
|
Copyright IBM Corporation 2020
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const data = require('../../data');
|
const data = require('../data');
|
||||||
|
|
||||||
const get = function(_id){
|
const get = function(_id){
|
||||||
return getAll().find(car => car._id == _id);
|
return getAll().find(car => car._id == _id);
|
||||||
@@ -3,7 +3,7 @@
|
|||||||
,"info":{"description":"Sample Node API"
|
,"info":{"description":"Sample Node API"
|
||||||
,"version":"1.0"
|
,"version":"1.0"
|
||||||
,"title":"Sample Node API"}
|
,"title":"Sample Node API"}
|
||||||
,"host":"localhost:8080"
|
,"host":"localhost:18000"
|
||||||
,"basePath":"/"
|
,"basePath":"/"
|
||||||
,"tags":[{"name":"Accounts","description":"Accounts API"},{"name":"Cars","description":"Cars API"}]
|
,"tags":[{"name":"Accounts","description":"Accounts API"},{"name":"Cars","description":"Cars API"}]
|
||||||
,"schemes":["http"]
|
,"schemes":["http"]
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
## start locally
|
## 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
|
cd ~/zowe/extenders/sample-node-api
|
||||||
## start on z/os uss
|
## start on z/os uss
|
||||||
@@ -8,9 +8,8 @@ KEYSTORE_ALIAS=localhost
|
|||||||
KEYSTORE_PREFIX="${KEYSTORE_DIRECTORY}/${KEYSTORE_ALIAS}/${KEYSTORE_ALIAS}.keystore"
|
KEYSTORE_PREFIX="${KEYSTORE_DIRECTORY}/${KEYSTORE_ALIAS}/${KEYSTORE_ALIAS}.keystore"
|
||||||
KEYSTORE_KEY=${KEYSTORE_PREFIX}.key
|
KEYSTORE_KEY=${KEYSTORE_PREFIX}.key
|
||||||
KEYSTORE_CERTIFICATE=${KEYSTORE_PREFIX}.cer-ebcdic
|
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
|
# register with API ML layer
|
||||||
cd ~/zowe/extenders/sample-node-api
|
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.yml > ~/zowe/instance/workspace/api-mediation/api-defs/sample-node-api.yml
|
||||||
iconv -f IBM-1047 -t IBM-850 sample-node-api.http.yml > ~/zowe/instance/workspace/api-mediation/api-defs/sample-node-api.yml
|
|
||||||
Reference in New Issue
Block a user