Here we go.

This commit is contained in:
mde
2010-03-13 23:17:10 -08:00
parent 98bd5429a2
commit 2f5b75fdfa
10 changed files with 2949 additions and 0 deletions

10
app/controllers/main.js Normal file
View File

@@ -0,0 +1,10 @@
var Main = function () {
this.index = function (params) {
this.render('hello world');
};
};
exports.Main = Main;

View File

11
app/controllers/users.js Normal file
View File

@@ -0,0 +1,11 @@
var sys = require('sys');
var Users = function () {
this.index = function (params) {
sys.puts(this.constructor.prototype);
this.render(JSON.stringify(params));
};
};
exports.Users = Users;

7
config/router.js Normal file
View File

@@ -0,0 +1,7 @@
var Router = require('../framework/router').Router;
router = new Router();
router.match('/').to({controller: 'Main', action: 'index'});
router.match('/users/:userid/messages/:messageid').to({controller: 'Users', action: 'index'});
exports.router = router;

59
framework/app.js Normal file
View File

@@ -0,0 +1,59 @@
http = require('http');
var sys = require('sys');
var fleegix = require('./fleegix');
var App = function (config) {
var _this = this;
this.config = config;
this.router = config.router;
this.controllers = config.controllers;
this.req = null;
this.resp = null;
this.run = function (req, resp) {
this.req = req;
this.resp = resp;
var url = req.url;
var base = fleegix.url.getBase(url);
var route = this.router.find(base);
if (route) {
var qs = fleegix.url.getQS(url);
var qsParams = fleegix.url.qsToObject(qs);
var params = fleegix.mixin(route.params, qsParams);
var constructor = this.controllers[route.controller];
constructor.prototype = new Controller(req, resp);
var controller = new constructor();
controller[route.action].call(controller, params);
}
else {
this.resp.writeHead(404, {'Content-Type': 'text/plain'});
this.resp.write('404: Oops, page not found.');
this.resp.close();
}
}
};
var Controller = function (req, resp) {
this.request = req;
this.response = resp;
this.content = '';
};
Controller.prototype = new function () {
this.render = function (content) {
if (typeof content != 'undefined') {
this.content = content;
}
this.response.writeHead(200, {'Content-Type': 'text/plain'});
this.response.write(this.content);
this.response.close();
};
}();
exports.App = App;

26
framework/config.js Normal file
View File

@@ -0,0 +1,26 @@
var fs = require('fs');
var fleegix = require('./fleegix');
var Config = function (dirname) {
this.appDir = dirname;
this.router = require(this.appDir + '/config/router').router;
var dirList = fs.readdirSync(this.appDir + '/app/controllers');
var fileName, controllerName;
var controllers = {};
var jsPat = /\.js$/;
for (var i = 0; i < dirList.length; i++) {
fileName = dirList[i];
if (jsPat.test(fileName)) {
fileName = fileName.replace(jsPat, '');
controllerName = fleegix.string.camelize(fileName);
controllerName = fleegix.string.capitalize(controllerName);
controllers[controllerName] = require('../app/controllers/' + fileName)[controllerName];
}
}
this.controllers = controllers;
}
exports.Config = Config;

2678
framework/fleegix.js Normal file

File diff suppressed because it is too large Load Diff

61
framework/requests.js Normal file
View File

@@ -0,0 +1,61 @@
var requests = new function () {
this.outstanding = {};
this.content = '';
this.idIncr = 0;
this.addRequest = function (param) {
var reqId = new String(this.idIncr);
this.outstanding[reqId] = {
method: param.method || 'GET',
url: param.url,
data: param.data || null,
handler: null,
finished: false
}
this.idIncr++;
};
this.makeRequests = function () {
var client = http.createClient(5984, 'localhost');
for (var p in this.outstanding) {
this.makeRequest(client, p);
}
};
this.makeRequest = function (client, p) {
var param = this.outstanding[p];
var fetchReq = client.request(param.method, param.url);
fetchReq.addListener('response', function (fetchResp) {
fetchResp.setBodyEncoding("utf8");
fetchResp.addListener("data", function (chunk) {
_this.content += chunk;
});
fetchResp.addListener("end", function () {
_this.outstanding[p].finished = true;
_this.close.call(_this);
});
});
if (param.data) {
data = typeof param.data == 'string' ?
param.data : JSON.stringify(param.data);
fetchReq.write(data, encoding="utf8");
}
fetchReq.close();
};
this.finished = function () {
for (var p in this.outstanding) {
if (!this.outstanding[p].finished) {
return false;
}
}
return true;
};
this.close = function () {
if (this.finished()) {
this.resp.write(this.content);
this.resp.close();
}
};
}();

85
framework/router.js Normal file
View File

@@ -0,0 +1,85 @@
var Router = function () {
// From BomberJS: http://bomber.obtdev.com/
const KEY_PATTERN = /:([a-zA-Z_][a-zA-Z0-9_]*)/g;
const MATCH_PATTERN_STRING = "([^\\\/.]+)";
var _routes = [];
var _namedRoutes = {};
this.regExpEscape = function(str) {
return str.replace(/(\/|\.)/g, "\\$1");
};
this.match = function(p) {
var keys = [];
var pat;
var route;
var path = '^' + p;
if (path.lastIndexOf('/') != (path.length-1)) {
path += '/?';
}
path = path + '$';
path = this.regExpEscape(path);
// full is ':foo' and submatch is 'foo'
path = path.replace(KEY_PATTERN, function(full, submatch) {
keys.push(submatch);
return MATCH_PATTERN_STRING;
});
pat = new RegExp(path);
route = new Route(pat, keys);
_routes.push(route);
return route;
};
this.find = function(path) {
var count = _routes.length;
for (var i = 0; i < count; i++) {
var route = _routes[i];
var match = path.match(route.regex);
if (match) {
match.shift(); // First entry is the entire matched string
for(var j = 0; j < route.keys.length; j++) {
var key = route.keys[j];
route.params[key] = match[j];
}
return route;
}
}
return null;
};
this.name = function (n, r) {
_namedRoutes[n] = r;
}
};
var Route = function (regex, keys, params, controller, action, name) {
var _name;
this.regex = regex || null;
this.keys = keys || null;
this.params = params || {};
this.controller = controller || null;
this.action = action || null;
if (name) {
this.name(name);
}
};
Route.prototype.to = function (obj) {
this.controller = obj.controller;
this.action = obj.action;
return this;
};
Route.prototype.name = function (n) {
_name = n;
router.name(n, this);
return this;
}
exports.Router = Router;

12
runserv.js Normal file
View File

@@ -0,0 +1,12 @@
var sys = require('sys'),
http = require('http');
var Config = require('./framework/config').Config;
var App = require('./framework/app').App;
config = new Config(__dirname);
http.createServer(function (req, resp) {
new App(config).run(req, resp);
}).listen(8000);
sys.puts('Server running at http://127.0.0.1:8000/');