diff --git a/buildbug.js b/buildbug.js index 57ce42a..3a446cb 100644 --- a/buildbug.js +++ b/buildbug.js @@ -42,6 +42,7 @@ buildProperties({ main: "./lib/AirBugServer.js", dependencies: { bugpack: "https://s3.amazonaws.com/airbug/bugpack-0.0.5.tgz", + connect: "2.x", express: "3.0.x", mu2express: "0.0.x", "mongodb": ">=1.2.11", @@ -55,10 +56,11 @@ buildProperties({ sourcePaths: [ "./projects/airbugserver/js/src", '../bugjs/projects/annotate/js/src', - '../bugjs/projects/bugboil/js/src', '../bugjs/projects/bugflow/js/src', '../bugjs/projects/bugfs/js/src', + '../bugjs/projects/bugioc/js/src', '../bugjs/projects/bugjs/js/src', + '../bugjs/projects/bugroutes/js/src', '../bugjs/projects/bugtrace/js/src', '../bugunit/projects/bugunit/js/src' // "../bugjs/projects/clientjs/js/src" @@ -68,7 +70,8 @@ buildProperties({ "../bugunit/projects/bugunit/js/scripts" ], testPaths: [ - "../bugjs/projects/bugjs/js/test" + "../bugjs/projects/bugjs/js/test", + '../bugjs/projects/bugroutes/js/test' ], //TODO BRN: These static paths are temporary until we get the client js server working. @@ -150,7 +153,6 @@ buildProperties({ sourcePaths: [ '../bugjs/projects/annotate/js/src', '../bugjs/projects/bugjs/js/src', - '../bugjs/projects/bugboil/js/src', '../bugjs/projects/bugflow/js/src', '../bugjs/projects/bugfs/js/src', '../bugjs/projects/bugtrace/js/src', @@ -247,7 +249,7 @@ buildTarget('local').buildFlow( packageVersion: buildProject.getProperty("server.packageJson.version") } }), - /*targetTask('startNodeModuleTests', { + targetTask('startNodeModuleTests', { init: function(task, buildProject, properties) { var packedNodePackage = nodejs.findPackedNodePackage( buildProject.getProperty("server.packageJson.name"), @@ -257,7 +259,7 @@ buildTarget('local').buildFlow( modulePath: packedNodePackage.getFilePath() }); } - }),*/ + }), targetTask("s3EnsureBucket", { properties: { bucket: buildProject.getProperty("local-bucket") diff --git a/projects/airbugserver/js/src/AirBugApplication.js b/projects/airbugserver/js/src/AirBugApplication.js index a431da9..3a567dc 100644 --- a/projects/airbugserver/js/src/AirBugApplication.js +++ b/projects/airbugserver/js/src/AirBugApplication.js @@ -71,4 +71,4 @@ var AirBugApplication = Class.extend(Obj, { // Exports //------------------------------------------------------------------------------- -bugpack.export('airbug.AirBugApplication', AirBugApplication); +bugpack.export('airbugserver.AirBugApplication', AirBugApplication); diff --git a/projects/airbugserver/js/src/AirBugConfiguration.js b/projects/airbugserver/js/src/AirBugConfiguration.js index 9052f3f..2dcb346 100644 --- a/projects/airbugserver/js/src/AirBugConfiguration.js +++ b/projects/airbugserver/js/src/AirBugConfiguration.js @@ -86,6 +86,20 @@ var AirBugConfiguration = Class.extend(Obj, { * @type {AirBugServer} */ this._airBugServer = null; + + /** + * @private + * @type {{ + * port: number, + * mongoDbIp: string + * }} + */ + this._config = null; + + /** + * @type {string} + */ + this._configFilePath = null; }, @@ -100,11 +114,6 @@ var AirBugConfiguration = Class.extend(Obj, { this._airBugServer.start(); }, - - //------------------------------------------------------------------------------- - // Configuration Methods - //------------------------------------------------------------------------------- - /** * @return {AirBugServer} */ @@ -120,6 +129,12 @@ var AirBugConfiguration = Class.extend(Obj, { return new ChatMessagesApi(); }, + config: function(){ + this._configFilePath = path.resolve(__dirname, '../config.json'); + this._config = this.loadConfig(this.configFilePath); + return this._config; + }, + /** * @return {ConversationsApi} */ @@ -131,7 +146,7 @@ var AirBugConfiguration = Class.extend(Obj, { * @return {ExpressServer} */ expressApp: function() { - return new ExpressApp(); + return new ExpressApp().initialize(); }, /** @@ -148,6 +163,9 @@ var AirBugConfiguration = Class.extend(Obj, { return new ExpressServer(); }, + /** + * @return {Mongoose} + */ mongoose: function(){ return mongoose; }, @@ -189,6 +207,40 @@ var AirBugConfiguration = Class.extend(Obj, { */ usersApi: function(){ return new UsersApi(); + }, + + //------------------------------------------------------------------------------- + // Private Methods + //------------------------------------------------------------------------------- + + /* + * @private + * @param {?string=} configPath + * @return {{ + * port: number, + * mongoDbIp: string + * }} + **/ + loadConfig: function(configPath){ + var config; + var defaults = { + port: 8000, + mongoDbIp: "localhost" + }; + + if (BugFs.existsSync(configPath)) { + try { + config = JSON.parse(BugFs.readFileSync(configPath, 'utf8')); + } catch(error) { + console.log(configPath, "could not be parsed. Invalid JSON."); + console.log(AirBugServer, "config set to defaults."); + return defaults; + } finally { + return config; + } + } else { + return defaults; + } } }); @@ -212,6 +264,7 @@ annotate(AirBugServerConfiguration).with( //------------------------------------------------------------------------------- module("airBugServer") .properties([ + property("config").ref("config"), property("expressServer").ref("expressServer"), property("mongoose").ref("mongoose"), property("socketManager").ref("socketManager") @@ -220,12 +273,14 @@ annotate(AirBugServerConfiguration).with( //------------------------------------------------------------------------------- // Express //------------------------------------------------------------------------------- - module("expressApp"), - module("expressRoutes"), - module("expressServer") + module("expressApp") .properties([ + property("config").ref("config"), + property("expressRoutes").re("expressRoutes"), property("sessionStore").ref("sessionStore") ]), + module("expressRoutes"), + module("expressServer"), //------------------------------------------------------------------------------- // Sockets @@ -233,7 +288,6 @@ annotate(AirBugServerConfiguration).with( module("socketManager") .properties([ property("server").ref("expressServer"), - // property("socketIoManager").ref("socketIoManager"), property("socketsMap").ref("socketsMap") ]), module("socketsMap"), diff --git a/projects/airbugserver/js/src/AirBugServer.js b/projects/airbugserver/js/src/AirBugServer.js index fa01ca5..2754022 100644 --- a/projects/airbugserver/js/src/AirBugServer.js +++ b/projects/airbugserver/js/src/AirBugServer.js @@ -34,7 +34,6 @@ var path = require('path'); //------------------------------------------------------------------------------- var BugFs = bugpack.require('bugfs.BugFs'); -// var Map = bugpack.require('Map'); var ExpressServer = bugpack.require('airbugserver.ExpressServer'); var SocketManager = bugpack.require('airbugserver.SocketManager'); @@ -59,20 +58,28 @@ var AirBugServer = Class.extend(Obj, { // Variables //------------------------------------------------------------------------------- + /* + * @type {{ + * port: number, + * mongoDbIp: string + * }} + **/ + this.config = null; + /* * @type {ExpressServer} **/ - this.expressServer = null; + this.expressServer = null; /* - * @type {mongoose} + * @type {Mongoose} **/ - this.mongoose = null; + this.mongoose = null; /* * @type {SocketManager} **/ - this.socketManager = null; + this.socketManager = null; } @@ -80,29 +87,35 @@ var AirBugServer = Class.extend(Obj, { // Public Methods //------------------------------------------------------------------------------- + /* + **/ start: function() { + var configPath = path.resolve(__dirname, '../config.json'); + var config = this.loadConfig(configPath); var expressServer = this.expressServer.start(); var mongoose = this.mongoose; var socketManager = this.socketManager.initialize(expressServer); var cookieParser = expressServer.getCookieParser(); var sessionStore = expressServer.getSessionStore(); var sessionKey = expressServer.getSessionKey(); - - socketManager.enablesockets(cookieParser, sessionStore, sessionKey); - - var configPath = path.resolve(__dirname, '../config.json'); - var config = { - port: 8000, - mongoDbIp: "localhost" - }; - if (BugFs.existsSync(configPath)) { - config = JSON.parse(BugFs.readFileSync(configPath, 'utf8')); - } + //------------------------------------------------------------------------------- + // Connect to MongoDB + //------------------------------------------------------------------------------- mongoose.connect('mongodb://' + config.mongoDbIp + '/airbug'); + //------------------------------------------------------------------------------- + // Enable Sockets + //------------------------------------------------------------------------------- + + socketManager.enablesockets(cookieParser, sessionStore, sessionKey); + + //------------------------------------------------------------------------------- + // + //------------------------------------------------------------------------------- + //var clientJSServer = new ClientJSServer(app); @@ -128,6 +141,11 @@ var AirBugServer = Class.extend(Obj, { });*/ } + + //------------------------------------------------------------------------------- + // Private Methods + //------------------------------------------------------------------------------- + }); @@ -135,4 +153,4 @@ var AirBugServer = Class.extend(Obj, { // Exports //------------------------------------------------------------------------------- -bugpack.export('airbug.AirBugServer', AirBugServer); +bugpack.export('airbugserver.AirBugServer', AirBugServer); diff --git a/projects/airbugserver/js/src/Express/ExpressApp.js b/projects/airbugserver/js/src/Express/ExpressApp.js index 4f9d66d..487db7c 100644 --- a/projects/airbugserver/js/src/Express/ExpressApp.js +++ b/projects/airbugserver/js/src/Express/ExpressApp.js @@ -49,14 +49,36 @@ var ExpressApp = Class.extend(Obj, { // Variables //------------------------------------------------------------------------------- + /* + * @type {express.app} + **/ this.app = null; + /* + * @type {{}} + **/ + this.config = null; // Injected by AirBugConfiguration + + this.expressRoutes = null; + + /* + * @type {} + **/ this.secret = null; + /* + * @type {} + **/ this.sessionKey = null; + /* + * @type {} + **/ this.cookieParser = null; + /* + * @type {} + **/ this.sessionStore = null; }, @@ -95,26 +117,6 @@ var ExpressApp = Class.extend(Obj, { **/ var sessionStore = this.sessionStore = new connect.middleware.session.MemoryStore(); - return this; - }, - - start: function() { - - var app = this.app; - var configPath = path.resolve(__dirname, '../config.json'); - var config = { - port: 8000, - mongoDbIp: "localhost" - }; - var secret = 'some secret'; - var sessionKey = 'express.sid'; - var cookieParser = express.cookieParser(secret); - var sessionStore = new connect.middleware.session.MemoryStore(); - - if (BugFs.existsSync(configPath)) { - config = JSON.parse(BugFs.readFileSync(configPath, 'utf8')); - } - // Configure App //------------------------------------------------------------------------------- this.configure(app); @@ -127,6 +129,8 @@ var ExpressApp = Class.extend(Obj, { //------------------------------------------------------------------------------- this.enableGracefulShutdown(app); + return this; + }, @@ -163,7 +167,7 @@ var ExpressApp = Class.extend(Obj, { /* * @private - * @param {} app + * @param {express.app} app **/ configure: function(app){ app.configure(function(){ @@ -193,7 +197,7 @@ var ExpressApp = Class.extend(Obj, { /* * @private - * @param {} app + * @param {express.app} app **/ enableRoutes: function(app){ @@ -201,7 +205,7 @@ var ExpressApp = Class.extend(Obj, { /* * @private - * @param {} app + * @param {express.app} app **/ enableGracefulShutdown: function(app){ process.on('SIGTERM', function () { @@ -221,4 +225,4 @@ var ExpressApp = Class.extend(Obj, { // Exports //------------------------------------------------------------------------------- -bugpack.export('airbugserver.ExpressServer', ExpressServer); +bugpack.export('airbugserver.ExpressApp', ExpressApp); diff --git a/projects/airbugserver/js/src/Express/ExpressServer.js b/projects/airbugserver/js/src/Express/ExpressServer.js index 8ed21f4..f7282d3 100644 --- a/projects/airbugserver/js/src/Express/ExpressServer.js +++ b/projects/airbugserver/js/src/Express/ExpressServer.js @@ -6,6 +6,9 @@ //@Export('ExpressServer') +//@Require('Class') +//@Require('Obj') + //------------------------------------------------------------------------------- // Common Modules @@ -19,6 +22,9 @@ var http = require('http'); // BugPack //------------------------------------------------------------------------------- +var Class = bugpack.require('Class'); +var Obj = bugpack.require('Obj'); + //------------------------------------------------------------------------------- // Build App @@ -28,7 +34,7 @@ var ExpressServer = Class.extend(Obj, { _constructor: function(){ - this.super(); + this._super(); //------------------------------------------------------------------------------- // Variables @@ -53,10 +59,13 @@ var ExpressServer = Class.extend(Obj, { // Create Server //------------------------------------------------------------------------------- - return http.createServer(app).listen(app.get('port'), function(){ + var server = http.createServer(app); + server.listen(app.get('port'), function(){ console.log("Express server listening on port " + app.get('port')); }); + return server; + }, @@ -73,7 +82,7 @@ var ExpressServer = Class.extend(Obj, { getSessionKey: function(){ return this.expressApp.getSessionKey; } - +}); //------------------------------------------------------------------------------- // Exports diff --git a/projects/airbugserver/js/src/Models/ChatMessage.js b/projects/airbugserver/js/src/Models/ChatMessage.js index 8ec2992..6be5076 100644 --- a/projects/airbugserver/js/src/Models/ChatMessage.js +++ b/projects/airbugserver/js/src/Models/ChatMessage.js @@ -4,7 +4,7 @@ //@Package('airbugserver') -//@Export('User') +//@Export('ChatMessage') //@Require('airbugserver.ChatMessageSchema') diff --git a/projects/airbugserver/js/src/Routes/ExpressRoutes/ExpressRoutes.js b/projects/airbugserver/js/src/Routes/ExpressRoutes/ExpressRoutes.js index e31d6d0..f6f91c0 100644 --- a/projects/airbugserver/js/src/Routes/ExpressRoutes/ExpressRoutes.js +++ b/projects/airbugserver/js/src/Routes/ExpressRoutes/ExpressRoutes.js @@ -8,7 +8,8 @@ //@Require('Class') //@Require('Obj') -//@Require('airbugserver.Route') +//@Require('bugroutes.ExpressRoute') +//@Require('bugroutes.Routes') //------------------------------------------------------------------------------- // Common Modules @@ -21,27 +22,26 @@ var bugpack = require('bugpack').context(); // BugPack //------------------------------------------------------------------------------- -var ExpressRoutes = { - - enableRoutes: function(){ - - }, - - routes: [ - new Route('/alpha', function(req, res){ - res.render('alpha', { - title: 'airbug', - production: config.production - }, 'get') - ] -}; - -// app.get('/alpha', function(req, res){ -// res.render('alpha', { -// title: 'airbug', -// production: config.production -// }); -// }); +var Routes = require('bugroutes.Routes'); +var ExpressRoute = require('bugroutes.ExpressRoute'); + + +//------------------------------------------------------------------------------- +// Routes +//------------------------------------------------------------------------------- +var getAlpha = new ExpressRoute('get', '/alpha', function(req, res){ + res.render('alpha', { + title: 'airbug', + production: config.production + }); +}); + +var routes = [getAlpha]; +//------------------------------------------------------------------------------- +// Declare Class +//------------------------------------------------------------------------------- + +var ExpressRoutes = new Routes(routes); //------------------------------------------------------------------------------- // Exports diff --git a/projects/airbugserver/js/src/Routes/Route.js b/projects/airbugserver/js/src/Routes/Route.js deleted file mode 100644 index 2d653f1..0000000 --- a/projects/airbugserver/js/src/Routes/Route.js +++ /dev/null @@ -1,20 +0,0 @@ -Route = Class.extend(Obj, { - _constructor: function(name, listener, method){ - this.super(); - - this.name = name; - - this.listener = listener; - - this.method = method; - }, - - enableOnSocket: function(socket){ - var _this = this; - socket.on(_this.name, _this.listener); - }, - - enableOnApp: function(app){ - app[this.method](this.name, this.listener); - } -}); diff --git a/projects/airbugserver/js/test/SocketsMapTest.js b/projects/airbugserver/js/test/SocketsMapTest.js index 6b476cb..655f306 100644 --- a/projects/airbugserver/js/test/SocketsMapTest.js +++ b/projects/airbugserver/js/test/SocketsMapTest.js @@ -20,8 +20,9 @@ var bugpack = require('bugpack').context(); // BugPack //------------------------------------------------------------------------------- -var Annotate = bugpack.require('annotate.Annotate'); -var TestAnnotation = bugpack.require('bugunit-annotate.TestAnnotation'); +var Annotate = bugpack.require('annotate.Annotate'); +var SocketsMap = bugpack.require('airbugserver.SocketsMap'); +var TestAnnotation = bugpack.require('bugunit-annotate.TestAnnotation'); //------------------------------------------------------------------------------- @@ -42,7 +43,7 @@ var associateSocketWithSessionTest = { // Setup Test //------------------------------------------------------------------------------- setup: function(){ - this.socketsMap = new SocketsMap()initialize(); + this.socketsMap = new SocketsMap().initialize(); this.socketsMap.findSocketsBySessionIdTest }, @@ -69,7 +70,7 @@ var associateUserWithSessionTest = { // Setup Test //------------------------------------------------------------------------------- setup: function(){ - this.socketsMap = new SocketsMap()initialize(); + this.socketsMap = new SocketsMap().initialize(); }, //------------------------------------------------------------------------------- @@ -89,7 +90,7 @@ var findSocketsBySessionIdTest = { // Setup Test //------------------------------------------------------------------------------- setup: function(){ - this.socketsMap = new SocketsMap()initialize(); + this.socketsMap = new SocketsMap().initialize(); }, //------------------------------------------------------------------------------- @@ -109,7 +110,7 @@ var findSocketsBySessionIdTest = { // Setup Test //------------------------------------------------------------------------------- setup: function(){ - this.socketsMap = new SocketsMap()initialize(); + this.socketsMap = new SocketsMap().initialize(); }, //------------------------------------------------------------------------------- @@ -129,7 +130,7 @@ var findSocketsByUserIdTest = { // Setup Test //------------------------------------------------------------------------------- setup: function(){ - this.socketsMap = new SocketsMap()initialize(); + this.socketsMap = new SocketsMap().initialize(); }, //-------------------------------------------------------------------------------