Browse files

Merge branch 'develop'

  • Loading branch information...
2 parents 26056b2 + 48ce97b commit 7d1ffdd4d4b5df55ecf2532331c3cd0880edf6e2 @antz29 committed Apr 6, 2012
Showing with 206 additions and 248 deletions.
  1. +10 −6 example/base.js
  2. +1 −1 example/config/{general.json → general.js}
  3. +0 −5 example/resources/mongo.js
  4. +9 −7 package.json
  5. +169 −0 src/lib/base.coffee
  6. +0 −194 src/lib/base.js
  7. +17 −0 src/lib/resource.coffee
  8. +0 −35 src/lib/resource.js
View
16 example/base.js
@@ -1,12 +1,16 @@
+require('coffee-script')
+
var Base = require('../'),
- connect = require('connect');
+ connect = require('connect'),
+ http = require('http');
var base = new Base();
-connect(
- connect.static(__dirname + '/public'),
- connect.favicon(),
- base.connect
-).listen(3000);
+var app = connect()
+ .use(connect.static(__dirname + '/public'))
+ .use(connect.favicon())
+ .use(base.connect());
+
+http.createServer(app).listen(3000);
View
2 example/config/general.json → example/config/general.js
@@ -1,4 +1,4 @@
-{
+module.exports.config = {
"app" : "fred",
"test1" : "t1",
"test2" : "t2"
View
5 example/resources/mongo.js
@@ -1,5 +0,0 @@
-exports.index = function(req,res,callback) {
- callback({
- 'title' : 'Hello world!'
- });
-}
View
16 package.json
@@ -3,7 +3,7 @@
"name": "base",
"description": "A lightweight micro web framework that integrates with connect.js",
"keywords": ["framework", "api", "web", "mvc", "extensible", "micro", "connect"],
- "version": "0.2.6",
+ "version": "0.3.0",
"homepage": "http://github.com/antz29/node-base",
"repository": {
"type": "git",
@@ -19,13 +19,15 @@
"directories": {
"lib": "./src/lib"
},
- "main" : "./src/lib/base.js",
+ "main" : "./src/lib/base.coffee",
"dependencies": {
- "twister" : ">=0.0.1",
- "slicer" : ">=0.0.1",
- "configr" : ">=0.0.1",
- "underscore" : ">=0.0.1",
- "async" : ">=0.0.1"
+ "coffee-script" : "*",
+ "twister" : "*",
+ "slicer" : "*",
+ "configr" : "*",
+ "underscore" : "*",
+ "async" : "*",
+ "connect" : "*"
},
"devDependencies": {}
}
View
169 src/lib/base.coffee
@@ -0,0 +1,169 @@
+fs = require('fs')
+Configr = require('configr')
+Twister = require('twister')
+Slicer = require('slicer')
+_ = require('underscore')
+async = require('async')
+EventEmitter = require('events').EventEmitter
+path = require('path')
+
+class Base extends EventEmitter
+
+ ready = false
+ engine = null
+ renderer = null
+ twister = null
+ slicer = null
+ configr = null
+
+ constructor: (opt) ->
+ opt = opt or {}
+ root = path.resolve(path.dirname(process.argv[1]))
+
+ console.log "Initializing Base..."
+
+ @options = _.defaults(opt,{
+ root: root,
+ config: "/config",
+ controllers: "/controllers",
+ views: "/views",
+ models: "/models",
+ template_engine: "jqtpl"
+ })
+
+ @template_engines = {
+ 'jqtpl' : {
+ 'init' : ->
+ return require('jqtpl');
+
+ 'render' : (template, tdata, callback) ->
+ callback(null,engine.tmpl(template, tdata));
+ }
+ }
+
+ console.log "Loading the configuration in #{@options.root}#{@options.config}..."
+
+ configr = new Configr("#{@options.root}#{@options.config}")
+ twister = new Twister()
+ slicer = new Slicer()
+
+ configr.on 'ready', =>
+ twister.addRules configr.get().routes or []
+
+ slicer.addSegmentIdentifier 'controller','index'
+ slicer.addSegmentIdentifier 'action','index'
+
+ user_engine = if configr.get().general then configr.get().general.template_engine else null
+ @options.template_engine = if user_engine then user_engine else "jqtpl"
+
+ engine = @template_engines[@options.template_engine].init()
+ renderer = @template_engines[@options.template_engine].render
+
+ this.emit 'ready'
+ ready = true
+
+ console.log "Base Framework is ready to accept requests!"
+
+ addTemplateEngine: (name,def) ->
+ @template_engines[name] = def
+
+ getOptions: ->
+ return @options
+
+ _processRequest: (req,res,next) ->
+ ifReady = =>
+ if ready then return whenReady()
+ process.nextTick ifReady
+
+ process.nextTick ifReady
+
+ whenReady = =>
+ tdata = req.template or {}
+
+ async.series(
+ twister: (callback) =>
+ twister.twist req.url, (twisted) =>
+ req.url = twisted
+ callback null
+
+ splicer: (callback) =>
+ sl = slicer.slice req.url
+ req.controller = sl.controller
+ req.action = sl.action
+ req.params = sl.uri
+ callback null
+
+ controller: (callback) =>
+ target = "#{@options.root}#{@options.controllers}/#{req.controller}.js"
+
+ fs.stat target, (err) =>
+ if (err) then return callback(null)
+
+ callPreAction = (callback) =>
+ if controller.pre_action
+ controller.pre_action(req,res,callback);
+
+ callback();
+
+ callPostAction = =>
+ if controller.post_action
+ process.nextTick =>
+ controller.post_action(req)
+
+ try
+ controller = require target
+ controller.getBase = => return this
+
+ callPreAction ->
+ method = req.method.toLowerCase()
+ method_action = "#{req.action}_#{method}"
+
+ if controller[method_action]
+ controller_action = controller["#{req.action}_#{method}"]
+
+ else if controller[req.action]
+ controller_action = controller[req.action]
+
+ controller_action req, res, (td) =>
+ tdata = _.extend tdata, td
+ callback null
+ process.nextTick callPostAction
+ catch e
+ console.log "Error loading controller:\n",e
+ res.statusCode = 500
+ return res.end "<h1>500 Internal Server Error</h1>"
+
+ view: (callback) =>
+ target = "#{@options.root}#{@options.views}/#{req.controller}/#{req.action}.#{@options.template_engine}"
+ fs.readFile target, "utf8", (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 or 'layout';
+
+ target = "#{@options.root}#{@options.views}/#{tdata._layout}.#{@options.template_engine}"
+
+ fs.readFile target, "utf8", (err,laydata) =>
+ renderer tmpdata, tdata, (e,content) =>
+ if err then return callback(null,content)
+
+ tdata.content = content
+
+ renderer laydata, tdata, (e,content) =>
+ callback(null,content)
+
+ ,(err, data) =>
+ if !res.getHeader('content-type') then res.setHeader("content-type", "text/html")
+ res.end data.view
+ )
+
+ connect: ->
+ (req,res,next) =>
+ this._processRequest req, res, next
+
+ getConfig: -> configr.get()
+
+module.exports = Base
View
194 src/lib/base.js
@@ -1,194 +0,0 @@
-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 || {};
-
- var root = path.resolve(path.dirname(process.argv[1]));
-
- console.log("Initializing Base...");
- options = _.defaults(opt,{
- 'root' : root,
- 'config' : '/config',
- 'controllers' : '/controllers',
- 'views' : '/views',
- 'models' : '/models'
- });
-
- console.log("Loading the configuration in " + options.root + options.config);
-
- configr.setDirectory(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 = req.template || {};
-
- 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 = _.extend(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) {
- if (!res.getHeader('content-type')) res.setHeader("content-type", "text/html");
- res.end(data.view);
- });
-
- };
- };
-
- Base.prototype.getConfig = function() {
- return configr.get();
- };
-
- return Base;
-}());
View
17 src/lib/resource.coffee
@@ -0,0 +1,17 @@
+_ = require('underscore')
+EventEmitter = require('events').EventEmitter
+
+class Resource extends EventEmitter
+
+ constructor: (base, opt) ->
+ opt = opt or {}
+ @base = base
+
+ @options = _.defaults opt, this.defaults
+
+ getOptions: -> return @options
+ getBase: -> @base
+
+ defaults: {}
+
+module.exports = Resource
View
35 src/lib/resource.js
@@ -1,35 +0,0 @@
-
-
-module.exports = (function() {
-
- // Dependencies
- var fs = require('fs'),
- _ = require('underscore'),
- async = require('async'),
- EventEmitter = require('events').EventEmitter,
- util = require('util'),
- path = require('path');
-
- // Private members
- var options,
- base;
-
- // Constructor
- function Resource(base_core, opt) {
- base = base_core;
- var that = this;
- opt = opt || {};
-
- options = _.defaults(opt,this.defaults);
- }
-
- // Extend EventEmitter
- util.inherits(Resource, EventEmitter);
-
- Resource.prototype.defaults = {};
- Resource.prototype.getOptions = function() { return options; };
- Resource.prototype.getBase = function() { return base; };
- Resource.prototype.init = function() { return this; };
-
- return Resource;
-}());

0 comments on commit 7d1ffdd

Please sign in to comment.