Permalink
Browse files

First commit

  • Loading branch information...
0 parents commit 1ae6c608066cf7ec6b938ef316560b955ac72137 lollo committed Oct 12, 2012
@@ -0,0 +1 @@
+/node_modules/
20 LICENSE
@@ -0,0 +1,20 @@
+(The MIT License)
+
+Copyright (C) 2012 Lorenzo Tabacchini
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of
+this software and associated documentation files (the "Software"), to deal in
+the Software without restriction, including without limitation the rights to
+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
+the Software, and to permit persons to whom the Software is furnished to do so,
+subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
+FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
+COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
+IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,4 @@
+test:
+ @./node_modules/.bin/mocha
+
+.PHONY: test
@@ -0,0 +1,39 @@
+#agni-framework
+
+Simple and intuitive MVC web framework for node.js.
+
+##Overview
+
+Agni is a web framework built on top of Express, based on the principle
+of _convention over configuration_.
+
+##Usage
+
+To create an application, use the command-line tool [agni](https://github.com/lortabac/agni).
+
+## Documentation
+
+http://lortabac.github.com/agni/
+
+## License
+
+(The MIT License)
+
+Copyright (C) 2012 Lorenzo Tabacchini
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of
+this software and associated documentation files (the "Software"), to deal in
+the Software without restriction, including without limitation the rights to
+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
+the Software, and to permit persons to whom the Software is furnished to do so,
+subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
+FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
+COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
+IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,48 @@
+/**
+ * Module dependencies
+ */
+
+var path = require('path');
+
+/**
+ * Config constructor
+ *
+ * @param {String} Application directory
+ * @api public
+ */
+
+function Config(appDir) {
+ var configPath = path.join(appDir, 'config', 'config');
+ this.settings = require(configPath);
+}
+
+/**
+ * Get a configuration item
+ *
+ * @param {String} Item
+ * @api public
+ */
+
+Config.prototype.get = function(item) {
+ //Look for the environment-specific setting
+ var env = this.settings.current;
+ if(! env) {
+ //If no environment is specified, it defaults to 'development'
+ env = 'development';
+ }
+
+ if(this.settings.hasOwnProperty(env) &&
+ this.settings[env].hasOwnProperty(item)) {
+ return this.settings[env][item];
+ }
+
+ //Look for item in the 'all' settings
+ if(this.settings.hasOwnProperty('all') &&
+ this.settings.all.hasOwnProperty(item)) {
+ return this.settings.all[item];
+ }
+
+ return false;
+}
+
+module.exports = Config;
@@ -0,0 +1,100 @@
+/**
+ * Module dependencies
+ */
+
+var express = require('express');
+var h = require('./helpers');
+var NestedView = require('./nested_view');
+var factory = require('./factory');
+
+/**
+ * Controller constructor
+ *
+ * @param {String} Application directory
+ * @param {Object} Request object
+ * @param {Object} Response object
+ * @param {String} Controller name
+ * @api public
+ */
+
+function Controller(appDir, req, res, path) {
+ this._appDir = appDir;
+ this._path = path;
+ this.req = req;
+ this.res = res;
+ this.session = express.cookieSession;
+}
+
+/**
+ * Render a view
+ *
+ * @api public
+ */
+
+Controller.prototype.render = function(view, locals, callback) {
+ if((typeof view) === 'object') {
+ locals = view;
+ callback = locals;
+ } else if((typeof view) === 'function') {
+ callback = view;
+ view = '';
+ locals = {}
+ }
+
+ if(! view) {
+ view = h.ltrimSlash(this._path);
+ }
+
+ if(! locals) {
+ locals = {}
+ }
+
+ locals['agni'] = {
+ service: this.service,
+ model: this.model
+ };
+
+ this.res.render(view, locals, callback);
+}
+
+/**
+ * Nest a view inside another
+ *
+ * @api public
+ */
+
+Controller.prototype.nest = function(view, locals) {
+ if(! view) {
+ view = h.ltrimSlash(this._path);
+ }
+
+ return (new NestedView(this.res, view, locals));
+}
+
+/**
+ * Service factory
+ *
+ * @api public
+ */
+
+Controller.prototype.service = function(name) {
+ if(! name) {
+ name = h.ltrimSlash(this._path);
+ }
+ return factory.getService(this._appDir, name);
+}
+
+/**
+ * Model factory
+ *
+ * @api public
+ */
+
+Controller.prototype.model = function(name) {
+ if(! name) {
+ name = h.ltrimSlash(this._path);
+ }
+ return factory.getModel(this._appDir, name);
+}
+
+module.exports = Controller;
@@ -0,0 +1,79 @@
+/**
+ * Module dependencies
+ */
+
+var fs = require('fs');
+var path = require('path');
+
+/**
+ * Controller constructor
+ *
+ * @param {String} Controllers directory
+ * @api public
+ */
+
+function Controllers(appDir) {
+ //Controller directory
+ this.controllerDir = path.join(appDir, 'controllers');
+ //Controller list
+ this.controllers = {};
+}
+
+/**
+ * Build controller list
+ *
+ * @param {Function} Callback
+ * @api public
+ */
+
+Controllers.prototype.buildList = function(done) {
+ this._processDir('');
+ done(this.controllers);
+}
+
+/**
+ * Iterate through a directory
+ *
+ * @api private
+ */
+
+Controllers.prototype._processDir = function(dir) {
+ var self = this;
+
+ //Read directory
+ var absDir = path.join(this.controllerDir, dir);
+ var list = fs.readdirSync(absDir);
+
+ //Iterate on directory contents
+ list.forEach(function(element, index, array) {
+ var elementPath = path.join(dir, element);
+ var stat = fs.statSync(path.join(self.controllerDir, elementPath));
+
+ if(stat.isFile()) {
+ //If it is a file, add to the controller list
+ self._processFile(elementPath);
+
+ } else if(stat.isDirectory()) {
+ //If it is a directory, call this function recursively
+ self._processDir(elementPath);
+ }
+ });
+ return true;
+}
+
+/**
+ * Add a file to the controller list
+ *
+ * @api private
+ */
+
+Controllers.prototype._processFile = function(file) {
+ //Remove extension
+ var basename = file.substring(0, file.lastIndexOf('.'));
+
+ //Store controller
+ var module = require(path.join(this.controllerDir, basename));
+ this.controllers[('/' + basename)] = module;
+}
+
+module.exports = Controllers;
@@ -0,0 +1,57 @@
+/**
+ * Module dependencies
+ */
+
+var path = require('path');
+
+/**
+ * Model factory
+ */
+
+var getModel = exports.getModel = function(appDir, name) {
+ var model = require(path.join(appDir, 'models', name));
+ return model;
+}
+
+/**
+ * Service factory
+ */
+
+var getService = exports.getService = function(appDir, name) {
+ var Service = require(path.join(appDir, 'services', name));
+
+ if((typeof Service) === 'function') {
+
+ Service.prototype.service = function(serviceName) {
+ return getService(appDir, serviceName);
+ }
+
+ Service.prototype.model = function(modelName) {
+ if(! modelName) {
+ modelName = name;
+ }
+ return getModel(name);
+ }
+
+ Service.construct = function() {
+ var self = Service.prototype;
+ Service.apply(self, arguments);
+ return self;
+ }
+
+ } else {
+
+ Service.service = function(serviceName) {
+ return getService(appDir, serviceName);
+ }
+
+ Service.model = function(modelName) {
+ if(! modelName) {
+ modelName = name;
+ }
+ return getModel(name);
+ }
+ }
+
+ return Service;
+}
Oops, something went wrong.

0 comments on commit 1ae6c60

Please sign in to comment.