2015-05-05 11:56:35 +02:00
|
|
|
/**
|
|
|
|
* Module dependencies.
|
|
|
|
*/
|
|
|
|
|
|
|
|
var express = require('express');
|
|
|
|
var fs = require('fs');
|
|
|
|
|
|
|
|
module.exports = function(parent, options){
|
|
|
|
var verbose = options.verbose;
|
2015-06-25 17:21:19 +02:00
|
|
|
|
2015-07-01 10:14:15 +02:00
|
|
|
if (verbose)
|
|
|
|
console.log("Loading controllers :");
|
2015-06-25 17:21:19 +02:00
|
|
|
|
2015-05-19 18:09:46 +02:00
|
|
|
fs.readdirSync(__dirname + '/../controllers').forEach(function(name){
|
2015-05-19 11:03:53 +02:00
|
|
|
|
|
|
|
// index.js in controller, with function as pages (views.py for django)
|
2015-05-19 18:09:46 +02:00
|
|
|
var obj = require('./../controllers/' + name + '/index');
|
2015-05-19 11:03:53 +02:00
|
|
|
|
|
|
|
// urls.js, just like django urls.py
|
2015-05-19 18:09:46 +02:00
|
|
|
var urls = require('./../controllers/' + name + '/urls');
|
2015-07-01 10:14:15 +02:00
|
|
|
name = obj.name || name;
|
2015-05-19 11:03:53 +02:00
|
|
|
var app = express();
|
|
|
|
|
|
|
|
// allow specifying the view engine
|
|
|
|
if (obj.engine) app.set('view engine', obj.engine);
|
2015-05-19 18:09:46 +02:00
|
|
|
app.set('views', __dirname + '/../controllers/' + name + '/views');
|
2015-05-19 11:03:53 +02:00
|
|
|
|
|
|
|
// generate routes based
|
|
|
|
// on the exported methods
|
|
|
|
|
2015-07-01 10:14:15 +02:00
|
|
|
if (verbose)
|
|
|
|
console.log('\t' + name + ':');
|
|
|
|
|
2015-05-19 11:03:53 +02:00
|
|
|
for (var key in urls) {
|
2015-05-19 18:09:46 +02:00
|
|
|
app.get(key, obj[urls[key]]);
|
2015-07-01 10:14:15 +02:00
|
|
|
|
|
|
|
if (verbose)
|
|
|
|
console.log('\t\t' + key + ' -> ' + name + '.' + urls[key]);
|
|
|
|
|
2015-05-19 11:03:53 +02:00
|
|
|
}
|
2015-07-01 10:14:15 +02:00
|
|
|
|
|
|
|
if (verbose)
|
|
|
|
console.log();
|
2015-05-19 11:03:53 +02:00
|
|
|
|
|
|
|
// mount the app
|
|
|
|
parent.use(app);
|
|
|
|
});
|
2015-05-05 11:56:35 +02:00
|
|
|
};
|