Skip to content

Commit

Permalink
got the thing working
Browse files Browse the repository at this point in the history
  • Loading branch information
John Le Drew committed Dec 4, 2011
1 parent 8b08725 commit d17fb8c
Show file tree
Hide file tree
Showing 9 changed files with 301 additions and 39 deletions.
29 changes: 0 additions & 29 deletions Jakefile.js

This file was deleted.

3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@

## What's it do?

It's a lightweight, full stack web framework for NodeJS.
It's a lightweight, full stack web framework for NodeJS that hooks into Connect.js

## Usage



## Rules


Expand Down
12 changes: 12 additions & 0 deletions example/base.js
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);


6 changes: 6 additions & 0 deletions example/config/general.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"app" : "fred",
"test1" : "t1",
"test2" : "t2"
}

5 changes: 5 additions & 0 deletions example/controllers/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
exports.index = function(req,res,callback) {
callback({
'title' : 'Hello world!'
});
}
3 changes: 3 additions & 0 deletions example/views/index/index.jqtpl
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>

73 changes: 73 additions & 0 deletions example/views/layout.jqtpl
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>

18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"author": "John Le Drew <jp@antz29.com> (http://antz29.com)",
"name": "base",
"description": "A lightweight micro web framework.",
"keywords": ["framework", "api", "web", "mvc", "extensible", "micro"],
"version": "0.0.1dev",
"description": "A lightweight micro web framework that integrates with connect.js",
"keywords": ["framework", "api", "web", "mvc", "extensible", "micro", "connect"],
"version": "0.0.1",
"homepage": "http://base.antz29.com",
"repository": {
"type": "git",
Expand All @@ -21,11 +21,11 @@
},
"main" : "./src/lib/base.js",
"dependencies": {
"twister" : ">=0.2.0",
"slicer" : ">=0.2.0"
"twister" : ">=0.0.1",
"slicer" : ">=0.0.1",
"configr" : ">=0.0.1",
"underscore" : ">=0.0.1",
"async" : ">=0.0.1"
},
"devDependencies": {
"expresso" : ">=0.9.0",
"linter" : ">=0.0.7"
}
"devDependencies": {}
}
191 changes: 191 additions & 0 deletions src/lib/base.js
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;
}());

0 comments on commit d17fb8c

Please sign in to comment.