From 6d8884ca14f5d25af818c245ebf1851e6b02e58e Mon Sep 17 00:00:00 2001
From: bill bittner <bittner.w@gmail.com>
Date: Thu, 8 Mar 2018 13:18:34 -0800
Subject: [PATCH] updated webpack to build to index.js and moved sql to take a
 config

---
 .gitignore                     |   2 -
 config/speechConfig.js.example |   5 --
 controllers/statsController.js |  22 -----
 models/index.js                | 142 ++++++++++++++++-----------------
 package.json                   |   4 +-
 index.js => server.js          |  50 +++++++-----
 webpack.config.js              |   7 ++
 webpack.server.common.js       |   4 +-
 8 files changed, 110 insertions(+), 126 deletions(-)
 rename index.js => server.js (73%)
 create mode 100644 webpack.config.js

diff --git a/.gitignore b/.gitignore
index 2d5233ca..77e8782b 100644
--- a/.gitignore
+++ b/.gitignore
@@ -3,5 +3,3 @@ node_modules
 config/sequelizeCliConfig.js
 config/speechConfig.js
 public/bundle
-server.js
-webpack.config.js
diff --git a/config/speechConfig.js.example b/config/speechConfig.js.example
index af4b1de7..9aa94c3b 100644
--- a/config/speechConfig.js.example
+++ b/config/speechConfig.js.example
@@ -5,11 +5,6 @@ module.exports = {
   analytics: {
     googleId: null, // google id for analytics tracking; leave `null` if not applicable
   },
-  sql: {
-    database: null,  // name of mysql database
-    username: null,  // username for mysql
-    password: null,  // password for mysql
-  },
   logging: {
     logLevel         : null,  // options: silly, debug, verbose, info
     slackWebHook     : null,  // enter a webhook if you wish to push logs to slack; otherwise leave as `null`
diff --git a/controllers/statsController.js b/controllers/statsController.js
index 75ddfb9c..8292ddc9 100644
--- a/controllers/statsController.js
+++ b/controllers/statsController.js
@@ -2,28 +2,6 @@ const logger = require('winston');
 const db = require('../models');
 
 module.exports = {
-  getTrendingClaims (startDate) {
-    logger.debug('retrieving trending');
-    return new Promise((resolve, reject) => {
-      // get the raw requests data
-      db.getTrendingFiles(startDate)
-        .then(fileArray => {
-          let claimsPromiseArray = [];
-          if (fileArray) {
-            fileArray.forEach(file => {
-              claimsPromiseArray.push(db.Claim.resolveClaim(file.name, file.claimId));
-            });
-            return Promise.all(claimsPromiseArray);
-          }
-        })
-        .then(claimsArray => {
-          resolve(claimsArray);
-        })
-        .catch(error => {
-          reject(error);
-        });
-    });
-  },
   getRecentClaims () {
     logger.debug('retrieving most recent claims');
     return new Promise((resolve, reject) => {
diff --git a/models/index.js b/models/index.js
index 1e6b6352..81060166 100644
--- a/models/index.js
+++ b/models/index.js
@@ -1,82 +1,78 @@
-// const fs = require('fs');
-// const path = require('path');
 const Sequelize = require('sequelize');
-// const basename = path.basename(module.filename);
 const logger = require('winston');
-const config = require('../config/speechConfig.js');
-const { database, username, password } = config.sql;
-const db = {};
 
-// set sequelize options
-const sequelize = new Sequelize(database, username, password, {
-  host          : 'localhost',
-  dialect       : 'mysql',
-  dialectOptions: {decimalNumbers: true}, // fix to ensure DECIMAL will not be stored as a string
-  logging       : false,
-  pool          : {
-    max    : 5,
-    min    : 0,
-    idle   : 10000,
-    acquire: 10000,
-  },
-});
-
-sequelize
-  .authenticate()
-  .then(() => {
-    logger.info('Sequelize has established mysql connection successfully.');
-  })
-  .catch(err => {
-    logger.error('Sequelize was unable to connect to the database:', err);
+module.exports = (mysqlConfig) => {
+  const { database, username, password } = mysqlConfig;
+  const db = {};
+  // set sequelize options
+  const sequelize = new Sequelize(database, username, password, {
+    host          : 'localhost',
+    dialect       : 'mysql',
+    dialectOptions: {decimalNumbers: true}, // fix to ensure DECIMAL will not be stored as a string
+    logging       : false,
+    pool          : {
+      max    : 5,
+      min    : 0,
+      idle   : 10000,
+      acquire: 10000,
+    },
   });
 
-// manually add each model to the db
-const Certificate = require('./certificate.js');
-const Channel = require('./channel.js');
-const Claim = require('./claim.js');
-const File = require('./file.js');
-const Request = require('./request.js');
-const User = require('./user.js');
-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
-Object.keys(db).forEach(modelName => {
-  if (db[modelName].associate) {
-    logger.info('Associating model:', modelName);
-    db[modelName].associate(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);
-      }
+  // establish mysql connection
+  sequelize
+    .authenticate()
+    .then(() => {
+      logger.info('Sequelize has established mysql connection successfully.');
     })
-    .catch(function (error) {
-      logger.error(`${tableName}.upsert error`, error);
-      throw error;
+    .catch(err => {
+      logger.error('Sequelize was unable to connect to the database:', err);
     });
-};
 
-// add a 'getTrendingFiles' method to the db object.  note: change this to get claims directly.  might need new association between Request and Claim
-db.getTrendingFiles = (startDate) => {
-  return db.sequelize.query(`SELECT COUNT(*), File.* FROM Request LEFT JOIN File ON Request.FileId = File.id WHERE FileId IS NOT NULL AND nsfw != 1 AND trendingEligible = 1 AND Request.createdAt > "${startDate}" GROUP BY FileId ORDER BY COUNT(*) DESC LIMIT 25;`, { type: db.sequelize.QueryTypes.SELECT });
-};
+  // manually add each model to the db object
+  const Certificate = require('./certificate.js');
+  const Channel = require('./channel.js');
+  const Claim = require('./claim.js');
+  const File = require('./file.js');
+  const Request = require('./request.js');
+  const User = require('./user.js');
+  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);
 
-module.exports = db;
+  // run model.association for each model in the db object that has an association
+  Object.keys(db).forEach(modelName => {
+    if (db[modelName].associate) {
+      logger.info('Associating model:', modelName);
+      db[modelName].associate(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;
+      });
+  };
+
+  return db;
+};
diff --git a/package.json b/package.json
index c6d0dbce..428ebc75 100644
--- a/package.json
+++ b/package.json
@@ -6,8 +6,8 @@
   "scripts": {
     "test": "mocha --recursive",
     "test-all": "mocha --recursive",
-    "start": "node server.js",
-    "start-dev": "nodemon server.js",
+    "start": "node index.js",
+    "start-dev": "nodemon index.js",
     "lint": "eslint .",
     "fix": "eslint . --fix",
     "precommit": "eslint .",
diff --git a/index.js b/server.js
similarity index 73%
rename from index.js
rename to server.js
index 9303e671..7e0878ca 100644
--- a/index.js
+++ b/server.js
@@ -9,7 +9,6 @@ const logger = require('winston');
 const helmet = require('helmet');
 const PORT = 3000; // set port
 const app = express(); // create an Express application
-const db = require('./models'); // require our models for syncing
 const passport = require('passport');
 const cookieSession = require('cookie-session');
 
@@ -62,23 +61,34 @@ app.set('view engine', 'handlebars');
 app.use(populateLocalsDotUser);
 
 // start the server
-db.sequelize
-  .sync() // sync sequelize
-  .then(() => { // require routes
-    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/fallback-routes.js')(app);
-    const http = require('http');
-    return http.Server(app);
-  })
-  .then(server => { // start the server
-    server.listen(PORT, () => {
-      logger.info('Trusting proxy?', app.get('trust proxy'));
-      logger.info(`Server is listening on PORT ${PORT}`);
+const startServer = (mysqlConfig) => {
+  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/api-routes.js')(app);
+      require('./routes/page-routes.js')(app);
+      require('./routes/serve-routes.js')(app);
+      require('./routes/fallback-routes.js')(app);
+      const http = require('http');
+      return http.Server(app);
+    })
+    // start the server
+    .then(server => {
+      server.listen(PORT, () => {
+        logger.info('Trusting proxy?', app.get('trust proxy'));
+        logger.info(`Server is listening on PORT ${PORT}`);
+      });
+    })
+    .catch((error) => {
+      logger.error(`Startup Error:`, error);
     });
-  })
-  .catch((error) => {
-    logger.error(`Startup Error >> ${error.message}`, error);
-  });
+};
+
+module.exports = (config) => {
+  const { mysqlConfig } = config;
+  startServer(mysqlConfig);
+};
diff --git a/webpack.config.js b/webpack.config.js
new file mode 100644
index 00000000..efcdc0b7
--- /dev/null
+++ b/webpack.config.js
@@ -0,0 +1,7 @@
+const serverBaseConfig = require('./webpack.server.common.js');
+const clientBaseConfig = require('./webpack.client.common.js');
+
+module.exports = [
+  serverBaseConfig,
+  clientBaseConfig,
+];
diff --git a/webpack.server.common.js b/webpack.server.common.js
index e5627ffc..ab62e0ee 100644
--- a/webpack.server.common.js
+++ b/webpack.server.common.js
@@ -8,11 +8,11 @@ module.exports = {
     __dirname: false,
   },
   externals: [nodeExternals()],
-  entry    : ['babel-polyfill', 'whatwg-fetch', './index.js'],
+  entry    : ['babel-polyfill', 'whatwg-fetch', './server.js'],
   output   : {
     path      : Path.join(__dirname, '/'),
     publicPath: '/',
-    filename  : 'server.js',
+    filename  : 'index.js',
   },
   module: {
     rules: [