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;
}

@@ -23,32 +23,8 @@
 s');
- // minor convenience :)
 %0A%0Ava
@@ -343,16 +343,22 @@
 sources(
+routes
 );%0A%0A  va
@@ -607,24 +607,30 @@
 = function (
+routes
 ) %7B%0A  var re
@@ -653,24 +653,25 @@
 var resource
+s
 Path = confi
@@ -704,29 +704,14 @@
 rces
-;%0A  var filePath = %22%22
+ + '/'
 ;%0A  
@@ -759,291 +759,55 @@
  %7B%7D;
+ 
 %0A%0A  
-// list all resources in resources directory%0A  var filenames = require('fs').readdirSync(resourcePath);%0A%0A  // take each resource definition and generate corresponding jsgi component%0A  filenames.forEach(function(filename)%7B%0A    filePath = resourcePath + %22/%22 + filename;%0A    // take
+for(var route in routes) %7B%0A    try %7B%0A     
  res
@@ -815,245 +815,87 @@
 urce
- n
+N
 ame 
-from the filename (minus the '.js' extension)%0A    resourceName = filename.slice(0,-3);%0A    try %7B%0A      // take definition from the file%0A      definition = require(filePath);%0A      // generate jsgi component from resource definition
+= routes%5Broute%5D;%0A      definition = require(resourcesPath + resourceName);
 %0A   
@@ -1006,76 +1006,86 @@
 og(%22
-ERROR: Resource defined in %22 + filePath + %22 could not be initialized
+WARNING: The route %22 + route + %22 does not point to a valid resource definition
 %22);%0A
@@ -1094,51 +1094,12 @@
   %7D%0A
-%0A
   %7D
-);%0A  sys.debug(sys.inspect(resources));
+%0A
 %0A  r
@@ -1214,23 +1214,21 @@
 () %7B%0A  r
-outes =
+eturn
  require
@@ -1274,57 +1274,6 @@
 es;%0A
-  sys.debug(sys.inspect(routes));%0A  return routes;%0A
 %7D%0A

Change initialisation of resources