'use strict';

/*
 * base.js
 * Base server module
 *
 * Handles server instantiation, route initialization, and startup
 */

var hapi = require('hapi'),
    routes = require('./routes');

module.exports = function (configuration) {
    var log = configuration && configuration.log,
        port = configuration && configuration.port,
        server;

    server = hapi.createServer(port);

    // Send Hapi server log events to bunyan
    server.on('log', function (event, tags) {
        if (tags.error) {
            log.error(event);
            return;
        }

        log.info(event);
    });

    server.log(['routes'], 'Initializing routes');
    // We pass in the configuration object to the route table in case there's
    // need for data sources (databases, remote APIs) defined therein.
    server.route(routes(configuration));

    server.start(function () {
        log.info('HTTP server listening on port ' + port);
    });
};
