spee.ch/speech.js

95 lines
3.7 KiB
JavaScript
Raw Normal View History

// load dependencies
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');
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');
const cookieSession = require('cookie-session');
2017-07-18 18:59:40 +02:00
// configure logging
2017-11-07 00:18:45 +01:00
const logLevel = config.logging.logLevel;
require('./config/loggerConfig.js')(logger, logLevel);
2017-11-07 00:18:45 +01:00
require('./config/slackConfig.js')(logger);
2017-09-25 18:13:18 +02:00
// check for global config variables
require('./helpers/configVarCheck.js')();
// 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
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}`);
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
app.use(cookieSession({
name : 'session',
2017-11-07 00:18:45 +01:00
keys : [config.session.sessionKey],
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
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-25 18:13:18 +02:00
// middleware to pass user info back to client (for handlebars access), if user is logged in
app.use(populateLocalsDotUser);
// 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
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);
2018-02-21 00:25:04 +01:00
require('./routes/fallback-routes.js')(app);
const http = require('http');
return http.Server(app);
})
.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);
});