var hapi = require('hapi');
var q = require('q');
var server = new hapi.Server();
server.connection({port: process.env.PORT});

function enableCookie(server) {
  return q.fcall(function () {
    server.state('player', {
      encoding: 'base64json'
    });

    return server;
  });
}

function registerRoutes(server) {
  var register = q.nbind(server.register, server);

  return register({
    register: require('./routes').status
  }, {
    routes: {
      prefix: '/api/status'
    }
  })
  .then(function () {
    return register({
      register: require('./routes').games
    }, {
      routes: {
        prefix: '/api/games'
      }
    });
  })
  .then(function () {
    server.route({
      method: 'GET',
      path: '/{filename*}',
      handler: {
        directory: {
          path: 'public'
        }
      }
    });

    return server;
  });
}

module.exports = function () {
  var promise = q.defer();

  registerRoutes(server)
  .then(enableCookie)
  .then(function () {
    server.start(function serverStarted() {
      console.log('Server running at: ', server.info.uri);
      promise.resolve(server);
    });
  })
  .done();

  return promise.promise;
};

@@ -39,24 +39,58 @@
 quire('q');%0A
+var routes = require('./routes');%0A
 var server =
@@ -412,127 +412,78 @@
 urn 
-register(%7B%0A    register: require('./routes').status%0A  %7D, %7B%0A    routes:
+q.keys(routes)%0A  .then(function (endpoints)
  %7B%0A    
-  
-p
 re
-fix: '/api/status'%0A    %7D%0A  %7D)%0A  .then
+turn endpoints.map
 (fun
@@ -485,28 +485,38 @@
 p(function (
+endpoint
 ) %7B%0A
+  
     return r
@@ -531,16 +531,18 @@
 %7B%0A      
+ 
+ 
 register
@@ -548,33 +548,26 @@
 r: r
-equire('./routes').games%0A
+outes%5Bendpoint%5D%0A  
     
@@ -577,16 +577,18 @@
 %7B%0A      
+  
 routes: 
@@ -585,24 +585,26 @@
   routes: %7B%0A
+  
         pref
@@ -617,28 +617,119 @@
 api/
-games'%0A
+' + endpoint%0A 
       
+ 
 %7D%0A    
-%7D
+  %7D);%0A    %7D);%0A  %7D)%0A  .then(function (registrations) %7B%0A    return q.all(registrations
 );%0A 
@@ -903,32 +903,58 @@
       %7D%0A    %7D);%0A
+  %7D)%0A  .then(function () %7B
 %0A    return serv

refactor route registration