Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

re-indented code

  • Loading branch information...
commit e3e0c774a05b267b854dfa314dd9c70a588fa869 1 parent 829bb71
@Shogun147 authored
Showing with 1,464 additions and 2,143 deletions.
  1. +36 −67 bin/katana
  2. +27 −45 lib/bootstrap.js
  3. +30 −45 lib/config.js
  4. +16 −16 lib/controller.js
  5. +68 −97 lib/cookie.js
  6. +137 −192 lib/katana.js
  7. +16 −43 lib/model.js
  8. +88 −107 lib/module.js
  9. +39 −52 lib/multiparser.js
  10. +39 −39 lib/request.js
  11. +79 −145 lib/response.js
  12. +152 −152 lib/router.js
  13. +11 −27 lib/session/index.js
  14. +33 −61 lib/session/memory.js
  15. +35 −63 lib/session/redis.js
  16. +27 −45 lib/session/store.js
  17. +13 −14 lib/static.js
  18. +13 −13 lib/store/index.js
  19. +16 −16 lib/store/memory.js
  20. +6 −6 lib/store/mongodb.js
  21. +13 −13 lib/store/mongoose.js
  22. +12 −12 lib/store/mysql.js
  23. +19 −41 lib/store/redis.js
  24. +227 −285 lib/utils.js
  25. +44 −137 lib/view/ejs.js
  26. +29 −86 lib/view/engine.js
  27. +25 −27 lib/view/hogan.js
  28. +8 −8 lib/view/index.js
  29. +60 −61 lib/view/jade.js
  30. +41 −41 lib/view/jinsoku.js
  31. +1 −1  package.json
  32. +0 −28 readme.md
  33. +45 −77 template/application/config/development/application.js
  34. +3 −3 template/application/config/development/routing.js
  35. +43 −65 template/application/config/development/stores.js
  36. +5 −5 template/application/controllers/home.js
  37. +8 −8 template/cluster.js
View
103 bin/katana
@@ -18,45 +18,45 @@ App.version('0.1.0');
var create = App.command('create [path]')
.description('Create default application structure and base configuration')
.action(function(path) {
- path = path ? Path.resolve(root, path) + '/' : root;
-
- App.confirm('Create application at: '+ path +' ? [Yes|No]: ', function(ok) {
- if (ok) {
- if (!Fs.existsSync(path)) {
- mkdir.sync(path, 0755);
- }
+ path = path ? Path.resolve(root, path) + '/' : root;
- copy(mod_root + 'template', path, function(error) {
- console.log(error ? error : 'Done!');
-
- var empty_paths = [path + 'application/config/production', path + 'application/models',
- path + 'public/images', path + 'public/scripts', path + 'public/styles',
- path + 'modules', path + 'temp'];
-
- empty_paths.forEach(function(p) {
- if (!Fs.existsSync(p)) {
- mkdir.sync(p, 0777);
- }
- });
-
- var pack = {
- name: 'application-name',
- version: '0.0.1',
- private: true,
- dependencies: {
- katana: version,
- joose: '*'
- }
- };
+ App.confirm('Create application at: '+ path +' ? [Yes|No]: ', function(ok) {
+ if (ok) {
+ if (!Fs.existsSync(path)) {
+ mkdir.sync(path, 0755);
+ }
+
+ copy(mod_root + 'template', path, function(error) {
+ console.log(error ? error : 'Done!');
- Fs.writeFileSync(path + 'package.json', JSON.stringify(pack));
+ var empty_paths = [path + 'application/config/production', path + 'application/models',
+ path + 'public/images', path + 'public/scripts', path + 'public/styles',
+ path + 'modules', path + 'temp'];
+
+ empty_paths.forEach(function(p) {
+ if (!Fs.existsSync(p)) {
+ mkdir.sync(p, 0777);
+ }
+ });
+
+ var pack = {
+ name: 'application-name',
+ version: '0.0.1',
+ private: true,
+ dependencies: {
+ katana: version,
+ joose: '*'
+ }
+ };
+
+ Fs.writeFileSync(path + 'package.json', JSON.stringify(pack));
- process.stdin.destroy();
- });
- } else {
- process.stdin.destroy();
- }
- });
+ process.stdin.destroy();
+ });
+ } else {
+ process.stdin.destroy();
+ }
+ });
});
App.command('install <module>')
@@ -75,34 +75,3 @@ App.command('install <module>')
});
App.parse(process.argv);
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
View
72 lib/bootstrap.js
@@ -6,49 +6,49 @@ var Async = require('async');
// Call all EventListener's in parallel and callback on all done
EventEmitter.prototype.parallel = function(event, callback) {
- emit.call(this, event, callback, Array.prototype.slice.call(arguments, 2), 'parallel');
+ emit.call(this, event, callback, Array.prototype.slice.call(arguments, 2), 'parallel');
}
// Call all EventListener's in order they are added and callback on last done
EventEmitter.prototype.series = function(event, callback) {
- emit.call(this, event, callback, Array.prototype.slice.call(arguments, 2), 'series');
+ emit.call(this, event, callback, Array.prototype.slice.call(arguments, 2), 'series');
}
// Listener wrapper
var fn = function(listener, args) {
- return function(callback) {
- args = args.concat(callback);
+ return function(callback) {
+ args = args.concat(callback);
- listener.apply(listener, args);
- }
+ listener.apply(listener, args);
+ }
}
// Custom Event emit which allow to run event listeners in parallel or series
// also we could listen all event listeners complete with callback
function emit(event, callback, args, type) {
- // Get all listeners for this event added with standart addListener or on methods
- var listeners = this.listeners(event);
+ // Get all listeners for this event added with standart addListener or on methods
+ var listeners = this.listeners(event);
- // Prepared list of listeners
- var list = [];
+ // Prepared list of listeners
+ var list = [];
- if (args.length) {
- for (var i=0; i<listeners.length; i++) {
- // Wrap listener
- var f = fn(listeners[i], args);
+ if (args.length) {
+ for (var i=0; i<listeners.length; i++) {
+ // Wrap listener
+ var f = fn(listeners[i], args);
- list.push(f);
- }
- } else {
- list = listeners;
+ list.push(f);
}
+ } else {
+ list = listeners;
+ }
- // Parallel or Series
- var method = type === 'parallel' ? Async.parallel : Async.series;
+ // Parallel or Series
+ var method = type === 'parallel' ? Async.parallel : Async.series;
- method(list, callback || function(error, results) {
- if (error) { throw error; }
- });
+ method(list, callback || function(error, results) {
+ if (error) { throw error; }
+ });
}
// Methods call wrapper, around builder with regexp selector
@@ -58,35 +58,17 @@ Joose.Managed.Builder.meta.addMethod('call', function(targetMeta, info) {
var around = {};
this.meta.getMethods().eachOwn(function(fn, name) {
- methods[name] = fn;
+ methods[name] = fn;
});
Joose.O.each(info, function (value, name) {
for (method in methods) {
- if (new RegExp(name).test(method)) {
- around[method] = value;
- }
+ if (new RegExp(name).test(method)) {
+ around[method] = value;
+ }
}
});
this.meta.extend({ around: around });
}, Joose.Managed.Property.MethodModifier.Before);
});
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
View
75 lib/config.js
@@ -6,71 +6,56 @@ var merge = require('./utils').merge;
var Config = {};
module.exports = function(config) {
- if (!config) { return Config.application.application; }
+ if (!config) { return Config.application.application; }
- var module = 'application';
+ var module = 'application';
- if (m = config.match(/(.*)(\:|\.)(.*)/i)) {
- module = m[1];
- config = m[3]!=='' ? m[3] : module;
- }
+ if (m = config.match(/(.*)(\:|\.)(.*)/i)) {
+ module = m[1];
+ config = m[3]!=='' ? m[3] : module;
+ }
- if (!Config[module]) { Config[module] = {}; }
+ if (!Config[module]) { Config[module] = {}; }
- if (config === '*') {
- return Config[module];
- }
+ if (config === '*') {
+ return Config[module];
+ }
- if (Config[module][config] === undefined) { Config[module][config] = {}; }
+ if (Config[module][config] === undefined) { Config[module][config] = {}; }
- return Config[module][config];
+ return Config[module][config];
}
module.exports.load = function(module) {
- module = module || 'application';
+ module = module || 'application';
- var path = module=='application' ? root+'application/config/' : root+'modules/'+module+'/config/';
+ var path = module=='application' ? root+'application/config/' : root+'modules/'+module+'/config/';
- if (!Config[module]) { Config[module] = {}; }
+ if (!Config[module]) { Config[module] = {}; }
- if (Fs.existsSync(path + 'development') && Fs.statSync(path + 'development').isDirectory()) {
- Fs.readdirSync(path + 'development').forEach(function(file) {
- var name = Path.basename(file, Path.extname(file));
+ if (Fs.existsSync(path + 'development') && Fs.statSync(path + 'development').isDirectory()) {
+ Fs.readdirSync(path + 'development').forEach(function(file) {
+ var name = Path.basename(file, Path.extname(file));
- Config[module][name] = require(path +'development/'+ name);
- });
- }
+ Config[module][name] = require(path +'development/'+ name);
+ });
+ }
- if (env !== 'development') {
- if (Fs.existsSync(path + env) && Fs.statSync(path + env).isDirectory()) {
- Fs.readdirSync(path + env).forEach(function(file) {
- var name = Path.basename(file, Path.extname(file));
+ if (env !== 'development') {
+ if (Fs.existsSync(path + env) && Fs.statSync(path + env).isDirectory()) {
+ Fs.readdirSync(path + env).forEach(function(file) {
+ var name = Path.basename(file, Path.extname(file));
- var config = require(path + env +'/'+ name);
+ var config = require(path + env +'/'+ name);
- merge(Config[module][name], Config[module][name], config);
- });
- }
+ merge(Config[module][name], Config[module][name], config);
+ });
}
+ }
- return Config[module];
+ return Config[module];
}
module.exports.Config = Config;
module.exports.load();
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
View
32 lib/controller.js
@@ -3,27 +3,27 @@ var View = App.View;
var extend = App.Utils.extend;
Class('Katana.Core.Controller', {
- have: {
- data: []
- },
+ have: {
+ data: []
+ },
- methods: {
- initialize: function() {
+ methods: {
+ initialize: function() {
- },
+ },
- set: function(name, value) {
- if (typeof(name) === 'object') {
- this.data = extend(this.data, name);
- } else {
- this.data[name] = value;
- }
- },
+ set: function(name, value) {
+ if (typeof(name) === 'object') {
+ this.data = extend(this.data, name);
+ } else {
+ this.data[name] = value;
+ }
+ },
- render: function(template, data) {
- return View.render(template, extend(this.data, data));
- }
+ render: function(template, data) {
+ return View.render(template, extend(this.data, data));
}
+ }
});
module.exports = Katana.Core.Controller;
View
165 lib/cookie.js
@@ -5,142 +5,113 @@ var sanitize = require('./utils').sanitize;
var config = App.Config().cookie;
Class('Katana.Core.Cookie', {
- have: {
- data: {}
- },
+ have: {
+ data: {}
+ },
- methods: {
- parse: function(Request) {
- var Cookies = this;
+ methods: {
+ parse: function(Request) {
+ var Cookies = this;
- if (Request.request.headers['cookie']) {
- var c = Request.request.headers['cookie'].split(';');
+ if (Request.request.headers['cookie']) {
+ var c = Request.request.headers['cookie'].split(';');
- var item, parsed, name, value;
+ var item, parsed, name, value;
- for (var i=0; i<c.length; i++) {
- item = sanitize(c[i]).trim();
+ for (var i=0; i<c.length; i++) {
+ item = sanitize(c[i]).trim();
- parsed = item.split('=');
+ parsed = item.split('=');
- name = parsed[0];
- value = parsed[1];
+ name = parsed[0];
+ value = parsed[1];
- Cookies.data[name] = new Cookie(name, value, config);
- }
- }
- },
+ Cookies.data[name] = new Cookie(name, value, config);
+ }
+ }
+ },
- to_array: function() {
- var send = [];
+ to_array: function() {
+ var send = [];
- for (var cookie in this.data) {
- var c = this.data[cookie];
+ for (var cookie in this.data) {
+ var c = this.data[cookie];
- if (c.send) {
- send.push(c.toString());
- }
+ if (c.send) {
+ send.push(c.toString());
}
+ }
- return send;
- },
+ return send;
+ },
- get: function(name, def_value) {
- if (!name) { return this.data; }
+ get: function(name, def_value) {
+ if (!name) { return this.data; }
- var cookie = this.data[name] !== undefined ? this.data[name].value : def_value;
+ var cookie = this.data[name] !== undefined ? this.data[name].value : def_value;
- return cookie;
- },
+ return cookie;
+ },
- set: function(name, value, options) {
- this.data[name] = new Cookie(name, value, options);
- }
+ set: function(name, value, options) {
+ this.data[name] = new Cookie(name, value, options);
}
+ }
});
var Cookie = function(name, value, options) {
- this.name = name;
- this.value = value;
+ this.name = name;
+ this.value = value;
- options = options || {};
- this.path = options.path || '/';
+ options = options || {};
+ this.path = options.path || '/';
- this.expires = null;
+ this.expires = null;
- if (options.lifetime != null) {
- this.expires = new Date(Date.now() + options.lifetime).toGMTString();
- }
+ if (options.lifetime != null) {
+ this.expires = new Date(Date.now() + options.lifetime).toGMTString();
+ }
- this.domain = options.domain || null;
- this.httpOnly = options.httpOnly || true;
- this.send = true;
+ this.domain = options.domain || null;
+ this.httpOnly = options.httpOnly || true;
+ this.send = true;
- this.toString = function() {
- var arr = [this.name +'='+ this.value];
- var keys = ['path', 'expires', 'domain'];
+ this.toString = function() {
+ var arr = [this.name +'='+ this.value];
+ var keys = ['path', 'expires', 'domain'];
- for (var i=0; i<keys.length; i++) {
- key = keys[i];
+ for (var i=0; i<keys.length; i++) {
+ key = keys[i];
- if (this[key]) {
- arr.push(key +'='+ this[key]);
- }
- }
-
- if (this.httpOnly) {
- arr.push('HttpOnly');
- }
+ if (this[key]) {
+ arr.push(key +'='+ this[key]);
+ }
+ }
- return arr.join('; ');
+ if (this.httpOnly) {
+ arr.push('HttpOnly');
}
+
+ return arr.join('; ');
+ }
}
App.on('request', function(Request, Response, callback) {
- var Cookies = new Katana.Core.Cookie;
+ var Cookies = new Katana.Core.Cookie;
- Cookies.parse(Request);
+ Cookies.parse(Request);
- Request.cookie = Cookies;
+ Request.cookie = Cookies;
- callback();
+ callback();
});
App.on('send_response', function(Response, Request, callback) {
- App.series('cookie.send', function() {
- Response.header('Set-Cookie', Request.cookie.to_array());
+ App.series('cookie.send', function() {
+ Response.header('Set-Cookie', Request.cookie.to_array());
- callback();
- }, Response, Request);
+ callback();
+ }, Response, Request);
});
module.exports = Katana.Core.Cookie;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
View
329 lib/katana.js
@@ -26,160 +26,160 @@ var Log = require('winston');
var load = require('./utils').load;
Class('Katana', {
- meta: Joose.Meta.Class,
+ meta: Joose.Meta.Class,
- isa: EventEmitter,
+ isa: EventEmitter,
- have: {
- about: {},
- server: null,
- controllers: []
- },
+ have: {
+ about: {},
+ server: null,
+ controllers: []
+ },
- methods: {
- initialize: function() {
- var App = this;
-
- App.about = require(mod_root + 'package.json');
+ methods: {
+ initialize: function() {
+ var App = this;
- process.nextTick(function() {
- App.boot();
- })
- },
+ App.about = require(mod_root + 'package.json');
- run: function() {
- App.series('run', function(error, results) {
- if (error instanceof Error) { throw error; }
+ process.nextTick(function() {
+ App.boot();
+ });
+ },
+
+ run: function() {
+ App.series('run', function(error, results) {
+ if (error instanceof Error) { throw error; }
- if (config.ssl.enabled) {
- var options = {
- key: Fs.readFileSync(config.ssl.key),
- cert: Fs.readFileSync(config.ssl.cert)
- };
-
- App.server = Https.Server(options);
- } else {
- App.server = Http.Server();
- }
+ if (config.ssl.enabled) {
+ var options = {
+ key: Fs.readFileSync(config.ssl.key),
+ cert: Fs.readFileSync(config.ssl.cert)
+ };
+
+ App.server = Https.Server(options);
+ } else {
+ App.server = Http.Server();
+ }
- App.server.on('request', function(request, response) {
- App.series('connection', function(error, results) {
- if (error instanceof Error) {
- App.series('request.error_connection', function(err, res) {
- if ((err instanceof Error) || err !== true) {
- response.writeHead(500, { 'Content-Type': 'text/html' });
- response.end('Error occured while handling request, please try again.');
- }
- }, error, request, response);
- } else if (error !== true) {
- App.handle_connection(request, response);
- }
- }, request, response);
- });
+ App.server.on('request', function(request, response) {
+ App.series('connection', function(error, results) {
+ if (error instanceof Error) {
+ App.series('request.error_connection', function(err, res) {
+ if ((err instanceof Error) || err !== true) {
+ response.writeHead(500, { 'Content-Type': 'text/html' });
+ response.end('Error occured while handling request, please try again.');
+ }
+ }, error, request, response);
+ } else if (error !== true) {
+ App.handle_connection(request, response);
+ }
+ }, request, response);
+ });
- App.server.listen(config.port, config.host);
+ App.server.listen(config.port, config.host);
- Log.info('['+ process.pid +'] Listen at: '+ config.host +':'+ config.port);
+ Log.info('['+ process.pid +'] Listen at: '+ config.host +':'+ config.port);
- App.parallel('ready', function(error, results) {
- if (error) { throw error; }
- });
- });
- },
+ App.parallel('ready', function(error, results) {
+ if (error) { throw error; }
+ });
+ });
+ },
- handle_connection: function(request, response) {
- var Request = new Katana.Core.Request(request, response);
- var Response = new Katana.Core.Response(response, Request);
+ handle_connection: function(request, response) {
+ var Request = new Katana.Core.Request(request, response);
+ var Response = new Katana.Core.Response(response, Request);
- App.series('request', function(error, results) {
- if (Request.module) {
- var Module = require('./module');
+ App.series('request', function(error, results) {
+ if (Request.module) {
+ var Module = require('./module');
- if (Module(Request.module) != undefined) {
- Module(Request.module).run_uri(Request.routed_uri, [Response, Request], Request.method);
- } else {
- if (App.controllers[routing.controller_404] && typeof(App.controllers[routing.controller_404][routing.action_404]) == 'function') {
- App.controllers[routing.controller_404][routing.action_404](Response, Request);
- } else {
- Response.error(404);
- }
- }
- } else {
- var Controller = App.controllers[Request.directory + Request.controller];
-
- if (Controller != undefined) {
- if (typeof(Controller[Request.action]) == 'function') {
- Controller[Request.action](Response, Request);
- } else {
- if (typeof(Controller[routing.action_404]) == 'function') {
- Controller[routing.action_404](Response, Request);
- } else if (App.controllers[routing.controller_404] && typeof(App.controllers[routing.controller_404][routing.action_404]) == 'function') {
- App.controllers[routing.controller_404][routing.action_404](Response, Request);
- } else {
- Response.error(404);
- }
- }
- } else {
- if (App.controllers[routing.controller_404] && typeof(App.controllers[routing.controller_404][routing.action_404]) == 'function') {
- App.controllers[routing.controller_404][routing.action_404](Response, Request);
- } else {
- Response.error(404);
- }
- }
- }
- }, Request, Response);
- },
+ if (Module(Request.module) != undefined) {
+ Module(Request.module).run_uri(Request.routed_uri, [Response, Request], Request.method);
+ } else {
+ if (App.controllers[routing.controller_404] && typeof(App.controllers[routing.controller_404][routing.action_404]) == 'function') {
+ App.controllers[routing.controller_404][routing.action_404](Response, Request);
+ } else {
+ Response.error(404);
+ }
+ }
+ } else {
+ var Controller = App.controllers[Request.directory + Request.controller];
+
+ if (Controller != undefined) {
+ if (typeof(Controller[Request.action]) == 'function') {
+ Controller[Request.action](Response, Request);
+ } else {
+ if (typeof(Controller[routing.action_404]) == 'function') {
+ Controller[routing.action_404](Response, Request);
+ } else if (App.controllers[routing.controller_404] && typeof(App.controllers[routing.controller_404][routing.action_404]) == 'function') {
+ App.controllers[routing.controller_404][routing.action_404](Response, Request);
+ } else {
+ Response.error(404);
+ }
+ }
+ } else {
+ if (App.controllers[routing.controller_404] && typeof(App.controllers[routing.controller_404][routing.action_404]) == 'function') {
+ App.controllers[routing.controller_404][routing.action_404](Response, Request);
+ } else {
+ Response.error(404);
+ }
+ }
+ }
+ }, Request, Response);
+ },
- boot: function() {
- Async.series({
- config: function(callback) {
- App.series('boot.config', function(error, results) {
- callback(error, results);
- });
- },
- stores: function(callback) {
- App.series('boot.stores', function(error, results) {
- callback(error, results);
- });
- },
- models: function(callback) {
- Model.load(null, function() {
- App.series('boot.models', function(error, results) {
- callback(error, results);
- });
- });
- },
- views: function(callback) {
- View.load(null, function() {
- App.series('boot.views', function(error, results) {
- callback(error, results);
- });
- });
- },
- modules: function(callback) {
- App.series('boot.modules', function(error, results) {
- callback(error, results);
- });
- },
- controllers: function(callback) {
- load(root + 'application/controllers', function(error, controllers) {
- App.controllers = controllers;
+ boot: function() {
+ Async.series({
+ config: function(callback) {
+ App.series('boot.config', function(error, results) {
+ callback(error, results);
+ });
+ },
+ stores: function(callback) {
+ App.series('boot.stores', function(error, results) {
+ callback(error, results);
+ });
+ },
+ models: function(callback) {
+ Model.load(null, function() {
+ App.series('boot.models', function(error, results) {
+ callback(error, results);
+ });
+ });
+ },
+ views: function(callback) {
+ View.load(null, function() {
+ App.series('boot.views', function(error, results) {
+ callback(error, results);
+ });
+ });
+ },
+ modules: function(callback) {
+ App.series('boot.modules', function(error, results) {
+ callback(error, results);
+ });
+ },
+ controllers: function(callback) {
+ load(root + 'application/controllers', function(error, controllers) {
+ App.controllers = controllers;
- App.series('boot.controllers', function(error, results) {
- callback(error, results);
- });
- });
- }
- },
- function(error, results) {
- if (error) {
- throw error;
- } else {
- App.run();
- }
+ App.series('boot.controllers', function(error, results) {
+ callback(error, results);
});
+ });
}
+ },
+ function(error, results) {
+ if (error) {
+ throw error;
+ } else {
+ App.run();
+ }
+ });
}
+ }
});
global.App = module.exports = new Katana;
@@ -198,60 +198,5 @@ module.exports.Model = require('./model');
module.exports.Module = require('./module');
module.exports.Controller = require('./controller');
-if (config.cookie.enabled) module.exports.Cookie = require('./cookie');
+if (config.cookie.enabled) module.exports.Cookie = require('./cookie');
if (config.session.enabled) module.exports.Session = require('./session');
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
View
59 lib/model.js
@@ -5,57 +5,30 @@ var load = require('./utils').load;
var models = {};
module.exports = Model = function(model, module) {
- if (!model) { return models; }
+ if (!model) { return models; }
- var module = module || 'application';
+ var module = module || 'application';
- model = model.replace('.', '/');
+ model = model.replace('.', '/');
- if (m = model.match(/(.*)\:(.*)/i)) {
- module = m[1];
- model = m[2];
- }
+ if (m = model.match(/(.*)\:(.*)/i)) {
+ module = m[1];
+ model = m[2];
+ }
- return models[module][model];
+ return models[module][model];
}
module.exports.load = load_models = function(module, callback) {
- module = module || 'application';
+ module = module || 'application';
- var path = module=='application' ? root +'application/models' : root+'modules/'+ module +'/models';
+ var path = module=='application' ? root +'application/models' : root+'modules/'+ module +'/models';
- load(path, function(error, mds) {
- if (error) { throw error; }
+ load(path, function(error, mds) {
+ if (error) { throw error; }
- models[module] = mds;
-
- callback && callback();
- });
+ models[module] = mds;
+
+ callback && callback();
+ });
}
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
View
195 lib/module.js
@@ -12,170 +12,151 @@ var merge = App.Utils.merge;
var modules = {};
Class('Katana.Core.Module', {
- have: {
- name: 'Katana.Module',
- controllers: {}
+ have: {
+ name: 'Katana.Module',
+ controllers: {}
+ },
+
+ methods: {
+ initialize: function() {
+ var Module = this;
},
- methods: {
- initialize: function() {
- var Module = this;
- },
-
- run: function(uri) {
- uri = uri || '';
+ run: function(uri) {
+ uri = uri || '';
- var args = Array.prototype.slice.call(arguments, 1);
+ var args = Array.prototype.slice.call(arguments, 1);
- return this._run(uri, args, false);
- },
+ return this._run(uri, args, false);
+ },
- run_uri: function(uri, args, method) {
- return this._run(uri, args, true, method);
- },
+ run_uri: function(uri, args, method) {
+ return this._run(uri, args, true, method);
+ },
- _run: function(uri, args, use_routes, method) {
- var Module = this;
-
- method = method || 'get';
+ _run: function(uri, args, use_routes, method) {
+ var Module = this;
- if (!use_routes) {
- uri = uri.replace('.', '/');
+ method = method || 'get';
+
+ if (!use_routes) {
+ uri = uri.replace('.', '/');
- uri = uri.replace(/^([a-z]+):/, function(_, m) {
- method = m;
+ uri = uri.replace(/^([a-z]+):/, function(_, m) {
+ method = m;
- return '';
- });
- }
+ return '';
+ });
+ }
- var routing = merge({}, App.Config(Module.name + ':routing'));
+ var routing = merge({}, App.Config(Module.name + ':routing'));
- if (!use_routes) { routing.routes = []; }
+ if (!use_routes) { routing.routes = []; }
- var route = Router.route(uri, method, routing, Module.name, use_routes);
+ var route = Router.route(uri, method, routing, Module.name, use_routes);
- route.arguments = args.concat(route.arguments);
+ route.arguments = args.concat(route.arguments);
- var Controller = Module.controllers[route.directory + route.controller];
+ var Controller = Module.controllers[route.directory + route.controller];
- if (Controller != undefined) {
- if (typeof(Controller[route.action]) === 'function') {
- return Controller[route.action].apply(Controller, route.arguments);
- } else {
- throw new Error('Could not find action '+ route.action +' for controller '+ route.controller +' in module '+ Module.name);
- }
- } else {
- throw new Error('Could not find controller '+ route.controller +' for module '+ Module.name);
- }
+ if (Controller != undefined) {
+ if (typeof(Controller[route.action]) === 'function') {
+ return Controller[route.action].apply(Controller, route.arguments);
+ } else {
+ throw new Error('Could not find action '+ route.action +' for controller '+ route.controller +' in module '+ Module.name);
}
+ } else {
+ throw new Error('Could not find controller '+ route.controller +' for module '+ Module.name);
+ }
}
+ }
});
module.exports = Module = function(module) {
- if (module === '*') {
- return modules;
- } else if (module) {
- return modules[module];
- }
+ if (module === '*') {
+ return modules;
+ } else if (module) {
+ return modules[module];
+ }
- return Katana.Core.Module;
+ return Katana.Core.Module;
}
var required_modules = Array.isArray(Config().modules) ? Config().modules.slice() : [];
var all_modules = [];
if (Fs.existsSync(root + 'modules')) {
- Fs.readdirSync(root + 'modules').forEach(function(module) {
- all_modules.push(Path.basename(module, Path.extname(module)));
- });
+ Fs.readdirSync(root + 'modules').forEach(function(module) {
+ all_modules.push(Path.basename(module, Path.extname(module)));
+ });
- required_modules.slice().forEach(function(module) {
- all_modules.forEach(function(m) {
- if (module == m) {
- modules[module] = module;
+ required_modules.slice().forEach(function(module) {
+ all_modules.forEach(function(m) {
+ if (module == m) {
+ modules[module] = module;
- required_modules.splice(required_modules.indexOf(module), 1);
- }
- });
+ required_modules.splice(required_modules.indexOf(module), 1);
+ }
});
+ });
}
if (required_modules.length) {
- throw new Error('Could not found modules: '+ required_modules.join(', '));
+ throw new Error('Could not found modules: '+ required_modules.join(', '));
}
App.on('boot.config', function(callback) {
- for (module in modules) {
- Config.load(module);
- }
+ for (module in modules) {
+ Config.load(module);
+ }
- callback();
+ callback();
});
App.on('boot.models', function(callback) {
- var pending = Object.keys(modules).length;
+ var pending = Object.keys(modules).length;
- if (!pending) { callback(); }
+ if (!pending) { callback(); }
- for (module in modules) {
- Model.load(module, function() {
- if (!--pending) { return callback(); }
- });
- }
+ for (module in modules) {
+ Model.load(module, function() {
+ if (!--pending) { return callback(); }
+ });
+ }
});
App.on('boot.views', function(callback) {
- var pending = Object.keys(modules).length;
+ var pending = Object.keys(modules).length;
- if (!pending) { return callback(); }
+ if (!pending) { return callback(); }
- for (module in modules) {
- View.load(module, function() {
- if (!--pending) { callback(); }
- })
- }
+ for (module in modules) {
+ View.load(module, function() {
+ if (!--pending) { callback(); }
+ })
+ }
});
App.on('boot.modules', function(callback) {
- for (module in modules) {
- modules[module] = require(root +'modules/'+ module);
- }
+ for (module in modules) {
+ modules[module] = require(root +'modules/'+ module);
+ }
- callback();
+ callback();
});
App.on('boot.controllers', function(callback) {
- var pending = Object.keys(modules).length;
+ var pending = Object.keys(modules).length;
- if (!pending) { return callback(); }
+ if (!pending) { return callback(); }
- Object.keys(modules).forEach(function(module) {
- load(root +'modules/'+ module +'/controllers', function(error, controllers) {
- if (error) { throw error; }
+ Object.keys(modules).forEach(function(module) {
+ load(root +'modules/'+ module +'/controllers', function(error, controllers) {
+ if (error) { throw error; }
- modules[module].controllers = controllers;
+ modules[module].controllers = controllers;
- if (!--pending) { callback(); }
- });
+ if (!--pending) { callback(); }
});
+ });
});
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
View
91 lib/multiparser.js
@@ -5,76 +5,63 @@ var EventEmitter = require('events').EventEmitter;
var config = App.Config().multipart;
App.on('connection', function(request, response, callback) {
- var need_parse = request.method != 'GET' && request.method != 'HEAD';
+ var need_parse = request.method != 'GET' && request.method != 'HEAD';
- request.form = null;
+ request.form = null;
- request.on('end', function() {
- if (!need_parse) {
- callback();
- }
- });
+ request.on('end', function() {
+ if (!need_parse) {
+ callback();
+ }
+ });
- if (need_parse) {
- var mime = (request.headers['content-type'] || '').split(';')[0];
+ if (need_parse) {
+ var mime = (request.headers['content-type'] || '').split(';')[0];
- if (mime != 'multipart/form-data' && mime != 'application/x-www-form-urlencoded') {
- return callback();
- }
+ if (mime != 'multipart/form-data' && mime != 'application/x-www-form-urlencoded') {
+ return callback();
+ }
- var Form = new Formidable.IncomingForm();
- Form.data = {};
- Form.files = {};
+ var Form = new Formidable.IncomingForm();
+ Form.data = {};
+ Form.files = {};
- request.form = Form;
+ request.form = Form;
- App.emit('request_multipart', request);
+ App.emit('request.multipart', request);
for (key in config) {
Form[key] = config[key];
}
- Form.on('field', function(name, value) {
- on_data(name, value, Form.data);
- });
+ Form.on('field', function(name, value) {
+ on_data(name, value, Form.data);
+ });
- Form.on('file', function(name, value) {
- on_data(name, value, Form.files);
- });
+ Form.on('file', function(name, value) {
+ on_data(name, value, Form.files);
+ });
- Form.on('error', function(error) {
- config.wait_end && callback(error);
- });
+ Form.on('error', function(error) {
+ config.wait_end && callback(error);
+ });
- Form.on('end', function() {
- config.wait_end && callback();
- });
+ Form.on('end', function() {
+ config.wait_end && callback();
+ });
- Form.parse(request);
+ Form.parse(request);
- !config.wait_end && callback();
- }
+ !config.wait_end && callback();
+ }
});
function on_data(name, value, data) {
- if (Array.isArray(data[name])) {
- data[name].push(value);
- } else if (data[name]) {
- data[name] = [data[name], value];
- } else {
- data[name] = value;
- }
+ if (Array.isArray(data[name])) {
+ data[name].push(value);
+ } else if (data[name]) {
+ data[name] = [data[name], value];
+ } else {
+ data[name] = value;
+ }
}
-
-
-
-
-
-
-
-
-
-
-
-
-
View
78 lib/request.js
@@ -10,54 +10,54 @@ var Router = require('./router');
var routing = require('./config')('routing');
Class('Katana.Core.Request', {
- have: {
- request: null, response: null,
- method: 'get', url: null, uri: '/', routed_uri: '',
- module: '', directory: '', controller: '', action: '', arguments: [], params: {},
- client: {}, form: null, data: {}, files: {}, query: {}, cookie: {}, session: {},
+ have: {
+ request: null, response: null,
+ method: 'get', url: null, uri: '/', routed_uri: '',
+ module: '', directory: '', controller: '', action: '', arguments: [], params: {},
+ client: {}, form: null, data: {}, files: {}, query: {}, cookie: {}, session: {},
- methods: ['get', 'post', 'put', 'delete']
- },
+ methods: ['get', 'post', 'put', 'delete']
+ },
- methods: {
- BUILD: function(Request, Response) {
- return {
- request: Request,
- response: Response,
- form: Request.form
- }
- },
+ methods: {
+ BUILD: function(Request, Response) {
+ return {
+ request: Request,
+ response: Response,
+ form: Request.form
+ }
+ },
- initialize: function() {
- var Request = this;
+ initialize: function() {
+ var Request = this;
- Request.method = Request.request.method.toLowerCase();
- Request.request.url = decodeURI(Request.request.url);
- Request.url = Url.parse(Request.request.url);
- Request.uri = sanitize(Request.url.pathname).trim('\\s\/');
- Request.query = Request.url.query ? QS.parse(Request.url.query) : {};
- Request.ajax = Request.is_ajax = (Request.request.headers['x-requested-with'] && Request.request.headers['x-requested-with'] == 'XMLHttpRequest');
+ Request.method = Request.request.method.toLowerCase();
+ Request.request.url = decodeURI(Request.request.url);
+ Request.url = Url.parse(Request.request.url);
+ Request.uri = sanitize(Request.url.pathname).trim('\\s\/');
+ Request.query = Request.url.query ? QS.parse(Request.url.query) : {};
+ Request.ajax = Request.is_ajax = (Request.request.headers['x-requested-with'] && Request.request.headers['x-requested-with'] == 'XMLHttpRequest');
- if (Request.request.form) {
- Request.data = Request.request.form.data;
- Request.files = Request.request.form.files;
- }
+ if (Request.request.form) {
+ Request.data = Request.request.form.data;
+ Request.files = Request.request.form.files;
+ }
- Request.client = {
- ip: Request.request.connection.remoteAddress
- }
+ Request.client = {
+ ip: Request.request.connection.remoteAddress
+ }
- var route = Router.route(Request.uri, Request.method, routing, null, true);
+ var route = Router.route(Request.uri, Request.method, routing, null, true);
- Request.module = route.module;
- Request.directory = route.directory;
- Request.controller = route.controller;
- Request.action = route.action;
- Request.arguments = route.arguments;
- Request.params = route.params;
- Request.routed_uri = route.routed;
- }
+ Request.module = route.module;
+ Request.directory = route.directory;
+ Request.controller = route.controller;
+ Request.action = route.action;
+ Request.arguments = route.arguments;
+ Request.params = route.params;
+ Request.routed_uri = route.routed;
}
+ }
});
module.exports = Katana.Core.Request;
View
224 lib/response.js
@@ -3,175 +3,109 @@ var View = App.View;
var to_json = JSON.stringify;
Class('Katana.Core.Response', {
- have: {
- response: null, request: null,
- status: 200, type: 'text/html', charset: 'utf-8',
- content: '', redirect_url: null
- },
+ have: {
+ response: null, request: null,
+ status: 200, type: 'text/html', charset: 'utf-8',
+ content: '', redirect_url: null
+ },
- methods: {
- BUILD: function(Response, Request) {
- return {
- response: Response,
- request: Request
- }
- },
+ methods: {
+ BUILD: function(Response, Request) {
+ return {
+ response: Response,
+ request: Request
+ }
+ },
- send: function(content, type, status, charset) {
- var Response = this;
+ send: function(content, type, status, charset) {
+ var Response = this;
- content && (this.content = content);
- type && (this.type = type);
- charset && (this.charset = charset);
- status && (this.status = status);
+ content && (this.content = content);
+ type && (this.type = type);
+ charset && (this.charset = charset);
+ status && (this.status = status);
- App.series('send_response', function(error, results) {
- if (error) { throw error; }
+ App.series('send_response', function(error, results) {
+ if (error) { throw error; }
- if (typeof(Response.content) === 'object') {
- Response.content = to_json(Response.content);
- Response.type = 'application/json';
- }
-
- Response.response.writeHead(Response.status, { 'Content-Type': Response.type, 'charset': Response.charset });
- Response.response.end(Response.content);
- }, Response, Response.request);
- },
+ if (typeof(Response.content) === 'object') {
+ Response.content = to_json(Response.content);
+ Response.type = 'application/json';
+ }
+
+ Response.response.writeHead(Response.status, { 'Content-Type': Response.type, 'charset': Response.charset });
+ Response.response.end(Response.content);
+ }, Response, Response.request);
+ },
- header: function(name, value) {
- if (arguments.length == 1) { return this.response.getHeader(name); }
+ header: function(name, value) {
+ if (arguments.length == 1) { return this.response.getHeader(name); }
- this.response.setHeader(name, value);
+ this.response.setHeader(name, value);
- return this;
- },
+ return this;
+ },
- set: function(name, value) {
- if (arguments.length == 2) {
- this.response.setHeader(name, value);
- } else {
- for (var key in name) {
- this.response.setHeader(key, name[key]);
- }
- }
+ set: function(name, value) {
+ if (arguments.length == 2) {
+ this.response.setHeader(name, value);
+ } else {
+ for (var key in name) {
+ this.response.setHeader(key, name[key]);
+ }
+ }
- return this;
- },
+ return this;
+ },
- get: function(name) {
- return this.response.getHeader(name);
- },
+ get: function(name) {
+ return this.response.getHeader(name);
+ },
- cache: function(type, time) {
- if (time) { type += ', max-age=' + time; }
+ cache: function(type, time) {
+ if (time) { type += ', max-age=' + time; }
- this.set('Cache-Control', type);
- },
+ this.set('Cache-Control', type);
+ },
- redirect: function(url) {
- var Response = this;
-
- var status = 302;
-
- if (arguments.length == 2) {
- status = arguments[0];
- url = arguments[1];
- }
+ redirect: function(url) {
+ var Response = this;
+
+ var status = 302;
- url = url || '/';
+ if (arguments.length == 2) {
+ status = arguments[0];
+ url = arguments[1];
+ }
- Response.redirect_url = url;
+ url = url || '/';
+
+ Response.redirect_url = url;
- App.series('send_response', function(error, results) {
- if (error) { throw error; }
+ App.series('send_response', function(error, results) {
+ if (error) { throw error; }
Response.response.setHeader('location', url);
- Response.response.writeHead(status, { 'Content-Type': Response.type, 'charset': Response.charset });
- Response.response.end('Redirecting to: '+ url);
- }, Response, Response.request);
- },
+ Response.response.writeHead(status, { 'Content-Type': Response.type, 'charset': Response.charset });
+ Response.response.end('Redirecting to: '+ url);
+ }, Response, Response.request);
+ },
- render: function(template, data, type, status, charset) {
- this.send(View.render(template, data), type, status, charset);
- },
+ render: function(template, data, type, status, charset) {
+ this.send(View.render(template, data), type, status, charset);
+ },
- error: function(code) {
- var self = this;
+ error: function(code) {
+ var self = this;
- App.series('request.error.' + code, function(error, results) {
- if ((error instanceof Error) || error !== true) {
- self.send('404 - Page not found', 'text/plain', code);
- }
- }, null, self.request, self);
- }
+ App.series('request.error.' + code, function(error, results) {
+ if ((error instanceof Error) || error !== true) {
+ self.send('404 - Page not found', 'text/plain', code);
+ }
+ }, null, self.request, self);
}
+ }
});
module.exports = Katana.Core.Response;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
View
304 lib/router.js
@@ -4,202 +4,202 @@ var check = require('./utils').check;
var methods = '\\*|all|options|get|post|put|delete|trace|connect|propfind|proppatch|mkcol|copy|move|lock|unlock|version-control|report|checkout|checkin|uncheckout|mkworkspace|update|label|merge|baseline-control|mkactivity|orderpatch|acl|search|patch';
Class('Katana.Core.Router', {
- have: {
- routes: {},
- cache: {}
- },
+ have: {
+ routes: {},
+ cache: {}
+ },
- methods: {
- initialize: function() {
- var Router = this;
+ methods: {
+ initialize: function() {
+ var Router = this;
- Router.SUPER();
+ Router.SUPER();
- Router.prepare(App.Config('routing').routes);
+ Router.prepare(App.Config('routing').routes);
- App.on('run', function(callback) {
- App.Config().modules.forEach(function(module) {
- Router.prepare(App.Config(module +':routing').routes || {}, module);
- });
+ App.on('run', function(callback) {
+ App.Config().modules.forEach(function(module) {
+ Router.prepare(App.Config(module +':routing').routes || {}, module);
+ });
- callback();
- });
- },
+ callback();
+ });
+ },
- prepare: function(routes, module) {
- var Router = this;
+ prepare: function(routes, module) {
+ var Router = this;
- module = module || 'app';
+ module = module || 'app';
- if (Array.isArray(routes)) {
- for (var i=0; i<routes.length; i++) {
- var exp = routes[i][0];
- var path = routes[i][1];
- var method = 'all';
+ if (Array.isArray(routes)) {
+ for (var i=0; i<routes.length; i++) {
+ var exp = routes[i][0];
+ var path = routes[i][1];
+ var method = 'all';
- var m;
+ var m;
- if (exp !== '*' && (m = exp.match(new RegExp('^('+ methods +') (.*)$', 'i')))) {
- method = m[1] == '*' ? 'all' : m[1];
- exp = m[2];
- }
+ if (exp !== '*' && (m = exp.match(new RegExp('^('+ methods +') (.*)$', 'i')))) {
+ method = m[1] == '*' ? 'all' : m[1];
+ exp = m[2];
+ }
- exp === '*' && (exp = 'all');
+ exp === '*' && (exp = 'all');
- var keys = [];
- var regexp = Router.normalize(exp, keys);
+ var keys = [];
+ var regexp = Router.normalize(exp, keys);
- if (!Router.routes[module]) { Router.routes[module] = {}; }
- if (!Router.routes[module][method]) { Router.routes[module][method] = []; }
-
- Router.routes[module][method].push({
- regexp: regexp,
- path: sanitize(path).trim('\\s\/'),
- keys: keys
- });
- }
- } else {
- for (method in routes) {
- for (var i=0; i<routes[method].length; i++) {
- var exp = routes[method][i][0];
- var path = routes[method][i][1];
-
- var keys = [];
- var regexp = Router.normalize(exp, keys);
-
- if (!Router.routes[module]) { Router.routes[module] = {}; }
- if (!Router.routes[module][method]) { Router.routes[module][method] = []; }
-
- Router.routes[module][method].push({
- regexp: regexp,
- path: sanitize(path).trim('\\s\/'),
- keys: keys
- });
- }
- }
- }
- },
+ if (!Router.routes[module]) { Router.routes[module] = {}; }
+ if (!Router.routes[module][method]) { Router.routes[module][method] = []; }
+
+ Router.routes[module][method].push({
+ regexp: regexp,
+ path: sanitize(path).trim('\\s\/'),
+ keys: keys
+ });
+ }
+ } else {
+ for (method in routes) {
+ for (var i=0; i<routes[method].length; i++) {
+ var exp = routes[method][i][0];
+ var path = routes[method][i][1];
+
+ var keys = [];
+ var regexp = Router.normalize(exp, keys);
+
+ if (!Router.routes[module]) { Router.routes[module] = {}; }
+ if (!Router.routes[module][method]) { Router.routes[module][method] = []; }
+
+ Router.routes[module][method].push({
+ regexp: regexp,
+ path: sanitize(path).trim('\\s\/'),
+ keys: keys
+ });
+ }
+ }
+ }
+ },
- normalize: function(path, keys, sensitive) {
- var i = 0;
+ normalize: function(path, keys, sensitive) {
+ var i = 0;
- if (path instanceof RegExp) {
- var k = path.source.match(/\(.*?\)/g);
+ if (path instanceof RegExp) {
+ var k = path.source.match(/\(.*?\)/g);
- for (i; i<k.length;) {
- keys.push({ name: ++i });
- }
+ for (i; i<k.length;) {
+ keys.push({ name: ++i });
+ }
- return path;
- }
+ return path;
+ }
- path = path
- .replace(/\/\(/g, '(?:/')
- .replace(/(\/)?(\.)?:(\w*)(?:(\(.*?\)))?(\?)?/g, function(_, slash, format, key, capture, optional){
- keys.push({ name: key || ++i, optional: !! optional });
- slash = slash || '';
- return ''
- + (optional ? '' : slash)
- + '(?:'
- + (optional ? slash : '')
- + (format || '') + (capture || (format && '([^/.]+?)' || '([^/]+?)')) + ')'
- + (optional || '');
- })
- .replace(/([\/.])/g, '\\$1')
- .replace(/\*/g, '(.*)');
+ path = path
+ .replace(/\/\(/g, '(?:/')
+ .replace(/(\/)?(\.)?:(\w*)(?:(\(.*?\)))?(\?)?/g, function(_, slash, format, key, capture, optional){
+ keys.push({ name: key || ++i, optional: !! optional });
+ slash = slash || '';
+ return ''
+ + (optional ? '' : slash)
+ + '(?:'
+ + (optional ? slash : '')
+ + (format || '') + (capture || (format && '([^/.]+?)' || '([^/]+?)')) + ')'
+ + (optional || '');
+ })
+ .replace(/([\/.])/g, '\\$1')
+ .replace(/\*/g, '(.*)');
- return new RegExp('^'+ path +'$', sensitive ? '' : 'i');
- },
+ return new RegExp('^'+ path +'$', sensitive ? '' : 'i');
+ },
- route: function(uri, method, config, module, use_routes) {
- var Router = this;
+ route: function(uri, method, config, module, use_routes) {
+ var Router = this;
- var routed = uri;
- var mod;
+ var routed = uri;
+ var mod;
- var route = {
- module: module || '',
- directory: config.route.directory || '',
- controller: config.route.controller || (module || 'home'),
- action: config.route.action || 'index',
- arguments: config.route.arguments || [],
- params: config.params || {},
- routed: routed
- };
+ var route = {
+ module: module || '',
+ directory: config.route.directory || '',
+ controller: config.route.controller || (module || 'home'),
+ action: config.route.action || 'index',
+ arguments: config.route.arguments || [],
+ params: config.params || {},
+ routed: routed
+ };
- use_routes = !!use_routes;
+ use_routes = !!use_routes;
- if (!uri || (config.allowed_characters && uri.match(new RegExp('^['+ config.allowed_characters +']+$', 'i')) === null)) {
- return route;
- }
+ if (!uri || (config.allowed_characters && uri.match(new RegExp('^['+ config.allowed_characters +']+$', 'i')) === null)) {
+ return route;
+ }
- var methods = [method, 'all'];
- var matched = false;
+ var methods = [method, 'all'];
+ var matched = false;
- methods.forEach(function(method) {
- if (!matched) {
- if (use_routes && Router.routes[route.module || 'app'] && Router.routes[route.module || 'app'][method]) {
- for (var i=0; i<Router.routes[route.module || 'app'][method].length; i++) {
- var r = Router.routes[route.module || 'app'][method][i];
-
- if (match = uri.match(r.regexp)) {
- matched = true;
+ methods.forEach(function(method) {
+ if (!matched) {
+ if (use_routes && Router.routes[route.module || 'app'] && Router.routes[route.module || 'app'][method]) {
+ for (var i=0; i<Router.routes[route.module || 'app'][method].length; i++) {
+ var r = Router.routes[route.module || 'app'][method][i];
+
+ if (match = uri.match(r.regexp)) {
+ matched = true;
- match = match.slice(1);
+ match = match.slice(1);
- routed = r.path;
+ routed = r.path;
- for (var j=0; j<match.length; j++) {
- var key = r.keys[j];
+ for (var j=0; j<match.length; j++) {
+ var key = r.keys[j];
- if (key) {
- route.params[key.name] = match[j];
+ if (key) {
+ route.params[key.name] = match[j];
- routed = routed.replace(':'+key.name, match[j] || '');
- } else {
- route.params[j+1] = match[j];
+ routed = routed.replace(':'+key.name, match[j] || '');
+ } else {
+ route.params[j+1] = match[j];
- routed = routed.replace(':'+(j+1), match[j] || '');
- }
- }
+ routed = routed.replace(':'+(j+1), match[j] || '');
+ }
+ }
- if (!route.module) {
- mod = routed[0] == '#';
- if (mod) {
- routed = routed.substr(1);
- }
- }
+ if (!route.module) {
+ mod = routed[0] == '#';
+ if (mod) {
+ routed = routed.substr(1);
+ }
+ }
- routed = routed.replace(/\[(.*)\](\/)?/, function(_, dir) {
- route.directory = sanitize(dir.toLowerCase()).trim('\\s\/') + '/';
+ routed = routed.replace(/\[(.*)\](\/)?/, function(_, dir) {
+ route.directory = sanitize(dir.toLowerCase()).trim('\\s\/') + '/';
- return '';
- });
+ return '';
+ });
- route.routed = routed;
+ route.routed = routed;
- break;
- }
- }
- }
- }
- });
+ break;
+ }
+ }
+ }
+ }
+ });
- var segments = routed.indexOf('/')!=-1 ? routed.split('/') : (routed!='' ? [routed] : {});
+ var segments = routed.indexOf('/')!=-1 ? routed.split('/') : (routed!='' ? [routed] : {});
if (segments.length > 0 && mod && !route.module) {
- route.module = segments.shift().toLowerCase();
+ route.module = segments.shift().toLowerCase();
- route.routed = route.routed.replace(new RegExp('^'+ route.module +'[\/]?', 'i'), '');
- }
+ route.routed = route.routed.replace(new RegExp('^'+ route.module +'[\/]?', 'i'), '');
+ }
- if (segments.length > 0) { route.controller = segments.shift().toLowerCase(); }
- if (segments.length > 0) { route.action = segments.shift().toLowerCase(); }
- if (segments.length > 0) { route.arguments = segments; }
+ if (segments.length > 0) { route.controller = segments.shift().toLowerCase(); }
+ if (segments.length > 0) { route.action = segments.shift().toLowerCase(); }
+ if (segments.length > 0) { route.arguments = segments; }
- return route;
- }
+ return route;
}
+ }
});
module.exports = new Katana.Core.Router;
View
38 lib/session/index.js
@@ -6,43 +6,27 @@ var store_type = App.Config('stores')[config.store].type;
var Store;
App.on('run', function(callback) {
- Store = require('./' + store_type);
+ Store = require('./' + store_type);
- callback();
+ callback();
});
App.on('request', function(Request, Response, callback) {
- new Store(Request.cookie.get(config.key_name), function(Session) {
- Request.session = Session;
+ new Store(Request.cookie.get(config.key_name), function(Session) {
+ Request.session = Session;
- callback();
- });
+ callback();
+ });
});
App.on('cookie.send', function(Response, Request, callback) {
- Request.session.save(function(error) {
- if (error) { Log.error('Error saving session: ' + Request.session.id); return callback(); }
+ Request.session.save(function(error) {
+ if (error) { Log.error('Error saving session: ' + Request.session.id); return callback(); }
- Request.cookie.set(config.key_name, Request.session.id, { lifetime: config.lifetime });
+ Request.cookie.set(config.key_name, Request.session.id, { lifetime: config.lifetime });
- callback();
- });
+ callback();
+ });
});
module.exports = {};
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
View
94 lib/session/memory.js
@@ -6,82 +6,54 @@ require('./store');
require('joose');
Class('Katana.Core.Session.Memory', {
- does: Katana.Core.Session.Store,
+ does: Katana.Core.Session.Store,
- have: {
- name: 'memory'
+ have: {
+ name: 'memory'
+ },
+
+ methods: {
+ BUILD: function(session_id, callback) {
+ return {
+ session_id: session_id,
+ callback: callback
+ }
},
-
- methods: {
- BUILD: function(session_id, callback) {
- return {
- session_id: session_id,
- callback: callback
- }
- },
- initialize: function(args) {
- var Session = this;
+ initialize: function(args) {
+ var Session = this;
- var session_id = args.session_id;
- var callback = args.callback;
+ var session_id = args.session_id;
+ var callback = args.callback;
- if (!session_id) {
- Session.create();
+ if (!session_id) {
+ Session.create();
- return callback(Session);
- }
+ return callback(Session);
+ }
- var sess = Memory.get('session'+ session_id);
+ var sess = Memory.get('session'+ session_id);
- if (!sess || (sess.updated_at + config.lifetime) < Date.now()) {
- Session.create();
+ if (!sess || (sess.updated_at + config.lifetime) < Date.now()) {
+ Session.create();
- return callback(Session);
- }
+ return callback(Session);
+ }
- Session.id = session_id;
- Session.data = sess.data;
+ Session.id = session_id;
+ Session.data = sess.data;
- callback(Session);
- },
+ callback(Session);
+ },
- save: function(callback) {
- var Session = this;
+ save: function(callback) {
+ var Session = this;
- Memory.set('session'+ Session.id, { data: Session.data, updated_at: Date.now() });
+ Memory.set('session'+ Session.id, { data: Session.data, updated_at: Date.now() });
- callback();
- }
+ callback();
}
+ }
});
module.exports = Katana.Core.Session.Memory;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
View
98 lib/session/redis.js
@@ -6,82 +6,54 @@ require('./store');
require('joose');
Class('Katana.Core.Session.Redis', {
- does: Katana.Core.Session.Store,
+ does: Katana.Core.Session.Store,
- have: {
- name: 'redis'
- },
+ have: {
+ name: 'redis'
+ },
- methods: {
- BUILD: function(session_id, callback) {
- return {
- session_id: session_id,
- callback: callback
- }
- },
+ methods: {
+ BUILD: function(session_id, callback) {
+ return {
+ session_id: session_id,
+ callback: callback
+ }
+ },
- initialize: function(args) {
- var Session = this;
+ initialize: function(args) {
+ var Session = this;
- var session_id = args.session_id;
- var callback = args.callback;
+ var session_id = args.session_id;
+ var callback = args.callback;
- if (!session_id) {
- Session.create();
+ if (!session_id) {
+ Session.create();
- return callback(Session);
- }
+ return callback(Session);
+ }
- Redis.get('session:'+ session_id, function(error, data) {
- if (error || data===null) {
- Session.create();
+ Redis.get('session:'+ session_id, function(error, data) {
+ if (error || data===null) {
+ Session.create();
- return callback(Session);
- }
+ return callback(Session);
+ }
- Session.id = session_id;
- Session.data = JSON.parse(data);
+ Session.id = session_id;
+ Session.data = JSON.parse(data);
- callback(Session);
- });
- },
+ callback(Session);
+ });
+ },
- save: function(callback) {
- var Session = this;
+ save: function(callback) {
+ var Session = this;
- Redis.setex('session:'+ this.id, config.lifetime, JSON.stringify(this.data), function(error) {
- callback(error);
- });
- }
+ Redis.setex('session:'+ this.id, config.lifetime, JSON.stringify(this.data), function(error) {
+ callback(error);
+ });
}
+ }
});
module.exports = Katana.Core.Session.Redis;
-
-
-
-
-
-
-
-
-
-
-
-