Permalink
Browse files

Enabled auto-loading.

Services and Controllers are now loaded by convention and attached to the app.
  • Loading branch information...
1 parent 98accd7 commit aca29e89fe07a2623a06465f8d0b05a5d9dfe50c @dave-elkan committed Mar 28, 2011
View
24 app.js
@@ -9,14 +9,8 @@ var express = require('express'),
ObjectId = require('mongodb').ObjectId,
Server = require('mongodb').Server,
GenericPool = require('generic-pool'),
-
notFoundHandler = require("./lib/middleware/NotFoundHandler"),
- BookService = require("./lib/service/BookService"),
- BookController = require('./lib/controller/BookController'),
- AuthorService = require("./lib/service/AuthorService"),
- AuthorController = require('./lib/controller/AuthorController'),
- HomeService = require("./lib/service/HomeService"),
- HomeController = require('./lib/controller/HomeController');
+ loader = require("./lib/loader");
app.pool = GenericPool.Pool({
max: 10,
@@ -54,28 +48,20 @@ app.configure('production', function(){
});
/**
- * Services.
+ * Services and Controllers.
*
* These services are instantiated here as singletons and attached to the app object.
* This prevents the need for services to instantiate new instances of other services
* (as they can inter-dependent) which would potentially cause circular issues.
*/
-app.bookService = new BookService(app);
-app.authorService = new AuthorService(app);
-app.homeService = new HomeService(app);
+loader(app, "services");
+loader(app, "controllers");
// Bootstrap Content
require("./lib/bootstrap")(app);
-// Routes
-new BookController(app);
-new AuthorController(app);
-new HomeController(app);
-
-
// Only listen on $ node app.js
-
if (!module.parent) {
app.listen(3000);
console.log("Express server listening on port %d", app.address().port)
-}
+}
@@ -1,10 +1,10 @@
var ErrorView = require("../view/ErrorView");
-Controller = function() {
+AbstractController = function() {
this.errorView = new ErrorView();
};
-Controller.prototype = function() {};
+AbstractController.prototype = function() {};
/**
* Convenience function to set up a GET route.
@@ -13,7 +13,7 @@ Controller.prototype = function() {};
* @param path {String} The path of the route to bind.
* @param params {Object} The parameters map
*/
-Controller.prototype.setupGetRoute = function(app, path, params) {
+AbstractController.prototype.setupGetRoute = function(app, path, params) {
var self = this;
if (!path) {
throw new Error("No Path specified when creating GET route.");
@@ -52,4 +52,4 @@ Controller.prototype.setupGetRoute = function(app, path, params) {
});
};
-module.exports = Controller;
+module.exports = AbstractController;
@@ -1,4 +1,4 @@
-var Controller = require('./Controller'),
+var AbstractController = require('./AbstractController'),
AuthorIndexView = require('../view/author/AuthorIndexView'),
AuthorView = require('../view/author/AuthorView'),
JsonView = require('../view/JsonView');
@@ -32,7 +32,7 @@ AuthorController = function(app) {
});
};
-AuthorController.prototype = new Controller();
+AuthorController.prototype = new AbstractController();
AuthorController.prototype.getAuthorList = function(req, res, callback) {
this.authorService.getList(callback);
@@ -1,4 +1,4 @@
-var Controller = require('./Controller'),
+var AbstractController = require('./AbstractController'),
BookIndexView = require('../view/book/BookIndexView'),
BookView = require('../view/book/BookView'),
JsonView = require('../view/JsonView');
@@ -33,7 +33,7 @@ BookController = function(app) {
});
};
-BookController.prototype = new Controller();
+BookController.prototype = new AbstractController();
BookController.prototype.getBookList = function(req, res, callback) {
this.bookService.getList(callback);
@@ -1,4 +1,4 @@
-var Controller = require('./Controller'),
+var AbstractController = require('./AbstractController'),
HomeView = require('../view/HomeView'),
jsonView = require('../view/JsonView');
@@ -20,7 +20,7 @@ HomeController = function(app) {
});
};
-HomeController.prototype = new Controller();
+HomeController.prototype = new AbstractController();
HomeController.prototype.getAuthorAndBookList = function(req, res, callback) {
this.homeService.getAuthorAndBookList(callback);
View
@@ -0,0 +1,28 @@
+var fs = require('fs');
+
+function loader(app, namespace) {
+
+ var loaded = [],
+ path = __dirname + '/' + namespace;
+
+ function getClassName(file) {
+ return file.substr(0, file.lastIndexOf(".js"));
+ }
+
+ function getInstanceName(className) {
+ return className[0].toLowerCase() + className.substr(1);
+ }
+
+ var files = fs.readdirSync(path);
+
+ files.forEach(function(file, i) {
+ if (file.indexOf("Abstract") == -1) {
+ var className = getClassName(file),
+ clazz = require(path + "/" + className);
+
+ app[getInstanceName(className)] = new clazz(app);
+ }
+ });
+}
+
+module.exports = loader
@@ -1,18 +1,18 @@
-Service = function() {};
+AbstractService = function() {};
-Service.prototype = function(){};
+AbstractService.prototype = function(){};
/**
* Generic function to create an entry or list of entries for this type.
*/
-Service.prototype.create = function(items, callback) {
+AbstractService.prototype.create = function(items, callback) {
this.dao.create(items, callback);
};
/**
* Generic function to return a list of entries for this type.
*/
-Service.prototype.getList = function(callback) {
+AbstractService.prototype.getList = function(callback) {
var params = {
options: {
limit: 20
@@ -24,8 +24,8 @@ Service.prototype.getList = function(callback) {
/**
* Generic function to return an entry for this type by key.
*/
-Service.prototype.getItemByKey = function(key, callback) {
+AbstractService.prototype.getItemByKey = function(key, callback) {
this.dao.getItemByKey(key, callback);
};
-module.exports = Service;
+module.exports = AbstractService;
@@ -1,12 +1,12 @@
-var Service = require('../service/Service'),
+var AbstractService = require('./AbstractService'),
Dao = require('../dao/Dao');
AuthorService = function(app) {
this.app = app;
this.dao = new Dao(app.pool, "author");
};
-AuthorService.prototype = new Service();
+AuthorService.prototype = new AbstractService();
/**
* Retrieves an Author and a list of their Books.
@@ -1,12 +1,12 @@
-var Service = require('../service/Service'),
+var AbstractService = require('./AbstractService'),
Dao = require('../dao/Dao');
BookService = function(app) {
this.dao = new Dao(app.pool, "book");
this.app = app;
};
-BookService.prototype = new Service();
+BookService.prototype = new AbstractService();
/**
* Retrieves a list of Books by Author.
@@ -1,10 +1,10 @@
-var Service = require('../service/Service');
+var AbstractService = require('./AbstractService');
HomeService = function(app) {
this.app = app;
};
-HomeService.prototype = new Service();
+HomeService.prototype = new AbstractService();
/**
* Returns a list of all Authors and their Books.
@@ -1,7 +1,7 @@
var express = require('express'),
connect = require('connect'),
assert = require('assert'),
- AuthorController = require('../lib/controller/AuthorController');
+ AuthorController = require('../lib/controllers/AuthorController');
function configureServer(server) {
server.configure(function(){
@@ -1,7 +1,7 @@
var express = require('express'),
connect = require('connect'),
assert = require('assert'),
- AuthorController = require('../lib/controller/AuthorController');
+ AuthorController = require('../lib/controllers/AuthorController');
function configureServer(server) {
server.configure(function(){
View
@@ -3,7 +3,7 @@ var express = require('express'),
assert = require('assert'),
JsonView = require('../lib/view/JsonView'),
TestView = require('./lib/view/TestView'),
- Controller = require('../lib/controller/Controller');
+ Controller = require('../lib/controllers/AbstractController');
function configureServer(server) {
server.configure(function(){
View
@@ -1,4 +1,4 @@
-var HomeService = require("../lib/service/HomeService"),
+var HomeService = require("../lib/services/HomeService"),
assert = require('assert');
exports["TestHomeServiceAttemptsToGetAListOfAuthorsAndBooks"] = function() {

0 comments on commit aca29e8

Please sign in to comment.