2018-03-09 19:56:22 +01:00
|
|
|
// app dependencies
|
2018-03-09 00:40:54 +01:00
|
|
|
const express = require('express');
|
|
|
|
const bodyParser = require('body-parser');
|
|
|
|
const expressHandlebars = require('express-handlebars');
|
|
|
|
const Handlebars = require('handlebars');
|
|
|
|
const helmet = require('helmet');
|
|
|
|
const passport = require('passport');
|
2018-03-09 19:56:22 +01:00
|
|
|
const { populateLocalsDotUser, serializeSpeechUser, deserializeSpeechUser } = require('./helpers/authHelpers.js');
|
2018-03-09 00:40:54 +01:00
|
|
|
const cookieSession = require('cookie-session');
|
2018-03-10 01:45:24 +01:00
|
|
|
const http = require('http');
|
2018-03-09 19:56:22 +01:00
|
|
|
// logging dependencies
|
|
|
|
const logger = require('winston');
|
2018-03-09 00:40:54 +01:00
|
|
|
|
2018-03-10 01:45:24 +01:00
|
|
|
function SpeechServer ({ mysqlConfig, siteConfig, slackConfig }) {
|
2018-03-09 07:06:27 +01:00
|
|
|
this.start = () => {
|
2018-03-13 03:26:03 +01:00
|
|
|
this.configureConfigFiles();
|
2018-03-13 04:31:13 +01:00
|
|
|
this.configureLogging();
|
2018-03-09 19:56:22 +01:00
|
|
|
this.configureApp();
|
|
|
|
this.configureServer();
|
|
|
|
this.startServer();
|
|
|
|
};
|
2018-03-10 01:45:24 +01:00
|
|
|
this.configureConfigFiles = () => {
|
|
|
|
const mysqlAppConfig = require('./config/mysqlConfig.js');
|
|
|
|
mysqlAppConfig.configure(mysqlConfig);
|
2018-03-10 03:30:06 +01:00
|
|
|
const siteAppConfig = require('./config/siteConfig.js');
|
|
|
|
siteAppConfig.configure(siteConfig);
|
2018-03-13 23:37:42 +01:00
|
|
|
this.PORT = siteAppConfig.details.port;
|
2018-03-10 01:45:24 +01:00
|
|
|
const slackAppConfig = require('./config/slackConfig.js');
|
|
|
|
slackAppConfig.configure(slackConfig);
|
|
|
|
};
|
2018-03-09 19:56:22 +01:00
|
|
|
this.configureLogging = () => {
|
2018-03-09 20:20:13 +01:00
|
|
|
require('./helpers/configureLogger.js')(logger);
|
2018-03-10 01:45:24 +01:00
|
|
|
require('./helpers/configureSlack.js')(logger);
|
2018-03-09 19:56:22 +01:00
|
|
|
};
|
|
|
|
this.configureApp = () => {
|
|
|
|
const app = express(); // create an Express application
|
|
|
|
|
2018-03-09 05:48:29 +01:00
|
|
|
// trust the proxy to get ip address for us
|
|
|
|
app.enable('trust proxy');
|
2018-03-09 00:40:54 +01:00
|
|
|
|
2018-03-09 05:48:29 +01:00
|
|
|
// add middleware
|
|
|
|
app.use(helmet()); // set HTTP headers to protect against well-known web vulnerabilties
|
|
|
|
app.use(express.static(`${__dirname}/public`)); // 'express.static' to serve static files from public directory
|
|
|
|
app.use(bodyParser.json()); // 'body parser' for parsing application/json
|
|
|
|
app.use(bodyParser.urlencoded({ extended: true })); // 'body parser' for parsing application/x-www-form-urlencoded
|
|
|
|
app.use((req, res, next) => { // custom logging middleware to log all incoming http requests
|
|
|
|
logger.verbose(`Request on ${req.originalUrl} from ${req.ip}`);
|
|
|
|
next();
|
|
|
|
});
|
2018-03-09 00:40:54 +01:00
|
|
|
|
2018-03-09 05:48:29 +01:00
|
|
|
// configure passport
|
|
|
|
passport.serializeUser(serializeSpeechUser);
|
|
|
|
passport.deserializeUser(deserializeSpeechUser);
|
2018-03-10 01:45:24 +01:00
|
|
|
const localSignupStrategy = require('./passport/local-signup.js');
|
|
|
|
const localLoginStrategy = require('./passport/local-login.js');
|
2018-03-09 05:48:29 +01:00
|
|
|
passport.use('local-signup', localSignupStrategy);
|
|
|
|
passport.use('local-login', localLoginStrategy);
|
|
|
|
// initialize passport
|
|
|
|
app.use(cookieSession({
|
|
|
|
name : 'session',
|
2018-03-13 03:26:03 +01:00
|
|
|
keys : [siteConfig.auth.sessionKey],
|
2018-03-09 05:48:29 +01:00
|
|
|
maxAge: 24 * 60 * 60 * 1000, // i.e. 24 hours
|
|
|
|
}));
|
|
|
|
app.use(passport.initialize());
|
|
|
|
app.use(passport.session());
|
2018-03-09 00:40:54 +01:00
|
|
|
|
2018-03-09 05:48:29 +01:00
|
|
|
// configure handlebars & register it with express app
|
|
|
|
const hbs = expressHandlebars.create({
|
|
|
|
defaultLayout: 'embed',
|
|
|
|
handlebars : Handlebars,
|
2018-03-09 00:40:54 +01:00
|
|
|
});
|
2018-03-09 05:48:29 +01:00
|
|
|
app.engine('handlebars', hbs.engine);
|
|
|
|
app.set('view engine', 'handlebars');
|
2018-03-09 00:40:54 +01:00
|
|
|
|
2018-03-09 05:48:29 +01:00
|
|
|
// middleware to pass user info back to client (for handlebars access), if user is logged in
|
|
|
|
app.use(populateLocalsDotUser); // note: I don't think I need this any more?
|
|
|
|
|
2018-03-09 06:49:59 +01:00
|
|
|
// set the routes on the app
|
|
|
|
require('./routes/auth-routes.js')(app);
|
|
|
|
require('./routes/api-routes.js')(app);
|
|
|
|
require('./routes/page-routes.js')(app);
|
|
|
|
require('./routes/serve-routes.js')(app);
|
|
|
|
require('./routes/fallback-routes.js')(app);
|
|
|
|
|
2018-03-09 19:56:22 +01:00
|
|
|
this.app = app;
|
|
|
|
};
|
|
|
|
this.configureServer = () => {
|
|
|
|
this.server = http.Server(this.app);
|
2018-03-09 06:49:59 +01:00
|
|
|
};
|
2018-03-09 19:56:22 +01:00
|
|
|
this.startServer = () => {
|
2018-03-10 01:45:24 +01:00
|
|
|
const db = require('./models');
|
2018-03-09 05:48:29 +01:00
|
|
|
// sync sequelize
|
2018-03-10 01:45:24 +01:00
|
|
|
db.sequelize.sync()
|
2018-03-09 05:48:29 +01:00
|
|
|
// start the server
|
2018-03-09 06:49:59 +01:00
|
|
|
.then(() => {
|
2018-03-09 19:56:22 +01:00
|
|
|
this.server.listen(this.PORT, () => {
|
2018-03-09 06:49:59 +01:00
|
|
|
logger.info(`Server is listening on PORT ${this.PORT}`);
|
2018-03-09 05:48:29 +01:00
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
logger.error(`Startup Error:`, error);
|
|
|
|
});
|
2018-03-09 06:49:59 +01:00
|
|
|
};
|
2018-03-09 00:40:54 +01:00
|
|
|
};
|
2018-03-09 06:49:59 +01:00
|
|
|
|
|
|
|
module.exports = SpeechServer;
|