Add cert with force ssl

enable slack notifications
tidy up rollbar a bit
This commit is contained in:
2017-02-10 17:13:25 +00:00
parent 14e7aeeb5c
commit 97d5370e65
5 changed files with 93 additions and 16 deletions

19
src/core/rollbar.js Normal file
View File

@@ -0,0 +1,19 @@
import Rollbar from 'rollbar';
import { analytics } from '../config';
const serverToken = analytics.rollbar.serverToken;
export default {
errorHandler() {
return Rollbar.errorHandler(serverToken);
},
handleError(err) {
return Rollbar.handleError(err);
},
init() {
Rollbar.init(serverToken);
Rollbar.handleUncaughtExceptionsAndRejections(serverToken, {
exitOnUncaughtException: true,
});
},
};

31
src/core/slack.js Normal file
View File

@@ -0,0 +1,31 @@
import Slack from 'slack-node';
import { slackWebhook } from '../config';
import { handleError } from '../core/rollbar';
const slack = new Slack();
slack.setWebhook(slackWebhook);
// Debounce sending the slack message to avoid spamming
const slackTimers = {};
export const sendSlackMsg = function sendSlackMsg(text) {
slack.webhook({
channel: '#wedding-rsvps',
username: 'weddingbot',
text,
}, err => {
if (err) {
handleError(err);
}
});
};
export const sendSlackMsgWithDebounce = function sendSlackMsgWithDebounce(text, key) {
if (key) {
// Stop previous msg
clearTimeout(slackTimers[key]);
slackTimers[key] = setTimeout(() => {
sendSlackMsg(text);
}, 10000);
}
};

View File

@@ -10,7 +10,6 @@ import React from 'react';
import ReactDOM from 'react-dom/server';
import UniversalRouter from 'universal-router';
import PrettyError from 'pretty-error';
import Rollbar from 'rollbar';
import App from './components/App';
import Html from './components/Html';
import { ErrorPageWithoutStyle } from './routes/error/ErrorPage';
@@ -21,12 +20,15 @@ import models from './data/models';
import schema from './data/schema';
import routes from './routes';
import assets from './assets'; // eslint-disable-line import/no-unresolved
import { port, auth, analytics } from './config';
import { port, auth, starters, mains } from './config';
import Person from './data/models/Person';
import Rollbar from './core/rollbar';
import { sendSlackMsgWithDebounce } from './core/slack';
const app = express();
//
Rollbar.init();
// Tell any CSS tooling (such as Material UI) to use all vendor prefixes if the
// user agent is not known.
// -----------------------------------------------------------------------------
@@ -114,9 +116,22 @@ app.post('/rsvp/save', (req, res) => {
key: req.body.key,
},
}).then(() => {
res.json({
success: true,
prop: req.body.prop,
Person.findOne({
where: { key: req.body.key },
}).then(data => {
if (data.completed) {
sendSlackMsgWithDebounce(`${data.firstname} ${data.lastname} has saved their RSVP:\n${data.firstname} ${data.attending ? 'is' : 'is not'} coming.${data.attending ? `\nStarter: ${starters[data.starter]}\nMain: ${mains[data.main]}\nDietary requirements: ${data.dietary ? data.dietary : 'None'}` : ''}`, req.body.key);
}
res.json({
success: true,
prop: req.body.prop,
});
}).catch(err => {
Rollbar.handleError(err);
res.json({
success: false,
err,
});
});
}).catch(err => {
Rollbar.handleError(err);
@@ -259,11 +274,7 @@ app.get('*', async (req, res, next) => {
//
// Error handling
// -----------------------------------------------------------------------------
Rollbar.init(analytics.rollbar.serverToken);
Rollbar.handleUncaughtExceptionsAndRejections(analytics.rollbar.serverToken, {
exitOnUncaughtException: true,
});
app.use(Rollbar.errorHandler(analytics.rollbar.serverToken));
app.use(Rollbar.errorHandler());
const pe = new PrettyError();
pe.skipNodeFiles();
pe.skipPackage('express');