diff --git a/package.json b/package.json index 41a4fe1..5a68187 100644 --- a/package.json +++ b/package.json @@ -1,11 +1,11 @@ { - "name": "sample-api", + "name": "sample-node-api", "version": "1.0.0", "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", + "dev": "nodemon src/index.js", + "start": "node 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", "clean": "rimraf dist && mkdirp dist" }, diff --git a/server/utils/params.js b/src/config.js similarity index 50% rename from server/utils/params.js rename to src/config.js index bc2ea26..294b989 100644 --- a/server/utils/params.js +++ b/src/config.js @@ -4,21 +4,29 @@ 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, - } + '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', 'pfx'].forEach(key => { + ['key', 'cert'].forEach(key => { if (config.https[key]) { let file = config.https[key]; config.https[key] = fs.readFileSync(file); @@ -27,31 +35,23 @@ function loadCertificateFiles(config) { } return config; }; - + function validateParams (argv) { let isValid = true; - const serviceFor=argv.s; + const serviceName=argv.s; if((argv.p==='' || !argv.p) && isValid) { isValid = false; - process.stderr.write(`[${serviceFor}] port configuration is missing\n`); + process.stderr.write(`[${serviceName}] port configuration is missing\n`); } - if( (argv.k==='' && argv.c==='' && argv.x==='' && argv.w==='') && isValid) { + if( ( (argv.k==='' && argv.c>'') || (argv.k>'' && argv.c==='') ) && 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`); + process.stderr.write(`[${serviceName}] https configuration is missing\n`); } if(!isValid) { - process.stderr.write(`[${serviceFor}] is failed to start, error:\n`); + process.stderr.write(`[${serviceName}] is failed to start, error:\n`); process.exit(1); return false; } @@ -59,18 +59,18 @@ function validateParams (argv) { return true; } -function buildConfigFromParams() { - +function setUpParams() { var argv = require('yargs') .usage('Usage: $0 [options]') .option('s', { alias: 'service', description: 'service-for path', - default: '' + default: 'sample-node-api' }) .option('p', { alias: 'port', - description: 'listening port' + description: 'listening port', + default: 8080 }) .option('k', { alias: 'key', @@ -82,33 +82,26 @@ function buildConfigFromParams() { 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; + type: 'boolean' + }) + .help('h') + .alias('h', 'help') + .check(validateParams) + .argv; + return argv; +} + +function buildConfigFromParams() { + let argv = setUpParams(); let config = buildConfig(argv); - config = loadCertificateFiles(config); return config; } +const config = buildConfigFromParams(); -module.exports = { - buildConfigFromParams -}; \ No newline at end of file +module.exports = config; \ No newline at end of file diff --git a/server/config.json b/src/config.json similarity index 100% rename from server/config.json rename to src/config.json diff --git a/server/controllers/accounts.controller.js b/src/controllers/accounts.controller.js similarity index 100% rename from server/controllers/accounts.controller.js rename to src/controllers/accounts.controller.js diff --git a/server/controllers/accountsCars.controller.js b/src/controllers/accountsCars.controller.js similarity index 100% rename from server/controllers/accountsCars.controller.js rename to src/controllers/accountsCars.controller.js diff --git a/server/controllers/cars.controller.js b/src/controllers/cars.controller.js similarity index 100% rename from server/controllers/cars.controller.js rename to src/controllers/cars.controller.js diff --git a/data.js b/src/data.js similarity index 100% rename from data.js rename to src/data.js diff --git a/server/app.js b/src/index.js similarity index 56% rename from server/app.js rename to src/index.js index 57cb5fa..8ba0c85 100644 --- a/server/app.js +++ b/src/index.js @@ -13,22 +13,27 @@ const http = require('http'); const https = require('https'); const cors = require('cors'); -const {buildConfigFromParams} = require('./utils/params'); -let config = buildConfigFromParams(); -const {https:{key, cert}, port} = config; -const credentials = { key, cert}; +//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); -const httpsServer = https.createServer(credentials, app); - httpServer.listen(port); -httpsServer.listen(port+1); -console.log(`http server listening at port ${port}`); -console.log(`https server listening at port ${port + 1}`); +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 }; \ No newline at end of file diff --git a/server/routes/accounts.route.js b/src/routes/accounts.route.js similarity index 100% rename from server/routes/accounts.route.js rename to src/routes/accounts.route.js diff --git a/server/routes/accountsCars.route.js b/src/routes/accountsCars.route.js similarity index 100% rename from server/routes/accountsCars.route.js rename to src/routes/accountsCars.route.js diff --git a/server/routes/cars.route.js b/src/routes/cars.route.js similarity index 100% rename from server/routes/cars.route.js rename to src/routes/cars.route.js diff --git a/server/routes/index.route.js b/src/routes/index.route.js similarity index 100% rename from server/routes/index.route.js rename to src/routes/index.route.js diff --git a/server/routes/swagger.route.js b/src/routes/swagger.route.js similarity index 99% rename from server/routes/swagger.route.js rename to src/routes/swagger.route.js index a98700e..387b81f 100644 --- a/server/routes/swagger.route.js +++ b/src/routes/swagger.route.js @@ -1,7 +1,6 @@ const express = require('express'); const router = express.Router({ mergeParams: true }); - const swaggerUi = require('swagger-ui-express'); const swaggerDocument = require('../swagger.json'); diff --git a/server/services/accounts.service.js b/src/services/accounts.service.js similarity index 93% rename from server/services/accounts.service.js rename to src/services/accounts.service.js index c04bf3f..423fde1 100644 --- a/server/services/accounts.service.js +++ b/src/services/accounts.service.js @@ -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); diff --git a/server/services/accountsCars.service.js b/src/services/accountsCars.service.js similarity index 95% rename from server/services/accountsCars.service.js rename to src/services/accountsCars.service.js index 2a296dc..cc13b14 100644 --- a/server/services/accountsCars.service.js +++ b/src/services/accountsCars.service.js @@ -8,7 +8,7 @@ Copyright IBM Corporation 2020 */ -const data = require('../../data'); +const data = require('../data'); const carsService = require('./cars.service'); diff --git a/server/services/cars.service.js b/src/services/cars.service.js similarity index 92% rename from server/services/cars.service.js rename to src/services/cars.service.js index 04ef301..16d5ff7 100644 --- a/server/services/cars.service.js +++ b/src/services/cars.service.js @@ -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); diff --git a/server/swagger.json b/src/swagger.json similarity index 99% rename from server/swagger.json rename to src/swagger.json index 7b4a2da..5e3f88a 100644 --- a/server/swagger.json +++ b/src/swagger.json @@ -6,7 +6,7 @@ ,"host":"localhost:8080" ,"basePath":"/" ,"tags":[{"name":"Accounts","description":"Accounts API"},{"name":"Cars","description":"Cars API"}] - ,"schemes":["http","https"] + ,"schemes":["http"] ,"produces": ["application/json"], "paths": { "/accounts": {