spee.ch/speech.js

109 lines
3.9 KiB
JavaScript
Raw Normal View History

// load dependencies
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');
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
// configure logging
const logLevel = config.get('Logging.LogLevel');
require('./config/loggerConfig.js')(logger, logLevel);
require('./config/slackLoggerConfig.js')(logger);
// trust the proxy to get ip address for us
app.enable('trust proxy');
2017-09-19 17:47:24 +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
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}`);
logger.debug(req.body);
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 => {
done(null, user);
return null;
2017-09-18 19:14: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
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
});
app.engine('handlebars', hbs.engine);
app.set('view engine', 'handlebars');
2017-06-10 01:46:57 +02:00
2017-09-19 17:47:24 +02:00
// middleware to pass user info back to client, if user is logged in
app.use((req, res, next) => {
if (req.user) {
res.locals.user = {
id : req.user.id,
channelName: req.user.channelName,
};
}
next();
});
// 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...');
return getDownloadDirectory();
})
.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);
require('./routes/page-routes.js')(app);
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
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}`);
});
})
.catch((error) => {
2017-09-14 00:59:29 +02:00
logger.error(`Startup Error >> ${error.message}`, error);
});