Skip to content

Commit

Permalink
Refactored helpers to clean up core library
Browse files Browse the repository at this point in the history
  • Loading branch information
cliftonc committed Apr 13, 2012
1 parent 56194d6 commit 0a562e1
Show file tree
Hide file tree
Showing 6 changed files with 298 additions and 281 deletions.
122 changes: 67 additions & 55 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,64 +49,76 @@ function bootApplication(next) {
// Load configuration
var Config = calipso.config; //require(path + "/lib/core/Config").Config;
app.config = new Config();
app.config.init();

app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser());
app.use(express.responseTime());

// Create dummy session middleware - tag it so we can later replace
var temporarySession = express.session({ secret: "keyboard cat" });
temporarySession.tag = "session";
app.use(temporarySession);

// Default Theme
calipso.defaultTheme = app.config.get('themes:default');

// Create holders for theme dependent middleware
// These are here because they need to be in the connect stack before the calipso router
// THese helpers are re-used when theme switching.
app.mwHelpers = {};

// Load placeholder, replaced later
if(app.config.get('libraries:stylus:enabled')) {
app.mwHelpers.stylusMiddleware = function (themePath) {
var mw = stylus.middleware({
src: themePath + '/stylus',
dest: themePath + '/public',
debug: false,
compile: function (str, path) { // optional, but recommended
return stylus(str)
.set('filename', path)
.set('warn', app.config.get('libraries:stylus:warn'))
.set('compress', app.config.get('libraries:stylus:compress'));
}
});
mw.tag = 'theme.stylus';
app.config.init(function(err) {

if(err) return console.error(err.message);

// Default Theme
calipso.defaultTheme = app.config.get('themes:default');

app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser());
app.use(express.responseTime());

// Create dummy session middleware - tag it so we can later replace
var temporarySession = app.config.get('installed') ? {} : express.session({ secret: "installing calipso is great fun" });
temporarySession.tag = "session";
app.use(temporarySession);

// Create holders for theme dependent middleware
// These are here because they need to be in the connect stack before the calipso router
// THese helpers are re-used when theme switching.
app.mwHelpers = {};

// Load placeholder, replaced later
if(app.config.get('libraries:stylus:enabled')) {
app.mwHelpers.stylusMiddleware = function (themePath) {
var mw = stylus.middleware({
src: themePath + '/stylus',
dest: themePath + '/public',
debug: false,
compile: function (str, path) { // optional, but recommended
return stylus(str)
.set('filename', path)
.set('warn', app.config.get('libraries:stylus:warn'))
.set('compress', app.config.get('libraries:stylus:compress'));
}
});
mw.tag = 'theme.stylus';
return mw;
};
app.use(app.mwHelpers.stylusMiddleware(''));
}
// Static
app.mwHelpers.staticMiddleware = function (themePath) {
var mw = express["static"](themePath + '/public', {maxAge: 86400000});
mw.tag = 'theme.static';
return mw;
};
app.use(app.mwHelpers.stylusMiddleware(''));
}
// Static
app.mwHelpers.staticMiddleware = function (themePath) {
var mw = express["static"](themePath + '/public', {maxAge: 86400000});
mw.tag = 'theme.static';
return mw;
};
// Load placeholder, replaced later
app.use(app.mwHelpers.staticMiddleware(''));

// Core static paths
app.use(express["static"](path + '/media', {maxAge: 86400000}));
app.use(express["static"](path + '/lib/client/js', {maxAge: 86400000}));

// Translation - after static, set to add mode if appropriate
app.use(translate.translate(app.config.get('i18n:language'), app.config.get('i18n:languages'), app.config.get('i18n:additive')));

// Core calipso router
app.use(calipso.calipsoRouter(app, function() { next(app) }));
// Load placeholder, replaced later
app.use(app.mwHelpers.staticMiddleware(''));

// Core static paths
app.use(express["static"](path + '/media', {maxAge: 86400000}));
app.use(express["static"](path + '/lib/client/js', {maxAge: 86400000}));

// Translation - after static, set to add mode if appropriate
app.use(translate.translate(app.config.get('i18n:language'), app.config.get('i18n:languages'), app.config.get('i18n:additive')));

// Core calipso router
calipso.init(app, function() {

// Add the calipso mw
app.use(calipso.routingFn());

// return our app refrerence
next(app);

})

});

}

/**
Expand Down
61 changes: 31 additions & 30 deletions lib/calipso.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,30 +37,22 @@ var rootpath = process.cwd() + '/',
// Core object
var calipso = module.exports = {

// View Helpers
getDynamicHelpers: function(req, res) {
req.helpers = {};
for(var helper in this.helpers) {
req.helpers[helper] = this.helpers[helper](req, res, this);
}
},

// Router and initialisation
routingFn: routingFn,
init: init,

// Configuration exposed
reloadConfig: reloadConfig,

// Core objects - themes, data, modules
theme: {},
data: {},
modules: {},

// Express Router
calipsoRouter: router
modules: {}

};

// Load libraries in the core folder
loadCore(calipso);

function loadCore(calipso) {

fs.readdirSync(rootpath + 'lib/core').forEach(function(library){
Expand All @@ -70,41 +62,49 @@ function loadCore(calipso) {
});

}

module.exports.loaded = true;

/**
* Core router and initialisation function.
*
* Returns a connect middleware function that manages the roucting
* of requests to modules.
/**
* Calipso initialisation
*/
function router(app, initCallback) {

function init(app, initCallback) {
calipso.app = app;

// Load the calipso package.json into about
// Load the calipso package.json into app.about
calipso.module.loadAbout(app, rootpath, 'package.json');

calipso.config = app.config;

// Configure the cache
calipso.cacheService = calipso.cache.Cache({ttl:calipso.config.get('performance:cache:ttl')});

// Store the callback function for later
calipso.initCallback = function() {
initCallback();
};

// Configure the cache
calipso.cacheService = calipso.cache.Cache({ttl:calipso.config.get('performance:cache:ttl')});

// Create our calipso event emitter
calipso.e = new calipso.event.CalipsoEventEmitter();

// Load configuration
initialiseCalipso();

}

/**
* Core router function.
*
* Returns a connect middleware function that manages the roucting
* of requests to modules.
*
* Expects Calipso to be initialised.
*/
function routingFn() {

// Return the function that manages the routing
// Ok being non-synchro
return function(req,res,next) {
return function(req, res, next) {

// Default menus and blocks for each request
// More of these can be added in modules, these are jsut the defaults
Expand All @@ -120,12 +120,13 @@ function router(app, initCallback) {
res.client = new Client();

// Initialise helpers - first pass
calipso.getDynamicHelpers(req, res);
calipso.helpers.getDynamicHelpers(req, res, calipso);

// Route the modules
calipso.module.eventRouteModules(req, res, next);

};

}

/**
Expand Down Expand Up @@ -206,11 +207,11 @@ function loadThemes(next) {

var themeBasePath = calipso.config.get('server:themePath');

calipso.lib.fs.readdirSync(calipso.lib.path.join(calipso.app.path,themeBasePath)).forEach(function(folder){
calipso.lib.fs.readdirSync(calipso.lib.path.join(rootpath,themeBasePath)).forEach(function(folder){

if(folder != "README" && folder != '.DS_Store') {

var themes = calipso.lib.fs.readdirSync(calipso.lib.path.join(calipso.app.path,themeBasePath,folder));
var themes = calipso.lib.fs.readdirSync(calipso.lib.path.join(rootpath,themeBasePath,folder));

// First scan for legacy themes
var legacyTheme = false;
Expand All @@ -226,7 +227,7 @@ function loadThemes(next) {
themes.forEach(function(theme) {

if(theme != "README" && theme != '.DS_Store')
var themePath = calipso.lib.path.join(calipso.app.path,themeBasePath,folder,theme);
var themePath = calipso.lib.path.join(rootpath,themeBasePath,folder,theme);
// Create the theme object
calipso.availableThemes[theme] = {
name: theme,
Expand Down
Loading

0 comments on commit 0a562e1

Please sign in to comment.