Merge pull request #384 from lbryio/speech-as-a-service-constructor
Speech as a service constructor
This commit is contained in:
commit
34603fbcd6
8 changed files with 205 additions and 207 deletions
|
@ -1,4 +1,3 @@
|
||||||
// const db = require('../models'); // require our models for syncing
|
|
||||||
const logger = require('winston');
|
const logger = require('winston');
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
|
|
@ -1,9 +1,7 @@
|
||||||
const config = require('../config/speechConfig.js');
|
|
||||||
const logger = require('winston');
|
const logger = require('winston');
|
||||||
|
|
||||||
module.exports = function () {
|
module.exports = (config) => {
|
||||||
// get the config file
|
// get the config file
|
||||||
|
|
||||||
for (let configCategoryKey in config) {
|
for (let configCategoryKey in config) {
|
||||||
if (config.hasOwnProperty(configCategoryKey)) {
|
if (config.hasOwnProperty(configCategoryKey)) {
|
||||||
// get the final variables for each config category
|
// get the final variables for each config category
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
const axios = require('axios');
|
const axios = require('axios');
|
||||||
const logger = require('winston');
|
const logger = require('winston');
|
||||||
const config = require('../config/speechConfig.js');
|
const { api: { apiHost, apiPort } } = require('../config/speechConfig.js');
|
||||||
const { apiHost, apiPort } = config.api;
|
|
||||||
const lbryApiUri = 'http://' + apiHost + ':' + apiPort;
|
const lbryApiUri = 'http://' + apiHost + ':' + apiPort;
|
||||||
const { chooseGaLbrynetPublishLabel, sendGATimingEvent } = require('./googleAnalytics.js');
|
const { chooseGaLbrynetPublishLabel, sendGATimingEvent } = require('./googleAnalytics.js');
|
||||||
|
|
||||||
|
|
2
index.js
2
index.js
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
|
@ -1,8 +1,8 @@
|
||||||
const PassportLocalStrategy = require('passport-local').Strategy;
|
const PassportLocalStrategy = require('passport-local').Strategy;
|
||||||
const db = require('../models');
|
|
||||||
const logger = require('winston');
|
const logger = require('winston');
|
||||||
|
|
||||||
function returnUserAndChannelInfo (userInstance) {
|
module.exports = (db) => {
|
||||||
|
const returnUserAndChannelInfo = (userInstance) => {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
let userInfo = {};
|
let userInfo = {};
|
||||||
userInfo['id'] = userInstance.id;
|
userInfo['id'] = userInstance.id;
|
||||||
|
@ -22,9 +22,9 @@ function returnUserAndChannelInfo (userInstance) {
|
||||||
reject(error);
|
reject(error);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
};
|
||||||
|
|
||||||
module.exports = new PassportLocalStrategy(
|
return new PassportLocalStrategy(
|
||||||
{
|
{
|
||||||
usernameField: 'username',
|
usernameField: 'username',
|
||||||
passwordField: 'password',
|
passwordField: 'password',
|
||||||
|
@ -61,5 +61,6 @@ module.exports = new PassportLocalStrategy(
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
return done(error);
|
return done(error);
|
||||||
});
|
});
|
||||||
}
|
},
|
||||||
);
|
);
|
||||||
|
};
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
const db = require('../models');
|
|
||||||
const PassportLocalStrategy = require('passport-local').Strategy;
|
const PassportLocalStrategy = require('passport-local').Strategy;
|
||||||
const lbryApi = require('../helpers/lbryApi.js');
|
const lbryApi = require('../helpers/lbryApi.js');
|
||||||
const logger = require('winston');
|
const logger = require('winston');
|
||||||
|
|
||||||
module.exports = new PassportLocalStrategy(
|
module.exports = (db) => {
|
||||||
|
return new PassportLocalStrategy(
|
||||||
{
|
{
|
||||||
usernameField: 'username',
|
usernameField: 'username',
|
||||||
passwordField: 'password',
|
passwordField: 'password',
|
||||||
|
@ -61,4 +61,5 @@ module.exports = new PassportLocalStrategy(
|
||||||
return done(error);
|
return done(error);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
};
|
||||||
|
|
118
server.js
118
server.js
|
@ -4,94 +4,94 @@ const bodyParser = require('body-parser');
|
||||||
const expressHandlebars = require('express-handlebars');
|
const expressHandlebars = require('express-handlebars');
|
||||||
const Handlebars = require('handlebars');
|
const Handlebars = require('handlebars');
|
||||||
const { populateLocalsDotUser, serializeSpeechUser, deserializeSpeechUser } = require('./helpers/authHelpers.js');
|
const { populateLocalsDotUser, serializeSpeechUser, deserializeSpeechUser } = require('./helpers/authHelpers.js');
|
||||||
const config = require('./config/speechConfig.js');
|
const { logging: { logLevel } } = require('./config/speechConfig.js');
|
||||||
const logger = require('winston');
|
const logger = require('winston');
|
||||||
const helmet = require('helmet');
|
const helmet = require('helmet');
|
||||||
const PORT = 3000; // set port
|
|
||||||
const app = express(); // create an Express application
|
const app = express(); // create an Express application
|
||||||
const passport = require('passport');
|
const passport = require('passport');
|
||||||
const cookieSession = require('cookie-session');
|
const cookieSession = require('cookie-session');
|
||||||
|
|
||||||
// configure logging
|
// configure logging
|
||||||
const logLevel = config.logging.logLevel;
|
|
||||||
require('./config/loggerConfig.js')(logger, logLevel);
|
require('./config/loggerConfig.js')(logger, logLevel);
|
||||||
require('./config/slackConfig.js')(logger);
|
require('./config/slackConfig.js')(logger);
|
||||||
|
|
||||||
// check for global config variables
|
function SpeechServer (config) {
|
||||||
require('./helpers/configVarCheck.js')();
|
this.mysqlConfig = config.mysql;
|
||||||
|
this.siteConfig = config.siteConfig;
|
||||||
|
this.lbrynetConfig = config.lbrynetConfig;
|
||||||
|
this.db = require('./models')(config.mysqlConfig);
|
||||||
|
this.PORT = 3000;
|
||||||
|
this.speak = (something) => {
|
||||||
|
console.log(something);
|
||||||
|
};
|
||||||
|
this.start = () => {
|
||||||
|
// trust the proxy to get ip address for us
|
||||||
|
app.enable('trust proxy');
|
||||||
|
|
||||||
// trust the proxy to get ip address for us
|
// add middleware
|
||||||
app.enable('trust proxy');
|
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
|
||||||
// add middleware
|
app.use(bodyParser.json()); // 'body parser' for parsing application/json
|
||||||
app.use(helmet()); // set HTTP headers to protect against well-known web vulnerabilties
|
app.use(bodyParser.urlencoded({ extended: true })); // 'body parser' for parsing application/x-www-form-urlencoded
|
||||||
app.use(express.static(`${__dirname}/public`)); // 'express.static' to serve static files from public directory
|
app.use((req, res, next) => { // custom logging middleware to log all incoming http requests
|
||||||
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}`);
|
logger.verbose(`Request on ${req.originalUrl} from ${req.ip}`);
|
||||||
next();
|
next();
|
||||||
});
|
});
|
||||||
|
|
||||||
// configure passport
|
// configure passport
|
||||||
passport.serializeUser(serializeSpeechUser);
|
passport.serializeUser(serializeSpeechUser);
|
||||||
passport.deserializeUser(deserializeSpeechUser);
|
passport.deserializeUser(deserializeSpeechUser);
|
||||||
const localSignupStrategy = require('./passport/local-signup.js');
|
const localSignupStrategy = require('./passport/local-signup.js')(this.db);
|
||||||
const localLoginStrategy = require('./passport/local-login.js');
|
const localLoginStrategy = require('./passport/local-login.js')(this.db);
|
||||||
passport.use('local-signup', localSignupStrategy);
|
passport.use('local-signup', localSignupStrategy);
|
||||||
passport.use('local-login', localLoginStrategy);
|
passport.use('local-login', localLoginStrategy);
|
||||||
// initialize passport
|
// initialize passport
|
||||||
app.use(cookieSession({
|
app.use(cookieSession({
|
||||||
name : 'session',
|
name : 'session',
|
||||||
keys : [config.session.sessionKey],
|
keys : [this.siteConfig.session.sessionKey],
|
||||||
maxAge: 24 * 60 * 60 * 1000, // 24 hours
|
maxAge: 24 * 60 * 60 * 1000, // i.e. 24 hours
|
||||||
}));
|
}));
|
||||||
app.use(passport.initialize());
|
app.use(passport.initialize());
|
||||||
app.use(passport.session());
|
app.use(passport.session());
|
||||||
|
|
||||||
// configure handlebars & register it with express app
|
// configure handlebars & register it with express app
|
||||||
const hbs = expressHandlebars.create({
|
const hbs = expressHandlebars.create({
|
||||||
defaultLayout: 'embed', // sets the default layout
|
defaultLayout: 'embed',
|
||||||
handlebars : Handlebars, // includes basic handlebars for access to that library
|
handlebars : Handlebars,
|
||||||
});
|
});
|
||||||
app.engine('handlebars', hbs.engine);
|
app.engine('handlebars', hbs.engine);
|
||||||
app.set('view engine', 'handlebars');
|
app.set('view engine', 'handlebars');
|
||||||
|
|
||||||
// middleware to pass user info back to client (for handlebars access), if user is logged in
|
// middleware to pass user info back to client (for handlebars access), if user is logged in
|
||||||
app.use(populateLocalsDotUser);
|
app.use(populateLocalsDotUser); // note: I don't think I need this any more?
|
||||||
|
|
||||||
// start the server
|
// set the routes on the app
|
||||||
const start = (config) => {
|
|
||||||
const { mysqlConfig } = config;
|
|
||||||
const db = require('./models')(mysqlConfig); // require our models for syncing
|
|
||||||
db.sequelize
|
|
||||||
// sync sequelize
|
|
||||||
.sync()
|
|
||||||
// require routes
|
|
||||||
.then(() => {
|
|
||||||
require('./routes/auth-routes.js')(app);
|
require('./routes/auth-routes.js')(app);
|
||||||
require('./routes/api-routes.js')(app);
|
require('./routes/api-routes.js')(app);
|
||||||
require('./routes/page-routes.js')(app);
|
require('./routes/page-routes.js')(app);
|
||||||
require('./routes/serve-routes.js')(app);
|
require('./routes/serve-routes.js')(app);
|
||||||
require('./routes/fallback-routes.js')(app);
|
require('./routes/fallback-routes.js')(app);
|
||||||
|
|
||||||
const http = require('http');
|
const http = require('http');
|
||||||
return http.Server(app);
|
const server = http.Server(app);
|
||||||
})
|
this.startServer(server);
|
||||||
|
};
|
||||||
|
this.startServer = (server) => {
|
||||||
|
// print config variables
|
||||||
|
require('./helpers/configVarCheck.js')(this.config);
|
||||||
|
this.db.sequelize
|
||||||
|
// sync sequelize
|
||||||
|
.sync()
|
||||||
// start the server
|
// start the server
|
||||||
.then(server => {
|
.then(() => {
|
||||||
server.listen(PORT, () => {
|
server.listen(this.PORT, () => {
|
||||||
logger.info('Trusting proxy?', app.get('trust proxy'));
|
logger.info(`Server is listening on PORT ${this.PORT}`);
|
||||||
logger.info(`Server is listening on PORT ${PORT}`);
|
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
logger.error(`Startup Error:`, error);
|
logger.error(`Startup Error:`, error);
|
||||||
});
|
});
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = {
|
module.exports = SpeechServer;
|
||||||
speak (something) {
|
|
||||||
console.log(something);
|
|
||||||
},
|
|
||||||
start,
|
|
||||||
};
|
|
||||||
|
|
Loading…
Reference in a new issue