2017-05-24 20:07:43 +02:00
|
|
|
// load dependencies
|
2017-06-19 18:37:35 +02:00
|
|
|
const express = require('express');
|
|
|
|
const bodyParser = require('body-parser');
|
|
|
|
const expressHandlebars = require('express-handlebars');
|
|
|
|
const Handlebars = require('handlebars');
|
2017-09-17 02:50:22 +02:00
|
|
|
const handlebarsHelpers = require('./helpers/handlebarsHelpers.js');
|
2017-10-11 01:00:08 +02:00
|
|
|
const { populateLocalsDotUser, serializeSpeechUser, deserializeSpeechUser } = require('./helpers/authHelpers.js');
|
2017-11-07 00:18:45 +01:00
|
|
|
const config = require('./config/speechConfig.js');
|
2017-07-17 22:54:50 +02:00
|
|
|
const logger = require('winston');
|
2017-08-03 02:13:02 +02:00
|
|
|
const { getDownloadDirectory } = require('./helpers/lbryApi');
|
2017-09-15 20:09:21 +02:00
|
|
|
const helmet = require('helmet');
|
2017-07-18 18:59:40 +02:00
|
|
|
const PORT = 3000; // set port
|
|
|
|
const app = express(); // create an Express application
|
|
|
|
const db = require('./models'); // require our models for syncing
|
2017-09-17 02:50:22 +02:00
|
|
|
const passport = require('passport');
|
2017-10-26 19:48:09 +02:00
|
|
|
const cookieSession = require('cookie-session');
|
2017-07-18 18:59:40 +02:00
|
|
|
|
2017-06-19 22:10:06 +02:00
|
|
|
// configure logging
|
2017-11-07 00:18:45 +01:00
|
|
|
const logLevel = config.logging.logLevel;
|
2017-09-14 00:41:52 +02:00
|
|
|
require('./config/loggerConfig.js')(logger, logLevel);
|
2017-11-07 00:18:45 +01:00
|
|
|
require('./config/slackConfig.js')(logger);
|
2017-06-19 22:10:06 +02:00
|
|
|
|
2017-09-25 18:13:18 +02:00
|
|
|
// check for global config variables
|
|
|
|
require('./helpers/configVarCheck.js')();
|
|
|
|
|
2017-07-27 23:18:55 +02:00
|
|
|
// trust the proxy to get ip address for us
|
|
|
|
app.enable('trust proxy');
|
2017-09-19 17:47:24 +02:00
|
|
|
|
2017-07-27 23:18:55 +02:00
|
|
|
// add middleware
|
2017-09-15 20:09:21 +02:00
|
|
|
app.use(helmet()); // set HTTP headers to protect against well-known web vulnerabilties
|
2017-07-27 23:18:55 +02:00
|
|
|
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
|
2017-09-19 17:47:24 +02:00
|
|
|
app.use((req, res, next) => { // custom logging middleware to log all incoming http requests
|
2017-07-17 22:54:50 +02:00
|
|
|
logger.verbose(`Request on ${req.originalUrl} from ${req.ip}`);
|
2017-07-06 22:37:03 +02:00
|
|
|
next();
|
|
|
|
});
|
2017-09-19 17:47:24 +02:00
|
|
|
|
2017-11-30 23:46:32 +01:00
|
|
|
// configure passport
|
|
|
|
passport.serializeUser(serializeSpeechUser);
|
|
|
|
passport.deserializeUser(deserializeSpeechUser);
|
|
|
|
const localSignupStrategy = require('./passport/local-signup.js');
|
|
|
|
const localLoginStrategy = require('./passport/local-login.js');
|
|
|
|
passport.use('local-signup', localSignupStrategy);
|
|
|
|
passport.use('local-login', localLoginStrategy);
|
2017-09-17 02:50:22 +02:00
|
|
|
// initialize passport
|
2017-10-26 19:48:09 +02:00
|
|
|
app.use(cookieSession({
|
|
|
|
name : 'session',
|
2017-11-07 00:18:45 +01:00
|
|
|
keys : [config.session.sessionKey],
|
2017-10-26 19:48:09 +02:00
|
|
|
maxAge: 24 * 60 * 60 * 1000, // 24 hours
|
|
|
|
}));
|
2017-09-17 02:50:22 +02:00
|
|
|
app.use(passport.initialize());
|
2017-09-18 19:14:06 +02:00
|
|
|
app.use(passport.session());
|
2017-09-17 02:50:22 +02:00
|
|
|
|
2017-07-18 18:59:40 +02:00
|
|
|
// configure handlebars & register it with express app
|
2017-06-17 22:51:30 +02:00
|
|
|
const hbs = expressHandlebars.create({
|
|
|
|
defaultLayout: 'main', // sets the default layout
|
|
|
|
handlebars : Handlebars, // includes basic handlebars for access to that library
|
2017-09-17 02:50:22 +02:00
|
|
|
helpers : handlebarsHelpers, // custom defined helpers
|
2017-06-19 18:37:35 +02:00
|
|
|
});
|
|
|
|
app.engine('handlebars', hbs.engine);
|
|
|
|
app.set('view engine', 'handlebars');
|
2017-06-10 01:46:57 +02:00
|
|
|
|
2017-09-25 18:13:18 +02:00
|
|
|
// middleware to pass user info back to client (for handlebars access), if user is logged in
|
2017-10-11 01:00:08 +02:00
|
|
|
app.use(populateLocalsDotUser);
|
2017-09-20 03:50:25 +02:00
|
|
|
|
2017-07-18 18:52:18 +02:00
|
|
|
// start the server
|
|
|
|
db.sequelize
|
|
|
|
.sync() // sync sequelize
|
|
|
|
.then(() => { // get the download directory from the daemon
|
2017-09-14 00:59:29 +02:00
|
|
|
logger.info('Retrieving daemon download directory...');
|
2017-07-18 18:52:18 +02:00
|
|
|
return getDownloadDirectory();
|
|
|
|
})
|
2017-07-27 23:18:55 +02:00
|
|
|
.then(hostedContentPath => {
|
|
|
|
// add the hosted content folder at a static path
|
|
|
|
app.use('/media', express.static(hostedContentPath));
|
2017-11-08 01:15:12 +01:00
|
|
|
// require routes
|
2017-09-20 23:39:20 +02:00
|
|
|
require('./routes/auth-routes.js')(app);
|
|
|
|
require('./routes/api-routes.js')(app);
|
2017-08-01 02:02:39 +02:00
|
|
|
require('./routes/page-routes.js')(app);
|
2017-07-18 18:52:18 +02:00
|
|
|
require('./routes/serve-routes.js')(app);
|
|
|
|
require('./routes/home-routes.js')(app);
|
2017-11-01 21:07:13 +01:00
|
|
|
const http = require('http');
|
|
|
|
return http.Server(app);
|
2017-07-18 18:52:18 +02:00
|
|
|
})
|
|
|
|
.then(server => { // start the server
|
2017-07-12 01:55:03 +02:00
|
|
|
server.listen(PORT, () => {
|
2017-07-17 22:54:50 +02:00
|
|
|
logger.info('Trusting proxy?', app.get('trust proxy'));
|
|
|
|
logger.info(`Server is listening on PORT ${PORT}`);
|
2017-07-12 01:55:03 +02:00
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
2017-09-14 00:59:29 +02:00
|
|
|
logger.error(`Startup Error >> ${error.message}`, error);
|
2017-06-19 18:37:35 +02:00
|
|
|
});
|