created bundle
This commit is contained in:
parent
c67a25ca2c
commit
6e46e3271d
6 changed files with 10206 additions and 10523 deletions
20565
index.js
20565
index.js
File diff suppressed because one or more lines are too long
|
@ -7,19 +7,32 @@ const helmet = require('helmet');
|
|||
const cookieSession = require('cookie-session');
|
||||
const http = require('http');
|
||||
const logger = require('winston');
|
||||
const requestLogger = require('middleware/requestLogger.js');
|
||||
|
||||
function Server () {
|
||||
this.configureLogger = (loggerConfig) => {
|
||||
require('loggerConfig.js').configure(loggerConfig);
|
||||
}
|
||||
this.configureMysql = (mysqlConfig) => {
|
||||
require('../config/mysqlConfig.js').configure(mysqlConfig);
|
||||
require('mysqlConfig.js').configure(mysqlConfig);
|
||||
};
|
||||
this.configureSite = (siteConfig) => {
|
||||
require('../config/siteConfig.js').configure(siteConfig);
|
||||
this.configureSiteDetails = (siteConfig) => {
|
||||
require('siteConfig.js').configure(siteConfig);
|
||||
this.sessionKey = siteConfig.auth.sessionKey;
|
||||
this.PORT = siteConfig.details.port;
|
||||
};
|
||||
this.configureSlack = (slackConfig) => {
|
||||
require('../config/slackConfig.js').configure(slackConfig);
|
||||
require('slackConfig.js').configure(slackConfig);
|
||||
};
|
||||
this.configureClientBundle = () => {
|
||||
console.log('configure the client here by passing in the bundle and configuring it, or better yet: taking in the components to use dynamically from here.');
|
||||
}
|
||||
this.configureModels = () => {
|
||||
console.log('here is where you could add/overwrite the default models')
|
||||
}
|
||||
this.configureRoutes = () => {
|
||||
console.log('here is where you could add/overwrite the default routes')
|
||||
}
|
||||
this.createApp = () => {
|
||||
// create an Express application
|
||||
const app = express();
|
||||
|
@ -30,12 +43,12 @@ function Server () {
|
|||
// add middleware
|
||||
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
|
||||
// note: take in a different public folder, so it can serve it's own bundle from there?
|
||||
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}`);
|
||||
next();
|
||||
});
|
||||
|
||||
// add custom middleware (note: build out to accept dynamically use what is in server/middleware/
|
||||
app.use(requestLogger);
|
||||
|
||||
// configure passport
|
||||
const speechPassport = require('speechPassport');
|
||||
|
@ -57,17 +70,17 @@ function Server () {
|
|||
app.set('view engine', 'handlebars');
|
||||
|
||||
// set the routes on the app
|
||||
require('./routes/auth.js')(app);
|
||||
require('./routes/api.js')(app);
|
||||
require('./routes/pages.js')(app);
|
||||
require('./routes/assets.js')(app);
|
||||
require('./routes/fallback.js')(app);
|
||||
require('./routes/auth/')(app);
|
||||
require('./routes/api/')(app);
|
||||
require('./routes/pages/')(app);
|
||||
require('./routes/assets/')(app);
|
||||
require('./routes/fallback/')(app);
|
||||
|
||||
this.app = app;
|
||||
};
|
||||
this.initialize = () => {
|
||||
require('./helpers/configureLogger.js')(logger);
|
||||
require('./helpers/configureSlack.js')(logger);
|
||||
// require('./helpers/configureLogger.js')(logger);
|
||||
// require('./helpers/configureSlack.js')(logger);
|
||||
this.createApp();
|
||||
this.server = http.Server(this.app);
|
||||
};
|
8
server/middleware/requestLogger.js
Normal file
8
server/middleware/requestLogger.js
Normal file
|
@ -0,0 +1,8 @@
|
|||
const logger = require('winston');
|
||||
|
||||
const requestLogger = (req, res, next) => { // custom logging middleware to log all incoming http requests
|
||||
logger.verbose(`Request on ${req.originalUrl} from ${req.ip}`);
|
||||
next();
|
||||
};
|
||||
|
||||
module.exports = requestLogger;
|
|
@ -5,11 +5,75 @@ const File = require('models/file.js');
|
|||
const Request = require('models/request.js');
|
||||
const User = require('models/user.js');
|
||||
|
||||
module.exports = {
|
||||
Certificate,
|
||||
Channel,
|
||||
Claim,
|
||||
File,
|
||||
Request,
|
||||
User,
|
||||
}
|
||||
const Sequelize = require('sequelize');
|
||||
const logger = require('winston');
|
||||
|
||||
const {database, username, password} = require('mysqlConfig.js');
|
||||
|
||||
// set sequelize options
|
||||
const sequelize = new Sequelize(database, username, password, {
|
||||
host : 'localhost',
|
||||
dialect : 'mysql',
|
||||
dialectOptions: {decimalNumbers: true},
|
||||
logging : false,
|
||||
pool : {
|
||||
max : 5,
|
||||
min : 0,
|
||||
idle : 10000,
|
||||
acquire: 10000,
|
||||
},
|
||||
});
|
||||
|
||||
// establish mysql connection
|
||||
sequelize
|
||||
.authenticate()
|
||||
.then(() => {
|
||||
logger.info('Sequelize has established mysql connection successfully.');
|
||||
})
|
||||
.catch(err => {
|
||||
logger.error('Sequelize was unable to connect to the database:', err);
|
||||
});
|
||||
|
||||
// manually add each model to the db object (note: make this dynamic)
|
||||
const db = {};
|
||||
db['Certificate'] = sequelize.import('Certificate', Certificate);
|
||||
db['Channel'] = sequelize.import('Channel', Channel);
|
||||
db['Claim'] = sequelize.import('Claim', Claim);
|
||||
db['File'] = sequelize.import('File', File);
|
||||
db['Request'] = sequelize.import('Request', Request);
|
||||
db['User'] = sequelize.import('User', User);
|
||||
|
||||
// run model.association for each model in the db object that has an association
|
||||
logger.info('associating db models...');
|
||||
Object.keys(db).forEach(modelName => {
|
||||
if (db[modelName].associate) {
|
||||
logger.info('Associating model:', modelName);
|
||||
db[modelName].associate(db);
|
||||
}
|
||||
});
|
||||
|
||||
// add sequelize/Sequelize to db
|
||||
db.sequelize = sequelize;
|
||||
db.Sequelize = Sequelize;
|
||||
// add an 'upsert' method to the db object
|
||||
db.upsert = (Model, values, condition, tableName) => {
|
||||
return Model
|
||||
.findOne({
|
||||
where: condition,
|
||||
})
|
||||
.then(obj => {
|
||||
if (obj) { // update
|
||||
logger.debug(`updating record in db.${tableName}`);
|
||||
return obj.update(values);
|
||||
} else { // insert
|
||||
logger.debug(`creating record in db.${tableName}`);
|
||||
return Model.create(values);
|
||||
}
|
||||
})
|
||||
.catch(function (error) {
|
||||
logger.error(`${tableName}.upsert error`, error);
|
||||
throw error;
|
||||
});
|
||||
};
|
||||
|
||||
module.exports = db;
|
||||
|
|
33
speech.js
33
speech.js
|
@ -1,36 +1,7 @@
|
|||
import App from 'app';
|
||||
import Reducer from 'reducers';
|
||||
import rootSaga from 'sagas';
|
||||
import GAListener from 'components/GAListener';
|
||||
|
||||
const api = require('./server/routes/api/');
|
||||
const asset = require('./server/routes/assets/');
|
||||
const auth = require('./server/routes/auth/');
|
||||
const page = require('./server/routes/pages/');
|
||||
const logger = require('./config/loggerConfig.js');
|
||||
const mysql = require('./config/mysqlConfig');
|
||||
const site = require('./config/siteConfig');
|
||||
const slack = require('./config/slackConfig.js');
|
||||
const passport = require('./server/speechPassport/');
|
||||
const models = require('./server/models/');
|
||||
const Server = require('./server');
|
||||
|
||||
const exports = {
|
||||
App,
|
||||
Reducer,
|
||||
rootSaga,
|
||||
GAListener,
|
||||
logger,
|
||||
models,
|
||||
mysql,
|
||||
passport,
|
||||
site,
|
||||
slack,
|
||||
routes: {
|
||||
api,
|
||||
asset,
|
||||
auth,
|
||||
page,
|
||||
},
|
||||
Server,
|
||||
};
|
||||
|
||||
module.exports = exports;
|
||||
|
|
Loading…
Reference in a new issue