<commit_before>var sys = require ('sys'); // minor convenience :)

var jsgi = require('jsgi');

var Router    = require('./goo/router');
var Resource  = require('./goo/resource');

var config = {
  paths: {
    routes: '/config/routes',
    resources: '/app/resources'
  }
};

exports.app = function (cwd) {

  config.cwd = cwd;

  var routes = getRoutes();
  var resources = getResources();

  var router = Router.generate(routes, resources);

  return {
    routes: routes,
    start: function() {
      jsgi.start(router);
    }
  };

};

/* PRIVATE */

/*
* Returns collection of resources that are jsgi components
*/
var getResources = function () {
  var resources = {};
  var resourcePath = config.cwd + config.paths.resources;
  var filePath = "";
  var resourceName = "";
  var definition = {};

  // list all resources in resources directory
  var filenames = require('fs').readdirSync(resourcePath);

  // take each resource definition and generate corresponding jsgi component
  filenames.forEach(function(filename){
    filePath = resourcePath + "/" + filename;
    // take resource name from the filename (minus the '.js' extension)
    resourceName = filename.slice(0,-3);
    try {
      // take definition from the file
      definition = require(filePath);
      // generate jsgi component from resource definition
      resources[resourceName] = Resource.generate(definition.resource);
    } catch (err) {
      console.log("ERROR: Resource defined in " + filePath + " could not be initialized");
    }

  });
  sys.debug(sys.inspect(resources));
  return resources;
};

/*
* Returns routes defined in routes file (config/routes)
*/
var getRoutes = function () {
  routes = require(config.cwd + config.paths.routes).routes;
  sys.debug(sys.inspect(routes));
  return routes;
}<commit_msg>Change initialisation of resources<commit_after>var sys = require ('sys');

var jsgi = require('jsgi');

var Router    = require('./goo/router');
var Resource  = require('./goo/resource');

var config = {
  paths: {
    routes: '/config/routes',
    resources: '/app/resources'
  }
};

exports.app = function (cwd) {

  config.cwd = cwd;

  var routes = getRoutes();
  var resources = getResources(routes);

  var router = Router.generate(routes, resources);

  return {
    routes: routes,
    start: function() {
      jsgi.start(router);
    }
  };

};

/* PRIVATE */

/*
* Returns collection of resources that are jsgi components
*/
var getResources = function (routes) {
  var resources = {};
  var resourcesPath = config.cwd + config.paths.resources + '/';
  var resourceName = "";
  var definition = {}; 

  for(var route in routes) {
    try {
      resourceName = routes[route];
      definition = require(resourcesPath + resourceName);
      resources[resourceName] = Resource.generate(definition.resource);
    } catch (err) {
      console.log("WARNING: The route " + route + " does not point to a valid resource definition");
    }
  }

  return resources;
};

/*
* Returns routes defined in routes file (config/routes)
*/
var getRoutes = function () {
  return require(config.cwd + config.paths.routes).routes;
}
{'fraction_non_alphanumeric': 0.13692946058091288, 'fraction_numerical': 0.0006383657835939994, 'mean_word_length': 3.299039780521262, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3888571', 'n_tokens_mistral': 933, 'n_tokens_neox': 925, 'n_words': 255}