From 76321adfe202fbf98268e82fec0df7cb08e5a305 Mon Sep 17 00:00:00 2001 From: Nestor Morales Date: Thu, 23 Jan 2020 00:49:22 -0500 Subject: [PATCH 1/3] Modularize routes --- config/pm2.config.json | 10 ++--- package.json | 2 +- src/controller/IdController.js | 2 +- src/controller/cna.controller/index.js | 0 .../cve.controller/cve.controller.js} | 24 +++++----- src/controller/cve.controller/index.js | 9 ++++ src/controller/test.controller/hello-world.js | 18 ++++++++ src/controller/test.controller/index.js | 9 ++++ src/index.js | 44 +++++++++---------- src/model/cna.js | 2 +- src/model/cve-id.js | 2 +- src/model/cve.js | 2 +- src/model/user.js | 2 +- src/routes.config.js | 12 +++++ 14 files changed, 92 insertions(+), 46 deletions(-) create mode 100644 src/controller/cna.controller/index.js rename src/{routes.js => controller/cve.controller/cve.controller.js} (66%) create mode 100644 src/controller/cve.controller/index.js create mode 100644 src/controller/test.controller/hello-world.js create mode 100644 src/controller/test.controller/index.js create mode 100644 src/routes.config.js diff --git a/config/pm2.config.json b/config/pm2.config.json index 6c54180b1..c0bbae29c 100644 --- a/config/pm2.config.json +++ b/config/pm2.config.json @@ -1,6 +1,6 @@ -{ - "name": "CVE-API", - "script": "./src/index.js", - "instances": 0, +{ + "name": "cve-services", + "script": "./src/index.js", + "instances": 0, "exec_mode": "cluster" -} +} \ No newline at end of file diff --git a/package.json b/package.json index 13e1da80a..2802760a5 100644 --- a/package.json +++ b/package.json @@ -47,6 +47,6 @@ "scripts": { "docs": "./node_modules/apidoc/bin/apidoc -i src/ -o apidoc/", "lint": "node node_modules/eslint/bin/eslint.js src/ --fix", - "start:dev": "./node_modules/pm2/bin/pm2 start ./src/config/pm2.config.json --env dev && ./node_modules/pm2/bin/pm2 logs" + "start:dev": "./node_modules/pm2/bin/pm2-dev start ./config/pm2.config.json --env dev && ./node_modules/pm2/bin/pm2 logs" } } diff --git a/src/controller/IdController.js b/src/controller/IdController.js index c82cb9abb..c1112e604 100644 --- a/src/controller/IdController.js +++ b/src/controller/IdController.js @@ -32,7 +32,7 @@ const uuidv4 = require('uuid/v4'); */ const allocateCveId = (request, response) => { // create cve id - var cveid = new cveId.CveId({id: 'CVE-' + Date().getFullYear().toString() + '-' + uuidv4()}); + const cveid = new cveId.CveId({id: 'CVE-' + Date().getFullYear().toString() + '-' + uuidv4()}); // save in mongo cveid.save(function (err, cveid) { if (err) { diff --git a/src/controller/cna.controller/index.js b/src/controller/cna.controller/index.js new file mode 100644 index 000000000..e69de29bb diff --git a/src/routes.js b/src/controller/cve.controller/cve.controller.js similarity index 66% rename from src/routes.js rename to src/controller/cve.controller/cve.controller.js index 30d76fa26..d6baf80f2 100644 --- a/src/routes.js +++ b/src/controller/cve.controller/cve.controller.js @@ -1,26 +1,23 @@ -var express = require('express'); -var router = express.Router() -var middleware = require('./middleware/middleware') -var cve_controller = require('./controller/CveController') -//var id_controller = require('./controller/IdController') +const middleware = require('../../middleware/middleware') +const cve_controller = require('../CveController') -router.get('/cve-id', function (req, res) { +async function allocateCveId(req, res) { //id_controller.allocateCveId(req, res) res.send("NEW-CVE-ID"); -}) +} -router.post('/cve', function (req, res) { +async function cveSubmission(req, res) { // Check API secret to make sure it's valid and avoid open requests let cna = req.header('X-API-CNA') let author = req.header('X-API-AUTHOR') let secret = req.header('X-API-SECRET') - var error = middleware.validApiSecret(cna, author, secret) + let error = middleware.validApiSecret(cna, author, secret) if (error) { return error } // TODO: implement any additional steps needed to make sure it's safe to parse the JSON // Validate CVE JSON against JSON schema to avoid bad data - var error = middleware.validCveJSON(req.body) + let error = middleware.validCveJSON(req.body) if (error) { return error } @@ -33,6 +30,9 @@ router.post('/cve', function (req, res) { // Pass CVE submission to CVE controller and therefore CPS cve_controller.submitCve(cna, cve) res.send("NEW-CVE-SUBMITTED") -}) +} -module.exports = router; \ No newline at end of file +module.exports = { + CVE_ID_ALLOCATION: allocateCveId, + CVE_SUBMISSION: cveSubmission +} \ No newline at end of file diff --git a/src/controller/cve.controller/index.js b/src/controller/cve.controller/index.js new file mode 100644 index 000000000..8457c4f93 --- /dev/null +++ b/src/controller/cve.controller/index.js @@ -0,0 +1,9 @@ + +const express = require('express'); +const router = express.Router() +const controller = require('./cve.controller') + +router.post('/cve', controller.CVE_SUBMISSION); +router.get('/cve-id', controller.CVE_ID_ALLOCATION); + +module.exports = router; \ No newline at end of file diff --git a/src/controller/test.controller/hello-world.js b/src/controller/test.controller/hello-world.js new file mode 100644 index 000000000..aad854b29 --- /dev/null +++ b/src/controller/test.controller/hello-world.js @@ -0,0 +1,18 @@ + +async function helloWorld(req, res) { + console.log('Hello world, called.'); + res.status(200).json({ + message: 'Hello world! Api works!' + }); +} + +async function helloWorld2(req, res) { + console.log('Hello world 2, called.'); + res.status(200).json({ + message: 'Hello world 2! Api works!' + }); +} +module.exports = { + hello : helloWorld, + hello2 : helloWorld2 +} \ No newline at end of file diff --git a/src/controller/test.controller/index.js b/src/controller/test.controller/index.js new file mode 100644 index 000000000..d53d3e28d --- /dev/null +++ b/src/controller/test.controller/index.js @@ -0,0 +1,9 @@ + +const express = require('express'); +const router = express.Router() +const helloWorld = require('./hello-world') + +router.post('/hello', helloWorld.hello); +router.get('/hello2', helloWorld.hello2); + +module.exports = router; \ No newline at end of file diff --git a/src/index.js b/src/index.js index 5592599b5..d38c4017c 100644 --- a/src/index.js +++ b/src/index.js @@ -5,24 +5,19 @@ const cors = require('cors'); const morgan = require('morgan'); const mongoose = require('mongoose'); const helmet = require('helmet'); -const routes = require('./routes'); - +const dotenv = require('dotenv'); +const configureRoutes = require('./routes.config'); const app = express(); +const configed = dotenv.config(); //This enables us to read from the .env file. app.use(morgan('combined')); app.use(bodyParser.json()); //parses incoming json app.use(cors()); app.use(helmet()); //gives standard security -app.use('/', routes); +configureRoutes(app) // Make mongoose connection available globally global.mongoose = mongoose; -//import 'reflect-metadata'; -//import { configureRoutes } from './routes'; -//import * as dotenv from 'dotenv'; -const dotenv = require('dotenv'); -//import { verifyRequester } from './middlewares/verification'; -//import { RESPONSE_ERROR_MESSAGE } from './constants'; -const configed = dotenv.config(); //This enables us to read from the .env file. + //*********Middleware Config*********** */ //if (process.env.NODE_ENV === 'dev') app.use(logger('dev')); //creates logs when NODE ENV is set to dev @@ -34,18 +29,21 @@ const configed = dotenv.config(); //This enables us to read from the .env file. app.use(express.static('client-dist')) app.use('/*', express.static('client-dist')) - -const dbConnectionStr = (config.has('database.username') && config.has('database.userpass')) ? - `mongodb://${config.get('database.username')}:${config.get('database.userpass')}@${config.get('database.host')}:${config.get('database.port')}/${config.get('database.name')}`: - `mongodb://${config.get('database.host')}:${config.get('database.port')}/${config.get('database.name')}`; - -mongoose.connect(dbConnectionStr, {useNewUrlParser: true, useUnifiedTopology: true }); -var db = mongoose.connection; -db.on('error', console.error.bind(console, 'connection error:')); -db.once('open', function() { - // we're connected! - console.log(`Successfully connected to database ${config.get('database.name')} at ${config.get('database.host')}:${config.get('database.port')}`); - var port = process.env.PORT || config.get('port'); +var port = process.env.PORT || config.get('port'); app.listen(port); console.log('Serving on port ' + port) -}); \ No newline at end of file + +// const dbConnectionStr = (config.has('database.username') && config.has('database.userpass')) ? +// `mongodb://${config.get('database.username')}:${config.get('database.userpass')}@${config.get('database.host')}:${config.get('database.port')}/${config.get('database.name')}`: +// `mongodb://${config.get('database.host')}:${config.get('database.port')}/${config.get('database.name')}`; + +// mongoose.connect(dbConnectionStr, {useNewUrlParser: true, useUnifiedTopology: true }); +// var db = mongoose.connection; +// db.on('error', console.error.bind(console, 'connection error:')); +// db.once('open', function() { +// // we're connected! +// console.log(`Successfully connected to database ${config.get('database.name')} at ${config.get('database.host')}:${config.get('database.port')}`); +// var port = process.env.PORT || config.get('port'); +// app.listen(port); +// console.log('Serving on port ' + port) +// }); \ No newline at end of file diff --git a/src/model/cna.js b/src/model/cna.js index a8be8ff5e..e52176846 100644 --- a/src/model/cna.js +++ b/src/model/cna.js @@ -1,4 +1,4 @@ -import { mongoose } from "mongoose" +const mongoose = require('mongoose'); var CnaSchema = new mongoose.Schema({ id: String, diff --git a/src/model/cve-id.js b/src/model/cve-id.js index 7e1e7da7e..74300d6d6 100644 --- a/src/model/cve-id.js +++ b/src/model/cve-id.js @@ -1,4 +1,4 @@ -import { mongoose } from "mongoose" +const mongoose = require('mongoose'); var CveIdSchema = new mongoose.Schema({ id: String diff --git a/src/model/cve.js b/src/model/cve.js index 6b20b0b51..90926d9e3 100644 --- a/src/model/cve.js +++ b/src/model/cve.js @@ -1,4 +1,4 @@ -import { mongoose } from "mongoose" +const mongoose = require('mongoose'); var CveSchema = new mongoose.Schema({ id: String diff --git a/src/model/user.js b/src/model/user.js index c0e33d421..11277c56e 100644 --- a/src/model/user.js +++ b/src/model/user.js @@ -1,4 +1,4 @@ -import { mongoose } from "mongoose" +const mongoose = require('mongoose'); var UserSchema = new mongoose.Schema({ id: String, diff --git a/src/routes.config.js b/src/routes.config.js new file mode 100644 index 000000000..731e58fef --- /dev/null +++ b/src/routes.config.js @@ -0,0 +1,12 @@ + +const TestRoutes = require('./controller/test.controller') +const Cve_Controller = require('./controller/cve.controller') + +module.exports = async function configureRoutes(app) { + //Default test route. + app.use('/test', TestRoutes); + + //All controler routes go here + app.use('/api', Cve_Controller); + +} \ No newline at end of file From 80ce3bcc4b13f7a2174891fb260ae195249825e5 Mon Sep 17 00:00:00 2001 From: Nestor Morales Date: Thu, 23 Jan 2020 00:55:47 -0500 Subject: [PATCH 2/3] Lint code --- config/pm2.config.json | 6 +++--- src/index.js | 32 +++++++++++++++----------------- 2 files changed, 18 insertions(+), 20 deletions(-) diff --git a/config/pm2.config.json b/config/pm2.config.json index c0bbae29c..d6956d522 100644 --- a/config/pm2.config.json +++ b/config/pm2.config.json @@ -1,6 +1,6 @@ { - "name": "cve-services", - "script": "./src/index.js", - "instances": 0, + "name": "cve-services", + "script": "./src/index.js", + "instances": 0, "exec_mode": "cluster" } \ No newline at end of file diff --git a/src/index.js b/src/index.js index d38c4017c..c0543aa95 100644 --- a/src/index.js +++ b/src/index.js @@ -14,6 +14,7 @@ app.use(morgan('combined')); app.use(bodyParser.json()); //parses incoming json app.use(cors()); app.use(helmet()); //gives standard security +//app.use('/', routes); configureRoutes(app) // Make mongoose connection available globally global.mongoose = mongoose; @@ -28,22 +29,19 @@ global.mongoose = mongoose; //configureRoutes(app); //configure app routes app.use(express.static('client-dist')) -app.use('/*', express.static('client-dist')) -var port = process.env.PORT || config.get('port'); - app.listen(port); - console.log('Serving on port ' + port) +app.use('/*', express.static('client-dist')) -// const dbConnectionStr = (config.has('database.username') && config.has('database.userpass')) ? -// `mongodb://${config.get('database.username')}:${config.get('database.userpass')}@${config.get('database.host')}:${config.get('database.port')}/${config.get('database.name')}`: -// `mongodb://${config.get('database.host')}:${config.get('database.port')}/${config.get('database.name')}`; +const dbConnectionStr = (config.has('database.username') && config.has('database.userpass')) ? + `mongodb://${config.get('database.username')}:${config.get('database.userpass')}@${config.get('database.host')}:${config.get('database.port')}/${config.get('database.name')}`: + `mongodb://${config.get('database.host')}:${config.get('database.port')}/${config.get('database.name')}`; -// mongoose.connect(dbConnectionStr, {useNewUrlParser: true, useUnifiedTopology: true }); -// var db = mongoose.connection; -// db.on('error', console.error.bind(console, 'connection error:')); -// db.once('open', function() { -// // we're connected! -// console.log(`Successfully connected to database ${config.get('database.name')} at ${config.get('database.host')}:${config.get('database.port')}`); -// var port = process.env.PORT || config.get('port'); -// app.listen(port); -// console.log('Serving on port ' + port) -// }); \ No newline at end of file +mongoose.connect(dbConnectionStr, {useNewUrlParser: true, useUnifiedTopology: true }); +var db = mongoose.connection; +db.on('error', console.error.bind(console, 'connection error:')); +db.once('open', function() { + // we're connected! + console.log(`Successfully connected to database ${config.get('database.name')} at ${config.get('database.host')}:${config.get('database.port')}`); + var port = process.env.PORT || config.get('port'); + app.listen(port); + console.log('Serving on port ' + port) +}); \ No newline at end of file From df8166ac7a6d8de01d2b7920bca596bc41921675 Mon Sep 17 00:00:00 2001 From: Nestor Morales Date: Thu, 23 Jan 2020 01:12:10 -0500 Subject: [PATCH 3/3] Edit comment in route.config --- src/routes.config.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/routes.config.js b/src/routes.config.js index 731e58fef..cb320fc40 100644 --- a/src/routes.config.js +++ b/src/routes.config.js @@ -3,10 +3,9 @@ const TestRoutes = require('./controller/test.controller') const Cve_Controller = require('./controller/cve.controller') module.exports = async function configureRoutes(app) { - //Default test route. + //localhost:3000/test/hello + //localhost:3000/test/hello2 app.use('/test', TestRoutes); - - //All controler routes go here app.use('/api', Cve_Controller); } \ No newline at end of file