Skip to content

Commit

Permalink
Merge pull request #14 from rotsen91/modularize
Browse files Browse the repository at this point in the history
Modularize Routes File
  • Loading branch information
mattrbianchi authored Jan 23, 2020
2 parents d2bc4f9 + df8166a commit e25feea
Show file tree
Hide file tree
Showing 14 changed files with 75 additions and 32 deletions.
6 changes: 3 additions & 3 deletions config/pm2.config.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "CVE-API",
{
"name": "cve-services",
"script": "./src/index.js",
"instances": 0,
"exec_mode": "cluster"
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
2 changes: 1 addition & 1 deletion src/controller/IdController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Empty file.
24 changes: 12 additions & 12 deletions src/routes.js → ...ntroller/cve.controller/cve.controller.js
Original file line number Diff line number Diff line change
@@ -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
}
Expand All @@ -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;
module.exports = {
CVE_ID_ALLOCATION: allocateCveId,
CVE_SUBMISSION: cveSubmission
}
9 changes: 9 additions & 0 deletions src/controller/cve.controller/index.js
Original file line number Diff line number Diff line change
@@ -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;
18 changes: 18 additions & 0 deletions src/controller/test.controller/hello-world.js
Original file line number Diff line number Diff line change
@@ -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
}
9 changes: 9 additions & 0 deletions src/controller/test.controller/index.js
Original file line number Diff line number Diff line change
@@ -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;
18 changes: 7 additions & 11 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,20 @@ 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 <https://www.npmjs.com/package/helmet>
app.use('/', routes);
//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
Expand All @@ -33,7 +29,7 @@ const configed = dotenv.config(); //This enables us to read from the .env file.
//configureRoutes(app); //configure app routes

app.use(express.static('client-dist'))
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')}`:
Expand Down
2 changes: 1 addition & 1 deletion src/model/cna.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { mongoose } from "mongoose"
const mongoose = require('mongoose');

var CnaSchema = new mongoose.Schema({
id: String,
Expand Down
2 changes: 1 addition & 1 deletion src/model/cve-id.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { mongoose } from "mongoose"
const mongoose = require('mongoose');

var CveIdSchema = new mongoose.Schema({
id: String
Expand Down
2 changes: 1 addition & 1 deletion src/model/cve.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { mongoose } from "mongoose"
const mongoose = require('mongoose');

var CveSchema = new mongoose.Schema({
id: String
Expand Down
2 changes: 1 addition & 1 deletion src/model/user.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { mongoose } from "mongoose"
const mongoose = require('mongoose');

var UserSchema = new mongoose.Schema({
id: String,
Expand Down
11 changes: 11 additions & 0 deletions src/routes.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

const TestRoutes = require('./controller/test.controller')
const Cve_Controller = require('./controller/cve.controller')

module.exports = async function configureRoutes(app) {
//localhost:3000/test/hello
//localhost:3000/test/hello2
app.use('/test', TestRoutes);
app.use('/api', Cve_Controller);

}

0 comments on commit e25feea

Please sign in to comment.