Skip to content

upgrade node and coding style #52

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Sep 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .eslintrc

This file was deleted.

9 changes: 9 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = {
"parserOptions": {
"ecmaVersion": 12,
"env": {
"node": true,
"es2021": true
}
}
}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
.env
log.txt
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
# and runs it against the specified Topcoder backend (development or
# production) when container is executed.

FROM node:8.2.1
LABEL app="tc email" version="1.1"
FROM node:16.17
LABEL app="tc email" version="2.0"

WORKDIR /opt/app
COPY . .
Expand Down
5 changes: 3 additions & 2 deletions config/default.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* The configuration file.
*/
require('dotenv').config()
module.exports = {
LOG_LEVEL: process.env.LOG_LEVEL,
PORT: process.env.PORT,
Expand Down Expand Up @@ -34,15 +35,15 @@ module.exports = {
TEMPLATE_MAP: process.env.TEMPLATE_MAP,
SENDGRID_API_KEY: process.env.SENDGRID_API_KEY || '',
EMAIL_FROM: process.env.EMAIL_FROM || 'no-reply@topcoder.com',

// temporary not in use feature
EMAIL_MAX_ERRORS: process.env.EMAIL_MAX_ERRORS || 2,
EMAIL_PAUSE_TIME: process.env.EMAIL_PAUSE_TIME || 30,

//in every 2 minutes will retry for failed status
EMAIL_RETRY_SCHEDULE: process.env.EMAIL_RETRY_SCHEDULE || '0 */2 * * * *',
//wont't retry failed emails older than this time (msec)
EMAIL_RETRY_MAX_AGE: process.env.EMAIL_RETRY_MAX_AGE || 1000*60*60*24,
EMAIL_RETRY_MAX_AGE: process.env.EMAIL_RETRY_MAX_AGE || 1000 * 60 * 60 * 24,

API_CONTEXT_PATH: process.env.API_CONTEXT_PATH || '/v5/email',

Expand Down
38 changes: 20 additions & 18 deletions connect/connectEmailServer.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
/**
* This is TopCoder connect email server.
*/
'use strict';

global.Promise = require('bluebird');

const _ = require('lodash');
const config = require('config');
const emailServer = require('../index');
Expand All @@ -13,27 +9,27 @@ const logger = require('../src/common/logger');

// set configuration for the server, see ../config/default.js for available config parameters
// setConfig should be called before initDatabase and start functions
emailServer.setConfig({ LOG_LEVEL: 'debug' });
emailServer.setConfig({ LOG_LEVEL: config.LOG_LEVEL });

// add topic handlers,
// handler is used build a notification list for a message of a topic,
// it is defined as: function(topic, message, callback),
// it is defined as: function(topic, message),
// the topic is topic name,
// the message is JSON event message,
// the callback is function(error, templateId), where templateId is the used SendGrid template id
const handler = (topic, message, callback) => {
const handler = async (topic, message) => {
let templateId = config.TEMPLATE_MAP[topic];
templateId = _.get(message, config.PAYLOAD_SENDGRID_TEMPLATE_KEY, templateId);
if (!templateId) {
return callback(null, { success: false, error: `Template not found for topic ${topic}` });
return { success: false, error: `Template not found for topic ${topic}` };
}

service.sendEmail(templateId, message).then(() => {
callback(null, { success: true });
}).catch((err) => {
logger.error("Error occurred in sendgrid api calling:", err);
callback(null, { success: false, error: err });
});
try {
await service.sendEmail(templateId, message)
return { success: true };
} catch (err) {
logger.error("Error occurred in sendgrid api calling:", err);
return { success: false, error: err };
}

};

Expand All @@ -45,8 +41,14 @@ _.keys(config.TEMPLATE_MAP).forEach((eventType) => {
// init database, it will clear and re-create all tables
emailServer
.initDatabase()
.then(() => emailServer.start())
.catch((e) => console.log(e)); // eslint-disable-line no-console
.then(() => {
logger.info('Database initialized successfully.')
emailServer.start()
})
.catch((e) => {
logger.error('Error occurred in starting email server:', e);
process.exit(1);
}); // eslint-disable-line no-console

// if no need to init database, then directly start the server:
// emailServer.start();
// emailServer.start()
11 changes: 8 additions & 3 deletions connect/service.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/**
* This is TopCoder connect email service.
*/
'use strict';

const sgMail = require('@sendgrid/mail');
const config = require('config');
Expand All @@ -10,7 +9,7 @@ const logger = require('../src/common/logger');
// set api key for SendGrid email client
sgMail.setApiKey(config.SENDGRID_API_KEY);

const sendEmail = (templateId, message) => { // send email
const sendEmail = async (templateId, message) => { // send email

let msg = {}
const from = message.from ? message.from : config.EMAIL_FROM;
Expand Down Expand Up @@ -52,7 +51,13 @@ const sendEmail = (templateId, message) => { // send email
};
}
logger.info(`Sending email with templateId: ${templateId} and message: ${JSON.stringify(msg)}`);
return sgMail.send(msg)
try {
const result = await sgMail.send(msg)
return result
} catch (err) {
logger.error(`Error occurred in sendgrid api calling: ${err}`);
throw err
}
}
module.exports = {
sendEmail,
Expand Down
7 changes: 3 additions & 4 deletions docs/swagger_api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ info:
description: "TOPCODER EMAIL SERIES - EMAIL SERVER"
version: "1.0.0"
host: "localhost:6100"
basePath : "/v5/email"
basePath: "/v5/email"
schemes:
- "https"
- "https"
securityDefinitions:
jwt:
type: apiKey
Expand All @@ -17,8 +17,7 @@ securityDefinitions:
paths:
/health:
get:
description:
health check endpoint
description: health check endpoint
produces:
- application/json
responses:
Expand Down
142 changes: 110 additions & 32 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
/**
* This is entry point of the TopCoder notification server module.
*/
'use strict';

require('dotenv').config()
const config = require('config');
const jwtAuth = require('tc-core-library-js').middleware.jwtAuthenticator;
const express = require('express');
const _ = require('lodash');
const cors = require('cors');
const bodyParser = require('body-parser');
const schedule = require('node-schedule');
const helper = require('./src/common/helper');
const logger = require('./src/common/logger');
const errors = require('./src/common/errors');
const models = require('./src/models');
const { initServer, retryEmail } = require('./src/init');

config.TEMPLATE_MAP = JSON.parse(config.TEMPLATE_MAP);

Expand All @@ -34,21 +37,6 @@ function setConfig(cfg) {
_.extend(config, cfg);
}

/**
* Add topic handler for topic, override existing one if any.
* @param {String} topic the topic name
* @param {Function} handler the handler
*/
function addTopicHandler(topic, handler) {
if (!topic) {
throw new errors.ValidationError('Missing topic.');
}
if (!handler) {
throw new errors.ValidationError('Missing handler.');
}
handlers[topic] = handler;
}

/**
* Remove topic handler for topic.
* @param {String} topic the topic name
Expand All @@ -69,28 +57,118 @@ function getAllHandlers() {
}

/**
* Start the notification server.
* Add topic handler for topic, override existing one if any.
* @param {String} topic the topic name
* @param {Function} handler the handler
*/
function start() {
if (_.isEmpty(handlers)) {
throw new errors.ValidationError('Missing handler(s).');
function addTopicHandler(topic, handler) {
if (!topic) {
throw new errors.ValidationError('Missing topic.');
}
if (!handler) {
throw new errors.ValidationError('Missing handler.');
}
// load app only after config is set
const app = require('./src/app');
schedule.scheduleJob(config.EMAIL_RETRY_SCHEDULE, function() {
app.retryEmail(handlers).catch((err) => logger.error(err));
handlers[topic] = handler;
}

const app = express();
app.set('port', config.PORT);

app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

const apiRouter = express.Router();

// load all routes
_.each(require('./src/routes'), (verbs, url) => {
_.each(verbs, (def, verb) => {
const actions = [];
const method = require('./src/controllers/' + def.controller)[def.method];
if (!method) {
throw new Error(def.method + ' is undefined');
}
actions.push((req, res, next) => {
req.signature = `${def.controller}#${def.method}`;
next();
});
if (url !== '/health') {
actions.push(jwtAuth());
actions.push((req, res, next) => {
if (!req.authUser) {
return next(new errors.UnauthorizedError('Authorization failed.'));
}
req.user = req.authUser;
return next();
});
}
actions.push(method);
apiRouter[verb](url, helper.autoWrapExpress(actions));
});
return app.start(handlers).catch((err) => logger.error(err));
});

app.use(config.API_CONTEXT_PATH, apiRouter);

app.use((req, res) => {
res.status(404).json({ error: 'route not found' });
});

app.use((err, req, res) => { // eslint-disable-line
logger.logFullError(err, req.signature);
let status = err.httpStatus || 500;
if (err.isJoi) {
status = 400;
}
// from express-jwt
if (err.name === 'UnauthorizedError') {
status = 401;
}
res.status(status);
if (err.isJoi) {
res.json({
error: 'Validation failed',
details: err.details,
});
} else {
res.json({
error: err.message,
});
}
});


function start() {

initServer(handlers).then(() => {
if (_.isEmpty(handlers)) {
throw new errors.ValidationError('Missing handler(s).');
}

schedule.scheduleJob(config.EMAIL_RETRY_SCHEDULE, async function () {
try {
await retryEmail(handlers)
} catch (err) {
console.log(err);
logger.error(err);
}
});
app.listen(app.get('port'), () => {
logger.info(`Express server listening on port ${app.get('port')}`);
});
}).catch((err) => {
logger.error(err);
process.exit(1);
})
}

/**
* Initialize database. All tables are cleared and re-created.
* @returns {Promise} promise to init db
*/
function initDatabase() {
async function initDatabase() {
// load models only after config is set
const models = require('./src/models');
return models.init(true);
logger.info('Initializing database...');
await models.init(true);
}

// Exports
Expand Down
Loading