Skip to content

Commit

Permalink
-stablizes airbugserver to the point of being able to be built and st…
Browse files Browse the repository at this point in the history
…arted without error

-actual chat functionality (ApiController methods and routes) needs to be built out

[airbug/airbug#89] Airbug. Airbug MVP Part I: Chat Basics
  • Loading branch information
sungchoi committed May 21, 2013
1 parent d47343c commit 383f229
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 31 deletions.
61 changes: 57 additions & 4 deletions projects/airbugserver/js/src/AirBugConfiguration.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
//@Require('Class')
//@Require('Obj')
//@Require('annotate.Annotate')
//@Require('bugflow.BugFlow')
//@Require('bugfs.BugFs')
//@Require('bugioc.ArgAnnotation')
//@Require('bugioc.ConfigurationAnnotation')
Expand Down Expand Up @@ -43,6 +44,7 @@ var path = require('path');
// BugPack
//-------------------------------------------------------------------------------

var BugFlow = bugpack.require('bugflow.BugFlow');
var BugFs = bugpack.require('bugfs.BugFs');
var Class = bugpack.require('Class');
var Obj = bugpack.require('Obj');
Expand Down Expand Up @@ -77,6 +79,9 @@ var configuration = ConfigurationAnnotation.configuration;
var module = ModuleAnnotation.module;
var property = PropertyAnnotation.property;

var $series = BugFlow.$series;
var $task = BugFlow.$task;


//-------------------------------------------------------------------------------
// Declare Class
Expand Down Expand Up @@ -127,9 +132,56 @@ var AirBugConfiguration = Class.extend(Obj, {
*
*/
initializeConfiguration: function() {
this._expressApp.initialize();
this._socketManager.initialize();
this._airBugServer.start();
var _this = this;
console.log("Initializing AirBugConfiguration");
$series([
$task(function(flow){
console.log("Initializing expressApp");

_this._expressApp.initialize(function(error){
if(!error){
console.log("expressApp initialized");
}
flow.complete(error);
});
}),
$task(function(flow){
console.log("starting expressServer");

_this._expressServer.start(function(error){
if(!error){
console.log("expressServer started");
}
flow.complete(error);
});
}),
$task(function(flow){
console.log("Initializing socketManager");

_this._socketManager.initialize(function(error){
if(!error){
console.log("socketManager initialized");
}
flow.complete(error);
});
}),
$task(function(flow){
console.log("Initializing airBugServer");

_this._airBugServer.start(function(error){
if(!error){
console.log("airBugServer initialized");
}
flow.complete(error);
});
}),
]).execute(function(error){
if(!error){
console.log("AirBugConfiguration successfully initialized.")
} else {
console.log(error);
}
});
},

/**
Expand Down Expand Up @@ -181,7 +233,8 @@ var AirBugConfiguration = Class.extend(Obj, {
* @return {ExpressServer}
*/
expressServer: function(expressApp) {
return new ExpressServer(expressApp);
this._expressServer = new ExpressServer(expressApp);
return this._expressServer;
},

/**
Expand Down
10 changes: 6 additions & 4 deletions projects/airbugserver/js/src/AirBugServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,13 @@ var AirBugServer = Class.extend(Obj, {

/*
**/
start: function() {
start: function(callback) {

var callback = callback || function(){};
var config = this.config;
var expressServer = this.expressServer.start();
var expressServer = this.expressServer;
var mongoose = this.mongoose;
var socketManager = this.socketManager.initialize(expressServer);
var socketManager = this.socketManager;
var cookieParser = expressServer.getCookieParser();
var sessionStore = expressServer.getSessionStore();
var sessionKey = expressServer.getSessionKey();
Expand All @@ -111,7 +112,7 @@ var AirBugServer = Class.extend(Obj, {
// Enable Sockets
//-------------------------------------------------------------------------------

socketManager.enablesockets(cookieParser, sessionStore, sessionKey);
socketManager.enableSockets(cookieParser, sessionStore, sessionKey);

//-------------------------------------------------------------------------------
//
Expand Down Expand Up @@ -141,6 +142,7 @@ var AirBugServer = Class.extend(Obj, {
}
});*/

callback();
}
});

Expand Down
71 changes: 54 additions & 17 deletions projects/airbugserver/js/src/Express/ExpressApp.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,17 @@ var path = require('path');
// BugPack
//-------------------------------------------------------------------------------

var BugFlow = bugpack.require('bugflow.BugFlow');
var BugFs = bugpack.require('bugfs.BugFs');
var Class = bugpack.require('Class');
var Obj = bugpack.require('Obj');
var SocketManager = bugpack.require('airbugserver.SocketManager');

//var ClientJSServer = bugpack.require('clientjs.ClientJSServer');

var $series = BugFlow.$series;
var $task = BugFlow.$task;


//-------------------------------------------------------------------------------
// Declare Class
Expand All @@ -47,7 +51,7 @@ var SocketManager = bugpack.require('airbugserver.SocketManager');
var ExpressApp = Class.extend(Obj, {

_constructor: function(config){
console.log("config:", config);

this._super();

//-------------------------------------------------------------------------------
Expand Down Expand Up @@ -96,11 +100,19 @@ var ExpressApp = Class.extend(Obj, {
//-------------------------------------------------------------------------------

/*
* @param {} callback
* @return {ExpressApp}
**/
initialize: function(){
initialize: function(callback){

var _this = this;
var callback = callback || function(){};

/*
* @type {}
**/
var app = this.app;

/*
* @type {string}
**/
Expand All @@ -116,17 +128,32 @@ var ExpressApp = Class.extend(Obj, {
**/
var cookieParser = this.cookieParser = express.cookieParser(secret);

// Configure App
//-------------------------------------------------------------------------------
this.configure(app);

// Routes
//-------------------------------------------------------------------------------
this.enableRoutes(app);

// Graceful Shut Down
//-------------------------------------------------------------------------------
this.enableGracefulShutdown(app);
$series([
$task(function(flow){

// Configure App
//-------------------------------------------------------------------------------
_this.configure(app, function(error){
flow.complete(error);
});
}),
$task(function(flow){

// Routes
//-------------------------------------------------------------------------------
_this.enableRoutes(app, function(error){
flow.complete(error);
});
}),
$task(function(flow){

// Graceful Shut Down
//-------------------------------------------------------------------------------
_this.enableGracefulShutdown(app, function(error){
flow.complete(error);
});
})
]).execute(callback);

return this;

Expand Down Expand Up @@ -175,13 +202,13 @@ var ExpressApp = Class.extend(Obj, {
* @private
* @param {express.app} app
**/
configure: function(app){
configure: function(app, callback){
var callback = callback || function(){};
var config = this.config;
var cookieParser = this.cookieParser;
var secret = this.secret;
var sessionKey = this.sessionKey;

console.log("config:", config);
app.configure(function(){
app.engine('mustache', mu2Express.engine);
app.set('view engine', 'mustache');
Expand All @@ -204,22 +231,30 @@ var ExpressApp = Class.extend(Obj, {
app.configure('development', function(){
app.use(express.errorHandler());
});

callback();
},


/*
* @private
* @param {express.app} app
**/
enableRoutes: function(app){
enableRoutes: function(app, callback){
var callback = callback || function(){};

this.expressRoutes.enableAll(app);

callback();
},

/*
* @private
* @param {express.app} app
**/
enableGracefulShutdown: function(app){
enableGracefulShutdown: function(app, callback){
var callback = callback || function(){};

process.on('SIGTERM', function () {
console.log("Server Closing");
app.close();
Expand All @@ -229,6 +264,8 @@ var ExpressApp = Class.extend(Obj, {
console.log("Server Closed");
db.connection.close();
});

callback();
}

//-------------------------------------------------------------------------------
Expand Down
7 changes: 6 additions & 1 deletion projects/airbugserver/js/src/Express/ExpressServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ var ExpressServer = Class.extend(Obj, {
**/
this.expressApp = expressApp;

/*
* @type {}
**/
this.httpServer = null;

},
Expand All @@ -56,8 +59,9 @@ var ExpressServer = Class.extend(Obj, {

/*
**/
start: function() {
start: function(callback) {

var callback = callback || function(){};
var app = this.expressApp.getApp();

// Create Server
Expand All @@ -68,6 +72,7 @@ var ExpressServer = Class.extend(Obj, {
console.log("Express server listening on port " + app.get('port'));
});

callback();
return this;

},
Expand Down
11 changes: 6 additions & 5 deletions projects/airbugserver/js/src/Sockets/SocketManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ var SocketManager = Class.extend(Obj, {
/*
* @type {ExpressServer}
**/
this.expressServer = null;
this.expressServer = null;

/*
* @type {}
Expand All @@ -60,11 +60,12 @@ var SocketManager = Class.extend(Obj, {

},

initialize: function(){
initialize: function(callback){
var callback = callback || function(){};
var server = this.expressServer.getHttpServer();
this.socketIoManager = io.listen(server);
this.socketsMap = new SocketsMap();

callback();
return this;
},

Expand All @@ -75,7 +76,7 @@ var SocketManager = Class.extend(Obj, {
var sessionToSocketsMap = this.socketsMap;
var alphaSocketManager = socketIoManager.of('/alpha');

alphaSocketManager.set('authorization', function(data, callback){
alphaSocketManager.manager.set('authorization', function(data, callback){
cookieParser(data, {}, function(error) {
if(!error){
sessionStore.get(data.signedCookies[sessionKey], function(error, session){
Expand All @@ -92,7 +93,7 @@ var SocketManager = Class.extend(Obj, {
});
});

alphaSocketManager.sockets.on('connection', function(socket){
alphaSocketManager.on('connection', function(socket){
console.log("Connection established")
console.log("session:", socket.handshake.session);
var session = socket.handshake.session;
Expand Down

0 comments on commit 383f229

Please sign in to comment.