{"size": 2465, "ext": "js", "max_stars_repo_path": "service.js", "max_stars_repo_name": "RetrucLabs/party-games-microservice", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "service.js", "max_issues_repo_name": "RetrucLabs/party-games-microservice", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "service.js", "max_forks_repo_name": "RetrucLabs/party-games-microservice", "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": 30.8125, "max_line_length": 114, "alphanum_fraction": 0.6604462475}
#!/bin/env node

var express         = require('express');
var bodyParser      = require('body-parser');
var cookieParser    = require('cookie-parser');
var methodOverride  = require('method-override');
var morgan          = require('morgan');
var logger          = require('./lib/logger');
var config          = require('./config/config');

var server = module.exports = this;

var request = require('request');


server.setupNetworkConfig = function() {
  server.port = config.server.port;
  server.ip = config.server.ip;
};

server.setExpressApp = function(){
  server.express = express();
  server.express.use(bodyParser.json());       // get information from html forms
  server.express.use(methodOverride());        // simulate DELETE and PUT
  server.express.use(cookieParser());          // read cookies (needed for auth)
  server.express.use(morgan('tiny', { 'stream': logger.stream }));
};

server.setRoutes = function(){
  require('./controllers/index.js')(server.express);
};

server.terminatorHandler = function(signal){
  if (typeof signal === 'string') {
    console.log('%s: Received %s - terminating app ...', Date(Date.now()), signal);
    process.exit(1);
  }
  console.log('%s: Node server stopped.', Date(Date.now()) );
};

//  Process on exit and signals.
server.setupTerminationHandlers = function(){
  process.on('exit', server.terminatorHandler);
  var termSignals = ['SIGHUP', 'SIGINT', 'SIGQUIT', 'SIGILL', 'SIGTRAP', 'SIGABRT', 'SIGBUS', 'SIGFPE', 'SIGUSR1',
  'SIGSEGV', 'SIGUSR2', 'SIGTERM'];
  termSignals.forEach(function(element) {
    process.on(element, function() { server.terminatorHandler(element); });
  });
};

server.setup = function() {
  server.setupNetworkConfig();
  server.setExpressApp();
  server.setRoutes();
  server.setupTerminationHandlers();
};

server.start = function() {
  server.express.listen(server.port, server.ipaddress, function() {
    console.log('%s: Node server started on %s:%d ...', Date(Date.now()), server.ip, server.port);
  });
};

server.loadGames = function(){
  for(var gameUrlId in config.gameMicroservices){
    var gameUrl = config.gameMicroservices[gameUrlId]+'/description';
    request(gameUrl, function (error, response, body) {
      if (!error && response.statusCode == 200) {
        config.gameAboutList.push(JSON.parse(body));
      }else{
        console.log('Error loading gameInfo from: '+gameUrl);
      }
    });
  }
};

server.setup();
server.loadGames();
server.start();
