diff --git a/package.json b/package.json index 177d66d..2672031 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,7 @@ "description": "", "main": "index.js", "scripts": { + "dev": "nodemon server/app.js --service sample-node-api --port 8080 --key sslcert/server.key --cert sslcert/server.cert", "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" diff --git a/server/app.js b/server/app.js index 0490ed3..57cb5fa 100644 --- a/server/app.js +++ b/server/app.js @@ -11,158 +11,24 @@ 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 {buildConfigFromParams} = require('./utils/params'); +let config = buildConfigFromParams(); +const {https:{key, cert}, port} = 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}`); +httpServer.listen(port); +httpsServer.listen(port+1); +console.log(`http server listening at port ${port}`); +console.log(`https server listening at port ${port + 1}`); module.exports = { app }; \ No newline at end of file diff --git a/server/routes/index.route.js b/server/routes/index.route.js index 964b3dd..7d2b2e4 100644 --- a/server/routes/index.route.js +++ b/server/routes/index.route.js @@ -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('Hello World!')); +router.get('/health', (req, res) => { + const healthcheck = { + uptime: process.uptime(), + message: 'OK', + timestamp: Date.now() + }; + res.send(JSON.stringify(healthcheck)); +}); module.exports = router; \ No newline at end of file diff --git a/server/routes/swagger.route.js b/server/routes/swagger.route.js new file mode 100644 index 0000000..a98700e --- /dev/null +++ b/server/routes/swagger.route.js @@ -0,0 +1,20 @@ +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; + \ No newline at end of file diff --git a/server/swagger.json b/server/swagger.json index 5e3f88a..7b4a2da 100644 --- a/server/swagger.json +++ b/server/swagger.json @@ -6,7 +6,7 @@ ,"host":"localhost:8080" ,"basePath":"/" ,"tags":[{"name":"Accounts","description":"Accounts API"},{"name":"Cars","description":"Cars API"}] - ,"schemes":["http"] + ,"schemes":["http","https"] ,"produces": ["application/json"], "paths": { "/accounts": { diff --git a/server/utils/params.js b/server/utils/params.js new file mode 100644 index 0000000..bc2ea26 --- /dev/null +++ b/server/utils/params.js @@ -0,0 +1,114 @@ +const fs = require('fs'); +const path = require("path"); + +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; +} + +function buildConfigFromParams() { + + 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); + return config; +} + + +module.exports = { + buildConfigFromParams +}; \ No newline at end of file