Permalink
Browse files

[ #@ ] Initial repository for arcabouco-js microframework

  • Loading branch information...
0 parents commit 0576e2e5ad810f68095f094f6c266b4e7d69587a @pnegri committed Oct 28, 2011
Showing with 705 additions and 0 deletions.
  1. +11 −0 .gitignore
  2. +7 −0 .npmignore
  3. +22 −0 LICENSE
  4. 0 README.md
  5. +1 −0 index.js
  6. +16 −0 lib/_monkey-fs.js
  7. +1 −0 lib/_monkey-patching.js
  8. +53 −0 lib/app.js
  9. +155 −0 lib/arcabouco.js
  10. +8 −0 lib/common.js
  11. +236 −0 lib/dispatcher.js
  12. +81 −0 lib/helper.js
  13. +29 −0 lib/language_helper.js
  14. +6 −0 lib/router.js
  15. +62 −0 lib/template.js
  16. +17 −0 package.json
@@ -0,0 +1,11 @@
+.DS_Store
+testing
+node_modules/
+lib-cov
+examples/
+examples/*/*
+examples/**
+*~
+*.lock
+*.swp
+*.out
@@ -0,0 +1,7 @@
+.git*
+docs/
+examples/
+support/
+test/
+testing.js
+.DS_Store
@@ -0,0 +1,22 @@
+(The MIT License)
+
+Copyright (c) 2011 Patrick Negri <patrick@iugu.com.br>
+
+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.
No changes.
@@ -0,0 +1 @@
+module.exports = require('./lib/arcabouco');
@@ -0,0 +1,16 @@
+Common.Fs.readdirSyncR = function( directory ) {
+ directory = Common.Path.normalize( directory );
+ var files = Common.Fs.readdirSync( directory );
+ var allFiles = new Array();
+ for (nIndex in files) {
+ var file = files[ nIndex ];
+ var stat = Common.Fs.statSync( directory + '/' + file );
+ if (stat.isFile()) allFiles.push( directory + '/' + file );
+ if (stat.isDirectory()) {
+ var dirFiles = Common.Fs.readdirSyncR( directory + '/' + file );
+ if (dirFiles.length) allFiles.concat( dirFiles );
+ }
+ }
+
+ return allFiles;
+}
@@ -0,0 +1 @@
+require('./_monkey-fs');
@@ -0,0 +1,53 @@
+/**
+* Lets Yell Server
+*/
+
+var SERVER_VERSION = '0.1 Pre-Alpha';
+var SERVER_NAME = 'Lets Yell - Version: ' + SERVER_VERSION;
+
+console.log(SERVER_NAME);
+console.log('loading...');
+
+// Load required modules
+var gmHttp = require('http');
+var gmFS = require('fs');
+var gmUrl = require('url');
+var gmOAP = require(__dirname + '/lib/oauth_provider');
+
+// Loaded in Global Javascript Space
+global.I18N = require('i18n');
+global.App = require(__dirname + '/configuration');
+global.Helper = require(__dirname + '/lib/helper');
+global.Template = require(__dirname + '/lib/template');
+global.OAuth = require('oauth').OAuth;
+
+// Output Debugging Information
+global.bDebugging = true;
+
+// If we are using Airbrake for Exceptions
+if (App.bUseAirbrake) {
+ console.log('Using Airbrake for Exception');
+ global.Airbrake = require('airbrake').createClient(App.sAirbrakeAPI);
+ global.Airbrake.host = App.sBaseURI;
+ global.Airbrake.env = App.sEnv;
+ Airbrake.handleExceptions();
+}
+
+I18N.aLanguages = {
+ locales: ['en', 'pt-BR' ],
+ names: ['English', 'Portugues' ]
+}
+
+I18N.configure({
+ // setup some locales - other locales default to en silently
+ locales:I18N.aLanguages.locales,
+ // where to register __() and __n() to, might be "global" if you know what you are doing
+ register: global
+});
+
+var gmDispatcher = require('./lib/dispatcher');
+
+// Create a server with the dispatcher module as callback for requests
+var mServer = gmHttp.createServer(gmDispatcher.dispatchRequest).listen(8888);
+
+console.log('Waiting for requests...');
@@ -0,0 +1,155 @@
+// Arcabouco-JS is a Lightweight framework without garbage with i18n/mobile/html5 built-in support
+
+var Common = require('./common');
+require('./_monkey-patching.js'); // Yes - i know its ugly butttt...
+
+var Arcabouco = function( config ) {
+ this.config = config;
+
+ // TODO: check for required directives
+ if (!this.config.baseDirectory) {
+ console.log('ERROR: Configuration doesnt have baseDirectory directive');
+ process.exit(1);
+ }
+
+ //var files = Common.Fs.readdirSyncR( this.config.baseDirectory );
+ //console.log(files);
+}
+
+Arcabouco.prototype = {
+ i18n: require('i18n'),
+ config: {},
+ httpServer: null,
+ routes: [],
+ controllerRoutes: [],
+ controllerIndexes: [],
+ controllerInstances: [],
+ mountApplication: null,
+ _routingChanged: false,
+ loadController:
+ function( controller_filename ) {
+ if (!controller_filename.match(/\.js$/gi)) return;
+
+ var this_controller = require( controller_filename );
+ var controller_index = this.controllerInstances.push( this_controller );
+
+ if (this_controller.getRoutes == undefined) return;
+ var controller_routes = this_controller.getRoutes();
+
+ for (var route_path in controller_routes) {
+ this.controllerIndexes[ route_path ] = {
+ 'method' : controller_routes[ route_path ],
+ 'object' : controller_index-1
+ }
+ this.controllerRoutes.push( route_path );
+ this._routingChanged = true;
+ }
+ },
+ buildRouting:
+ function() {
+ var sorted_cr = this.controllerRoutes.sort().reverse();
+ this.routes = [];
+
+ for (var index in sorted_cr) {
+ var pattern = sorted_cr[ index ];
+ var params = [];
+
+ var result = pattern.replace(/\{(.*?)\}/g,
+ function( match, sub1, pos, whole ) {
+ params.push( sub1 );
+ return "([^\/]+?)";
+ }
+ );
+
+ result = '^' + result + "(\\/?\$|\\/?\\?.*$)";
+
+ this.routes.push( {
+ regex : ( new RegExp( result ) ),
+ params : params,
+ index : pattern
+ });
+ }
+
+ if (global.debugging) {
+ console.log( 'DEBUG: Build routing array:' );
+ console.log( this.routes );
+ }
+ },
+ dispatchRequest:
+ function( request, response ) {
+ request.setEncoding( 'utf-8' );
+
+ var data = '';
+ var url = Common.Url.parse( request.url, true );
+ var path_name = url.pathname;
+ var query = url.query;
+
+ path_name = path_name.replace(/\/$g/,'');
+ if (path_name == '') path_name = '/';
+
+ if (global.debugging) {
+ console.log('DEBUG: HTTP Request: ' + path_name);
+ }
+
+ request.addListener('data',
+ function onData( data_chunk ) {
+ data += data_chunk;
+ }
+ );
+
+ // Methods PlaceHolder
+ response.respondWith = null;
+ response.redirectTo = null;
+ response.respondError = null;
+
+ request.addListener('end',
+ function onRequest() {
+ var url_parts = null;
+ for (index in this.routes) {
+ var route = this.routes[ index ];
+ path_name.replace( route.regex,
+ function matchRoute( match ) {
+ url_parts = {};
+ for (var i=0; i<route.params.length; i++) url_parts[ route.params[i] ] = arguments[i+1];
+ url_parts.response = response;
+ url_parts.query = query;
+ url_parts.path_name = path_name;
+ url_parts.data = data;
+ url_parts.route = route.index;
+ url_parts.request = request;
+ }
+ );
+ if (url_parts) break;
+ }
+
+ var routed = false;
+
+ // Try? echo Robot?
+ // Error Handling Here
+ if (url_parts) {
+ var controller_index = this.controllerIndexes[ url_parts.route ];
+ var controller = this.controllerInstances[ controller_index.object ];
+ if ( (controller[ controller_index.method ] != undefined) && (controller[ controller_index.method]( url_parts )) ); routed = true;
+ }
+
+ if (!url_parts || !routed) {
+ // Response with Template system a 404
+ response.writeHead( 404, {"Content-Type":"text/plain"});
+ response.write("404 Not Found.");
+ response.end();
+ }
+ }.bind(this)
+ );
+ },
+ run:
+ function() {
+ this.createServer( 8888 );
+ },
+ createServer:
+ function( serverPort ) {
+ this.httpServer = Common.Http.createServer( this.dispatchRequest.bind(this) ).listen( serverPort );
+ return this.httpServer;
+ }
+};
+
+module.exports = Arcabouco;
@@ -0,0 +1,8 @@
+Common = {
+ Http : require('http'),
+ Fs : require('fs'),
+ Path : require('path'),
+ Url : require('url')
+}
+
+module.exports = Common;
Oops, something went wrong.

0 comments on commit 0576e2e

Please sign in to comment.