added module aliasing

This commit is contained in:
bill bittner 2018-06-06 08:30:30 -07:00
parent a1210149b3
commit 7c847ceea8
4 changed files with 85 additions and 62 deletions

130
index.js
View file

@ -8,80 +8,86 @@ const cookieSession = require('cookie-session');
const http = require('http');
const logger = require('winston');
const Path = require('path');
const moduleAlias = require('module-alias');
// load local modules
const requestLogger = require('./server/middleware/requestLogger.js');
const siteConfig = require('@config/siteConfig');
const PORT = siteConfig.details.port;
const createDatabaseIfNotExists = require('./server/models/utils/createDatabaseIfNotExists.js');
const { getWalletBalance } = require('./server/lbrynet');
const db = require('./server/models');
// configure logging
const configureLogging = require('./server/utils/configureLogging.js');
configureLogging();
// configure slack logging
const configureSlack = require('./server/utils/configureSlack.js');
configureSlack();
const speechPassport = require('./server/speechPassport');
const createModuleAliases = require('./utils/createModuleAliases.js');
/* create app */
const app = express();
// set up aliasing
const customAliases = createModuleAliases();
logger.debug('custom aliases', customAliases);
moduleAlias.addAliases(customAliases);
// trust the proxy to get ip address for us
app.enable('trust proxy');
// set HTTP headers to protect against well-known web vulnerabilties
app.use(helmet());
// 'express.static' to serve static files from public directory
const publicPath = Path.resolve(process.cwd(), 'public');
app.use(express.static(publicPath));
logger.info(`serving static files from default static path at ${publicPath}.`);
// 'body parser' for parsing application/json
app.use(bodyParser.json());
// 'body parser' for parsing application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: true }));
// add custom middleware (note: build out to accept dynamically use what is in server/middleware/
app.use(requestLogger);
// configure passport
const speechPassport = require('./server/speechPassport/index');
// initialize passport
const sessionKey = siteConfig.auth.sessionKey;
app.use(cookieSession({
name : 'session',
keys : [sessionKey],
}));
app.use(speechPassport.initialize());
app.use(speechPassport.session());
// configure handlebars & register it with express app
const hbs = expressHandlebars.create({
defaultLayout: 'embed',
handlebars : Handlebars,
});
app.engine('handlebars', hbs.engine);
app.set('view engine', 'handlebars');
// set the routes on the app
require('./server/routes/auth/index')(app);
require('./server/routes/api/index')(app);
require('./server/routes/pages/index')(app);
require('./server/routes/assets/index')(app);
require('./server/routes/fallback/index')(app);
/* create server */
const server = http.Server(app);
/* start the server */
// load aliased modules
const siteConfig = require('@config/siteConfig');
const PORT = siteConfig.details.port;
const startServer = () => {
return createDatabaseIfNotExists()
// configure logging
configureLogging();
// configure slack logging
configureSlack();
/* create app */
const app = express();
// trust the proxy to get ip address for us
app.enable('trust proxy');
// set HTTP headers to protect against well-known web vulnerabilties
app.use(helmet());
// 'express.static' to serve static files from public directory
const publicPath = Path.resolve(process.cwd(), 'public');
app.use(express.static(publicPath));
logger.info(`serving static files from default static path at ${publicPath}.`);
// 'body parser' for parsing application/json
app.use(bodyParser.json());
// 'body parser' for parsing application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: true }));
// add custom middleware (note: build out to accept dynamically use what is in server/middleware/
app.use(requestLogger);
// initialize passport
const sessionKey = siteConfig.auth.sessionKey;
app.use(cookieSession({
name : 'session',
keys : [sessionKey],
}));
app.use(speechPassport.initialize());
app.use(speechPassport.session());
// configure handlebars & register it with express app
const hbs = expressHandlebars.create({
defaultLayout: 'embed',
handlebars : Handlebars,
});
app.engine('handlebars', hbs.engine);
app.set('view engine', 'handlebars');
// set the routes on the app
require('./server/routes/auth')(app);
require('./server/routes/api')(app);
require('./server/routes/pages')(app);
require('./server/routes/assets')(app);
require('./server/routes/fallback')(app);
/* create server */
const server = http.Server(app);
/* start the server */
createDatabaseIfNotExists()
.then(() => {
return getWalletBalance();
})

5
package-lock.json generated
View file

@ -5291,6 +5291,11 @@
}
}
},
"module-alias": {
"version": "2.0.6",
"resolved": "https://registry.npmjs.org/module-alias/-/module-alias-2.0.6.tgz",
"integrity": "sha1-q7LPoHAU9QNRStUGHG8D15tZGIk="
},
"moment": {
"version": "2.22.1",
"resolved": "http://registry.npmjs.org/moment/-/moment-2.22.1.tgz",

View file

@ -38,6 +38,7 @@
"express": "^4.15.2",
"express-handlebars": "^3.0.0",
"helmet": "^3.8.1",
"module-alias": "^2.0.6",
"mysql2": "^1.3.5",
"passport": "^0.4.0",
"passport-local": "^1.0.0",

View file

@ -0,0 +1,11 @@
const { resolve } = require('path');
const WWW_SPEECH_ROOT = resolve(process.cwd());
module.exports = () => {
let moduleAliases = {};
// default aliases
moduleAliases['@config'] = resolve(WWW_SPEECH_ROOT, 'config');
moduleAliases['@public'] = resolve(WWW_SPEECH_ROOT, 'public');
// return finished aliases
return moduleAliases;
};