-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
John Le Drew
committed
Dec 4, 2011
1 parent
8b08725
commit d17fb8c
Showing
9 changed files
with
301 additions
and
39 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
var Base = require('../'), | ||
connect = require('connect'); | ||
|
||
var base = new Base(); | ||
|
||
connect( | ||
connect.static(__dirname + '/public'), | ||
connect.favicon(), | ||
base.connect | ||
).listen(3000); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"app" : "fred", | ||
"test1" : "t1", | ||
"test2" : "t2" | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
exports.index = function(req,res,callback) { | ||
callback({ | ||
'title' : 'Hello world!' | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<h1>${title}</h1> | ||
<p>Hello! You have successfully run the Base Framework example.</p> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<!doctype html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8"> | ||
<title>${title}</title> | ||
|
||
<link href='http://fonts.googleapis.com/css?family=Lobster|Comfortaa' rel='stylesheet' type='text/css'> | ||
|
||
<style> | ||
html { | ||
height: 100%; | ||
} | ||
|
||
body { | ||
overflow: hidden; | ||
height: 100%; | ||
background: -moz-linear-gradient(top, rgba(0,0,0,0.65) 0%, rgba(0,0,0,0) 100%); /* FF3.6+ */ | ||
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(0,0,0,0.65)), color-stop(100%,rgba(0,0,0,0))); /* Chrome,Safari4+ */ | ||
background: -webkit-linear-gradient(top, rgba(0,0,0,0.65) 0%,rgba(0,0,0,0) 100%); /* Chrome10+,Safari5.1+ */ | ||
background: -o-linear-gradient(top, rgba(0,0,0,0.65) 0%,rgba(0,0,0,0) 100%); /* Opera 11.10+ */ | ||
background: -ms-linear-gradient(top, rgba(0,0,0,0.65) 0%,rgba(0,0,0,0) 100%); /* IE10+ */ | ||
background: linear-gradient(top, rgba(0,0,0,0.65) 0%,rgba(0,0,0,0) 100%); /* W3C */ | ||
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#a6000000', endColorstr='#00000000',GradientType=0 ); /* IE6-9 */ | ||
} | ||
|
||
h1 { | ||
margin: 0px 0px 30px 0px; | ||
font-family: 'Lobster', cursive; | ||
text-align: center; | ||
font-size: 70px; | ||
text-shadow: 2px 2px 3px #999; | ||
filter: dropshadow(color=#999, offx=2, offy=2); | ||
} | ||
|
||
p { | ||
font-family: 'Comfortaa', cursive; | ||
text-align: center; | ||
font-size: 25px; | ||
text-shadow: 1px 1px 2px #999; | ||
filter: dropshadow(color=#999, offx=1, offy=1); | ||
margin: 0; | ||
} | ||
|
||
div[role="main"] { | ||
background: #fff; | ||
margin: 250px auto; | ||
width: 400px; | ||
border: 1px solid rgba(0,0,0,.5); | ||
border-radius: 10px; | ||
-webkit-box-shadow: 0px 0px 100px 30px rgba(0, 0, 0, .75); | ||
-moz-box-shadow: 0px 0px 100px 30px rgba(0, 0, 0, .75); | ||
box-shadow: 0px 0px 100px 30px rgba(0, 0, 0, .75); | ||
} | ||
|
||
div[role="main"] div { | ||
border: 1px solid rgba(0,0,0,.5); | ||
border-radius: 10px; | ||
-webkit-box-shadow: inset 0px 0px 100px 30px rgba(0, 0, 0, .75); | ||
-moz-box-shadow: inset 0px 0px 100px 30px rgba(0, 0, 0, .75); | ||
box-shadow: inset 0px 0px 100px 30px rgba(0, 0, 0, .75); | ||
padding: 25px; | ||
} | ||
|
||
</style> | ||
</head> | ||
|
||
<body> | ||
|
||
<div role="main"><div>{{html content}}</div></div> | ||
|
||
</body> | ||
</html> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,191 @@ | ||
module.exports = (function() { | ||
|
||
// Dependencies | ||
var fs = require('fs'), | ||
configr = require('configr').create(), | ||
twister = require('twister').create(), | ||
slicer = require('slicer').create(), | ||
_ = require('underscore'), | ||
async = require('async'), | ||
EventEmitter = require('events').EventEmitter, | ||
util = require('util'), | ||
path = require('path'), | ||
ready = false, | ||
engine = null, | ||
renderer = null, | ||
|
||
// Register supported template engines | ||
template_engines = { | ||
'jqtpl' : { | ||
'init' : function() { | ||
return require('jqtpl'); | ||
}, | ||
'render' : function(template, tdata, callback) { | ||
callback(null,engine.tmpl(template, tdata)); | ||
} | ||
} | ||
}; | ||
|
||
// Private members | ||
var options; | ||
|
||
// Constructor | ||
function Base(opt) { | ||
var that = this; | ||
opt = opt || {}; | ||
|
||
console.log("Initializing Base..."); | ||
options = _.defaults(opt,{ | ||
'root' : path.resolve(path.dirname(process.argv[0])), | ||
'config' : '/config', | ||
'controllers' : '/controllers', | ||
'views' : '/views', | ||
'models' : '/models' | ||
}); | ||
|
||
configr.setDirectory(options.root + options.config); | ||
|
||
console.log("Loading the configuration in " + options.root + options.config); | ||
configr.load(function() { | ||
twister.addRules(configr.get().routes || []); | ||
|
||
slicer.addSegmentIdentifier('controller','index'); | ||
slicer.addSegmentIdentifier('action','index'); | ||
|
||
options.template_engine = (configr.get().general && configr.get().general.template_engine) ? configr.get().general.template_engine : 'jqtpl'; | ||
engine = template_engines[options.template_engine].init(); | ||
renderer = template_engines[options.template_engine].render; | ||
|
||
ready = true; | ||
that.emit('ready'); | ||
|
||
console.log("Base Framework is ready to accept requests!"); | ||
}); | ||
} | ||
|
||
// Extend EventEmitter | ||
util.inherits(Base, EventEmitter); | ||
|
||
Base.prototype.addTemplateEngine = function(name,def) { | ||
template_engines[name] = def; | ||
} | ||
|
||
Base.prototype.getOptions = function() { | ||
return options; | ||
} | ||
|
||
Base.prototype.connect = function(req,res,next) { | ||
var that = this; | ||
|
||
function ifReady() { | ||
if (ready) return whenReady(); | ||
process.nextTick(ifReady); | ||
} | ||
|
||
process.nextTick(ifReady); | ||
|
||
function whenReady() { | ||
|
||
var tdata = {}; | ||
|
||
async.series({ | ||
'twister' : function(callback) { | ||
twister.twist(req.url,function(twisted) { | ||
req.url = twisted; | ||
callback(null); | ||
}); | ||
}, | ||
'splicer' : function(callback) { | ||
var sl = slicer.slice(req.url); | ||
req.controller = sl.controller; | ||
req.action = sl.action; | ||
req.params = sl.uri; | ||
callback(null); | ||
}, | ||
'controller' : function(callback) { | ||
var controller, action; | ||
var target = options.root + options.controllers + '/' + req.controller + '.js'; | ||
|
||
fs.stat(target, function(err) { | ||
if (err) return callback(null); | ||
|
||
controller = require(target); | ||
controller.getBase = function() { return that; } | ||
|
||
callPreAction(function() { | ||
var method = req.method.toLowerCase(); | ||
var method_action = req.action + '_' + method; | ||
|
||
var controller_action; | ||
|
||
if (controller[method_action]) { | ||
controller_action = controller[req.action + '_' + method]; | ||
} | ||
else if (controller[req.action]) { | ||
controller_action = controller[req.action]; | ||
} | ||
|
||
controller_action(req,res,function(td) { | ||
tdata = td; | ||
callback(null); | ||
process.nextTick(callPostAction); | ||
}); | ||
}); | ||
|
||
function callPreAction(callback) { | ||
if (controller.pre_action) { | ||
return controller.pre_action(req,res,callback); | ||
} | ||
callback(); | ||
} | ||
|
||
function callPostAction() { | ||
if (controller.post_action) { | ||
process.nextTick(function() { | ||
controller.post_action(req); | ||
}); | ||
} | ||
} | ||
}); | ||
}, | ||
'view' : function(callback) { | ||
target = options.root + options.views + '/' + req.controller + '/' + req.action + '.' + options.template_engine; | ||
(function(template,callback) { | ||
fs.readFile(template,"utf8", function(err,tmpdata) { | ||
if (err) { | ||
res.statusCode = 404; | ||
return res.end("<h1>404 Not Found</h1>"); | ||
} | ||
|
||
tdata.controller = req.controller; | ||
tdata.action = req.action; | ||
tdata.layout = tdata.layout || 'layout'; | ||
|
||
target = options.root + options.views + '/' + tdata.layout + '.' + options.template_engine; | ||
|
||
fs.readFile(target,"utf8", function(err,laydata) { | ||
renderer(tmpdata, tdata, function(e,content) { | ||
if (err) return callback(content); | ||
|
||
tdata.content = content; | ||
renderer(laydata, tdata, function(e,content) { | ||
return callback(content); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}(target,function(out) { callback(null,out); })); | ||
} | ||
}, function(err,data) { | ||
res.end(data.view); | ||
}); | ||
|
||
}; | ||
}; | ||
|
||
Base.prototype.getConfig = function() { | ||
return configr.get(); | ||
}; | ||
|
||
return Base; | ||
}()); |