{"size": 922, "ext": "js", "max_stars_repo_path": "api/server/server.js", "max_stars_repo_name": "dashawk/hapi-api", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "api/server/server.js", "max_issues_repo_name": "dashawk/hapi-api", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "api/server/server.js", "max_forks_repo_name": "dashawk/hapi-api", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 20.0434782609, "max_line_length": 60, "alphanum_fraction": 0.6084598698}
'use strict';

const Hapi = require('hapi');
const plugins = require('./modules/plugins');
const Routes = require('./core/routes');
const db = require('./config/database');

const start = (host, port) => {

	return new Promise((resolve, reject) => {

		// Initialize the server
		const server = new Hapi.Server();
		server.connection({ host, port, routes: { cors: true } });

		// Register all plugins
		server.register(plugins, (error) => {
			if (error) {
				console.log(error);
				return reject(error);
			}

			// Initialize Routes
			server.route(Routes.endpoints);

			// Start listening for requests
			server.start((error) => {
				if (error) {
					console.log(error);
					return reject(error);
				}

				console.log(`Server running at: ${server.info.uri}`);
				resolve();
			});

			server.on('request-error', (request, error) => {
				console.log(error);
			});

		});

	});
};

module.exports = { start };