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-20 20:52:34 +01:00
|
|
|
const { 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-16 08:52:39 +01:00
|
|
|
function SpeechServer () {
|
|
|
|
this.configureMysql = (mysqlConfig) => {
|
2018-03-20 20:52:34 +01:00
|
|
|
require('../config/mysqlConfig.js').configure(mysqlConfig);
|
2018-03-16 08:52:39 +01:00
|
|
|
};
|
|
|
|
this.configureSite = (siteConfig) => {
|
2018-03-20 20:52:34 +01:00
|
|
|
require('../config/siteConfig.js').configure(siteConfig);
|
|
|
|
console.log(require('../config/siteConfig.js'));
|
2018-03-16 08:52:39 +01:00
|
|
|
this.sessionKey = siteConfig.auth.sessionKey;
|
|
|
|
this.PORT = siteConfig.details.port;
|
2018-03-09 19:56:22 +01:00
|
|
|
};
|
2018-03-16 08:52:39 +01:00
|
|
|
this.configureSlack = (slackConfig) => {
|
2018-03-20 20:52:34 +01:00
|
|
|
require('../config/slackConfig.js').configure(slackConfig);
|
2018-03-10 01:45:24 +01:00
|
|
|
};
|
2018-03-16 09:06:09 +01:00
|
|
|
this.createApp = () => {
|
2018-03-16 08:52:39 +01:00
|
|
|
// create an Express application
|
|
|
|
const app = express();
|
2018-03-09 19:56:22 +01:00
|
|
|
|
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-20 20:52:34 +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-16 08:52:39 +01:00
|
|
|
keys : [this.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 06:49:59 +01:00
|
|
|
// set the routes on the app
|
2018-03-20 20:52:34 +01:00
|
|
|
require('./routes/auth-routes.js')(app);
|
|
|
|
require('./routes/api-routes.js')(app);
|
|
|
|
require('./routes/page-routes.js')(app);
|
|
|
|
require('./routes/asset-routes.js')(app);
|
|
|
|
require('./routes/fallback-routes.js')(app);
|
2018-03-09 06:49:59 +01:00
|
|
|
|
2018-03-09 19:56:22 +01:00
|
|
|
this.app = app;
|
|
|
|
};
|
2018-03-16 09:06:09 +01:00
|
|
|
this.initialize = () => {
|
2018-03-20 20:52:34 +01:00
|
|
|
require('./helpers/configureLogger.js')(logger);
|
|
|
|
require('./helpers/configureSlack.js')(logger);
|
2018-03-16 09:06:09 +01:00
|
|
|
this.createApp();
|
2018-03-09 19:56:22 +01:00
|
|
|
this.server = http.Server(this.app);
|
2018-03-09 06:49:59 +01:00
|
|
|
};
|
2018-03-16 09:06:09 +01:00
|
|
|
this.start = () => {
|
2018-03-20 20:52:34 +01:00
|
|
|
const db = require('./models/index');
|
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;
|