# all files / keystone/lib/core/ start.js

10.42% Statements 5/48
0% Branches 0/22
0% Functions 0/12
11.36% Lines 5/44

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101

    1×

    1×

    1×

    1×

    1×
     /**
     * Configures and starts a Keystone app in encapsulated mode.
     *
     * Connects to the database, runs updates and listens for incoming requests.
     *
     * Events are fired during initialisation to allow customisation, including:
     *
     *   - onMount
     *   - onStart
     *   - onHttpServerCreated
     *   - onHttpsServerCreated
     *
     * If the events argument is a function, it is assumed to be the started event.
     *
     * @api public
     */

    var async = require('async');

    var dashes = '\n------------------------------------------------\n';

    function start (events) {

    	if (typeof events === 'function') {
    		events = { onStart: events };
    	}
    	if (!events) events = {};

    	function fireEvent (name) {
    		if (typeof events[name] === 'function') {
    			events[name]();
    		}
    	}

    	process.on('uncaughtException', function (e) {
    		if (e.code === 'EADDRINUSE') {
    			console.log(dashes
    				+ keystone.get('name') + ' failed to start: address already in use\n'
    				+ 'Please check you are not already running a server on the specified port.\n');
    			process.exit();
    		} else {
    			console.log(e.stack || e);
    			process.exit(1);
    		}
    	});

    	this.initExpressApp();

    	var keystone = this;
    	var app = keystone.app;

    	this.openDatabaseConnection(function () {

    		fireEvent('onMount');

    		var ssl = keystone.get('ssl');
    		var unixSocket = keystone.get('unix socket');
    		var startupMessages = ['KeystoneJS Started:'];

    		async.parallel([
    			// HTTP Server
    			function (done) {
    				if (ssl === 'only' || unixSocket) return done();
    				require('../../server/startHTTPServer')(keystone, app, function (err, msg) {
    					fireEvent('onHttpServerCreated');
    					startupMessages.push(msg);
    					done(err);
    				});
    			},
    			// HTTPS Server
    			function (done) {
    				if (!ssl || unixSocket) return done();
    				require('../../server/startSecureServer')(keystone, app, function () {
    					fireEvent('onHttpsServerCreated');
    				}, function (err, msg) {
    					startupMessages.push(msg);
    					done(err);
    				});
    			},
    			// Unix Socket
    			function (done) {
    				if (!unixSocket) return done();
    				require('../../server/startSocketServer')(keystone, app, function (err, msg) {
    					fireEvent('onSocketServerCreated');
    					startupMessages.push(msg);
    					done(err);
    				});
    			},
    		], function serversStarted (err, messages) {
    			if (keystone.get('logger')) {
    				console.log(dashes + startupMessages.join('\n') + dashes);
    			}
    			fireEvent('onStart');
    		});
    	});

    	return this;
    }

    module.exports = start;