Skip to content

Commit

Permalink
Merge pull request #4 from CBIIT/Bento-1822
Browse files Browse the repository at this point in the history
BENTO-1872, BENTO-1822
  • Loading branch information
n2iw authored Jun 22, 2022
2 parents 01bc86a + e410e41 commit d4685c0
Show file tree
Hide file tree
Showing 12 changed files with 455 additions and 15 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ sessions/
.idea/
logs/
yaml/
newrelic_agent.log
newrelic_agent.log
.env
15 changes: 2 additions & 13 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ const graphql = require("./data-management/init-graphql");
var createError = require('http-errors');
var express = require('express');
var path = require('path');
var session = require('express-session');
var FileStore = require('session-file-store')(session);
const {createSession} = require("./services/session");
var logger = require('morgan');
const fs = require('fs');
const cors = require('cors');
Expand All @@ -21,29 +20,19 @@ if (!fs.existsSync(LOG_FOLDER)) {
const accessLogStream = fs.createWriteStream(path.join(__dirname, LOG_FOLDER, 'access.log'), { flags: 'a'})

var authRouter = require('./routes/auth');

var app = express();
app.use(cors());

// setup the logger
app.use(logger('combined', { stream: accessLogStream }))
app.use(express.json());
app.use(express.urlencoded({ extended: false }));


var fileStoreOptions = {ttl: config.session_timeout, reapInterval: 10};

app.use(session({
secret: config.cookie_secret,
rolling: true,
store: new FileStore(fileStoreOptions),
}));
app.use(createSession({ session_timeout: config.session_timeout }));

app.use(express.static(path.join(__dirname, 'public')));

app.use('/api/auth', authRouter);
app.use('/api/auth/graphql', graphql);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
next(createError(404));
Expand Down
20 changes: 20 additions & 0 deletions config.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
COOKIE_SECRET=XXXXX
SESSION_TIMEOUT=1200
VERSION=1.0
DATE=2022.05.19
# MySQL Configuration
MY_SQL_HOST=127.0.0.1
MY_SQL_PORT=3306
MY_SQL_PASSWORD=Dbtldud1
MY_SQL_USER=root
MY_SQL_DATABASE=session
# Email Notification Config
EMAIL_SERVICE_EMAIL=XXXX@nih.gov
EMAIL_SMTP_HOST=mailfwd.nih.gov
EMAIL_SMTP_PORT=25
# If Sent From AWS SMTP
#EMAIL_USER=XXXX
#EMAIL_PASSWORD=XXXX
NEO4J_URI=bolt://localhost:XXXX
NEO4J_USER=neo4j
NEO4J_PASSWORD=xxxxx
28 changes: 27 additions & 1 deletion config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const fs = require('fs');
const dotenv = require('dotenv')
dotenv.config();

const config = {
version: process.env.VERSION,
Expand All @@ -20,8 +21,33 @@ const config = {
DATA_FILE: process.env.DATA_FILE,
//Testing
TEST_EMAIL: process.env.TEST_EMAIL,
// MySQL Session
mysql_host: process.env.MY_SQL_HOST,
mysql_port: process.env.MY_SQL_PORT,
mysql_user: process.env.MY_SQL_USER,
mysql_password: process.env.MY_SQL_PASSWORD,
mysql_database: process.env.MY_SQL_DATABASE,
// Email settings
email_service_email: process.env.EMAIL_SERVICE_EMAIL,
email_transport: getTransportConfig()
};

function getTransportConfig() {
return {
host: process.env.EMAIL_SMTP_HOST,
port: process.env.EMAIL_SMTP_PORT,
// Optional AWS Email Identity
...(process.env.EMAIL_USER && {
secure: true, // true for 465, false for other ports
auth: {
user: process.env.EMAIL_USER, // generated ethereal user
pass: process.env.EMAIL_PASSWORD, // generated ethereal password
}
}
)
};
}

if (!config.version) {
config.version = 'Version not set'
}
Expand Down
11 changes: 11 additions & 0 deletions lib/create-email-template.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const fsp = require('fs/promises');
const path = require('path');
const { template } = require('lodash');

async function createEmailTemplate(templateName, params, basePath = 'templates') {
const templatePath = path.resolve(basePath, templateName);
const templateSource = await fsp.readFile(templatePath, "utf-8");
return template(templateSource)(params);
}

module.exports = {createEmailTemplate}
35 changes: 35 additions & 0 deletions mailer/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"use strict";
const nodemailer = require("nodemailer");
const config = require("../config");
const {sendNotification} = require("../services/notify");

module.exports = {
/*
Sends an email to the provided recipient
Arguments:
- recipient {array of strings} -- recipients of the email
- subject {string} -- The email's subject
- contents {string} -- The email's contents
*/
sendEmail: async (recipient, subject, contents) => {
// create reusable transporter object using the default SMTP transport
let info = await sendNotification({
from: config.email_service_email,
to: recipient,
// cc: [],
// bcc: [],
subject: subject,
html: contents,
});

console.log("Message sent: %s", info.messageId);
// Message sent: <b658f8ca-6296-ccf4-8306-87d57a0b4321@example.com>

// Preview only available when sending through an Ethereal account
console.log("Preview URL: %s", nodemailer.getTestMessageUrl(info));
// Preview URL: https://ethereal.email/message/WaQKMgKddxQDoou...
}
};
13 changes: 13 additions & 0 deletions middleware/middleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function withAsync(fn) {
return async (request, response, next) => {
try {
return await fn(request, response, next);
} catch (error) {
next(error);
}
};
}

module.exports = {
withAsync
};
Loading

0 comments on commit d4685c0

Please sign in to comment.