diff --git a/bin/www b/bin/www index af91c2c40..6ebfc6944 100755 --- a/bin/www +++ b/bin/www @@ -13,27 +13,31 @@ process.env.DEBUG = process.env.TALK_DEBUG; const app = require('../app'); const debug = require('debug')('talk:server'); const http = require('http'); +const initPromise = require('../init'); +let server; -/** - * Get port from environment and store in Express. - */ +initPromise.then(() => { + /** + * Get port from environment and store in Express. + */ -const port = normalizePort(process.env.TALK_PORT || '3000'); -app.set('port', port); + const port = normalizePort(process.env.TALK_PORT || '3000'); + app.set('port', port); -/** - * Create HTTP server. - */ + /** + * Create HTTP server. + */ -const server = http.createServer(app); + server = http.createServer(app); -/** - * Listen on provided port, on all network interfaces. - */ + /** + * Listen on provided port, on all network interfaces. + */ -server.listen(port); -server.on('error', onError); -server.on('listening', onListening); + server.listen(port); + server.on('error', onError); + server.on('listening', onListening); +}); /** * Normalize a port into a number, string, or false. diff --git a/init.js b/init.js new file mode 100644 index 000000000..9c3c2bda5 --- /dev/null +++ b/init.js @@ -0,0 +1,6 @@ +const Setting = require('./models/setting'); + +const defaults = {id: '1', moderation: 'pre'}; +module.exports = Setting.init(defaults); + +// presumably this file will grow, which is why I've broken it out. diff --git a/models/setting.js b/models/setting.js index 15c457307..a9b60abb8 100644 --- a/models/setting.js +++ b/models/setting.js @@ -11,6 +11,14 @@ const SettingSchema = new Schema({ } }); +/** + * this is run once when the app starts to ensure settings are populated + * @return {Promise} null initialize the global settings object + */ +SettingSchema.statics.init = function (defaults) { + return this.update({id: '1'}, {$setOnInsert: defaults}, {upsert: true}); +}; + /** * gets the entire settings record and sends it back * @return {Promise} settings the whole settings record