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 siofu = require('socketio-file-upload');
|
|
|
|
const expressHandlebars = require('express-handlebars');
|
|
|
|
const Handlebars = require('handlebars');
|
2017-09-17 02:50:22 +02:00
|
|
|
const handlebarsHelpers = require('./helpers/handlebarsHelpers.js');
|
2017-06-19 18:37:35 +02:00
|
|
|
const config = require('config');
|
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-09-18 19:14:06 +02:00
|
|
|
const session = require('express-session');
|
2017-07-18 18:59:40 +02:00
|
|
|
|
2017-06-19 22:10:06 +02:00
|
|
|
// configure logging
|
|
|
|
const logLevel = config.get('Logging.LogLevel');
|
2017-09-14 00:41:52 +02:00
|
|
|
require('./config/loggerConfig.js')(logger, logLevel);
|
|
|
|
require('./config/slackLoggerConfig.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
|
|
|
|
app.use(siofu.router); // 'socketio-file-upload' router for uploading with socket.io
|
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-09-20 03:50:25 +02:00
|
|
|
logger.debug(req.body);
|
2017-07-06 22:37:03 +02:00
|
|
|
next();
|
|
|
|
});
|
2017-09-19 17:47:24 +02:00
|
|
|
|
2017-09-17 02:50:22 +02:00
|
|
|
// initialize passport
|
2017-09-18 19:14:06 +02:00
|
|
|
app.use(session({ secret: 'cats' }));
|
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-19 17:47:24 +02:00
|
|
|
passport.serializeUser((user, done) => {
|
2017-09-18 19:14:06 +02:00
|
|
|
done(null, user.id);
|
|
|
|
});
|
2017-09-19 17:47:24 +02:00
|
|
|
passport.deserializeUser((id, done) => {
|
2017-09-18 19:14:06 +02:00
|
|
|
db.User.findOne({ where: { id } })
|
|
|
|
.then(user => {
|
2017-09-21 20:00:06 +02:00
|
|
|
done(null, user);
|
|
|
|
return null;
|
2017-09-18 19:14:06 +02:00
|
|
|
})
|
2017-09-21 20:00:06 +02:00
|
|
|
.then()
|
2017-09-18 19:14:06 +02:00
|
|
|
.catch(error => {
|
|
|
|
logger.error('sequelize error', error);
|
2017-09-19 17:47:24 +02:00
|
|
|
done(error, null);
|
2017-09-18 19:14:06 +02:00
|
|
|
});
|
|
|
|
});
|
2017-09-17 02:50:22 +02:00
|
|
|
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-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-09-19 17:47:24 +02:00
|
|
|
app.use((req, res, next) => {
|
|
|
|
if (req.user) {
|
|
|
|
res.locals.user = {
|
2017-09-22 01:03:45 +02:00
|
|
|
id : req.user.id,
|
2017-09-26 06:03:43 +02:00
|
|
|
userName : req.user.userName,
|
2017-09-22 01:03:45 +02:00
|
|
|
channelName : req.user.channelName,
|
|
|
|
channelClaimId: req.user.channelClaimId,
|
2017-09-19 17:47:24 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
next();
|
|
|
|
});
|
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));
|
|
|
|
// require routes & wrap in socket.io
|
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);
|
|
|
|
return require('./routes/sockets-routes.js')(app, siofu, hostedContentPath);
|
|
|
|
})
|
|
|
|
.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
|
|
|
});
|