'use strict';

import http from 'http';

import Promise from 'bluebird';

import * as dispatcher from './dispatcher';
import * as config from './config';

export default {
    start(options, settings) {

        const { appSettings, routes } = config.init(settings);

        return startServer(options, appSettings, routes);
    }
}

function startServer(options, appSettings, routes) {

    return new Promise((resolve, reject) => {
        
        options = options || {};

        const listener = (request, response) => dispatcher.dispatch(appSettings, routes, request, response);
        const server = http.createServer(listener);

        const port = options.port
            || process.env.PORT
            || (appSettings.environment.toLowerCase() == 'production' ? 80 : 3000);

        server.listen(port, options.hostname, options.backlog, () => {

            console.log('listening ...');
            console.log();
            console.log('Port:', port);
            console.log('Process Id:', process.pid);

            // process.on('uncaughtException', (err) => {
            // 	if (!err.rejected) {
            // 		console.log(err.stack);
            // 	}
            // });

            resolve(server);
        });
    });
}

'use strict';

import cluster from 'cluster';
import http from 'http';
import os from 'os';

import Promise from 'bluebird';

import * as dispatcher from './dispatcher';
import * as config from './config';

export default {
    start(options, settings) {

        const { appSettings, routes } = config.init(settings);

        return startServer(options, appSettings, routes);
    }
}

function startServer(options, appSettings, routes) {
    
    options = options || {};
    
    const port = options.port
        || process.env.PORT
        || (appSettings.environment.toLowerCase() == 'production' ? 80 : 3000);
        
    const hostname = options.hostname;
    const backlog = options.backlog;
    
    const numCPUs = os.cpus().length;
    
    if (cluster.isMaster) {

        for (let i = 0; i < numCPUs; i++) {
            cluster.fork();
        }
        
        cluster.on('exit', function(worker, code, signal) {
            cluster.fork();
        });
    } else {
       
        const listener = (request, response) => dispatcher.dispatch(appSettings, routes, request, response);
        const server = http.createServer(listener);

        server.listen(port, hostname, backlog); 
    }
}

Use the node's cluster module
Use the node's cluster module
