Merge pull request #55 from coralproject/init-on-app-startup

initialize settings in an init file before app accepts requests
This commit is contained in:
Riley Davis
2016-11-10 13:34:58 -07:00
committed by GitHub
3 changed files with 36 additions and 16 deletions
+22 -16
View File
@@ -13,27 +13,33 @@ process.env.DEBUG = process.env.TALK_DEBUG;
const app = require('../app');
const debug = require('debug')('talk:server');
const http = require('http');
/**
* Get port from environment and store in Express.
*/
const initPromise = require('../init');
const port = normalizePort(process.env.TALK_PORT || '3000');
app.set('port', port);
/**
* Create HTTP server.
*/
let server;
const server = http.createServer(app);
initPromise
.then(() => {
/**
* Get port from environment and store in Express.
*/
/**
* Listen on provided port, on all network interfaces.
*/
app.set('port', port);
server.listen(port);
server.on('error', onError);
server.on('listening', onListening);
/**
* Create HTTP server.
*/
server = http.createServer(app);
/**
* Listen on provided port, on all network interfaces.
*/
server.listen(port);
server.on('error', onError);
server.on('listening', onListening);
});
/**
* Normalize a port into a number, string, or false.
+6
View File
@@ -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.
+8
View File
@@ -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