Skip to content

Commit

Permalink
Extract server setup into methods by component
Browse files Browse the repository at this point in the history
  • Loading branch information
jden committed Dec 14, 2015
1 parent ead0915 commit 9d9c904
Showing 1 changed file with 41 additions and 35 deletions.
76 changes: 41 additions & 35 deletions server/server.js
Expand Up @@ -73,52 +73,29 @@ Server.prototype.terminate = function (done) {
var VERSION_CLIENT_STOREDATA = '0.13.1'

Server.prototype._setup = function (httpServer, configuration) {
var engine = DefaultEngineIO
var engineConf

this.subscriber = Core.Persistence.pubsub()

this.subscriber.on('message', this._handlePubSubMessage.bind(this))

var oldPublish = Core.Persistence.publish

// Log all outgoing to redis server
Core.Persistence.publish = function (channel, data, callback) {
logging.info('#redis.message.outgoing', channel, data)
oldPublish(channel, data, callback)
}

configuration = configuration || {}

if (configuration.engineio) {
engine = configuration.engineio.module
engineConf = configuration.engineio.conf

this.engineioPath = configuration.engineio.conf ? configuration.engineio.conf.path : 'default'
}

this._setupSentry(configuration)

this.socketServer = engine.attach(httpServer, engineConf)
this.socketServer.on('connection', this._onSocketConnection.bind(this))
this._setupEngineio(httpServer, configuration.engineio)
this._setupDistributor()

logging.debug('#server - start ' + new Date().toString())
this.emit('ready')
}

Server.prototype._setupSentry = function (configuration) {
var sentryOptions = {
host: hostname,
port: configuration.port
}
Server.prototype._setupEngineio = function (httpServer, engineioConfig) {
var engine = DefaultEngineIO
var engineConf

if (configuration.sentry) {
_.extend(sentryOptions, configuration.sentry)
}
if (engineioConfig) {
engine = engineioConfig.module
engineConf = engineioConfig.conf

Stamper.setup(this.sentry.name)
this.engineioPath = engineioConfig.conf ? engineioConfig.conf.path : 'default'
}

this.sentry.start(sentryOptions)
this.socketServer = engine.attach(httpServer, engineConf)
this.socketServer.on('connection', this._onSocketConnection.bind(this))
}

Server.prototype._onSocketConnection = function (socket) {
Expand Down Expand Up @@ -158,6 +135,35 @@ Server.prototype._onSocketConnection = function (socket) {
})
}

Server.prototype._setupDistributor = function () {
this.subscriber = Core.Persistence.pubsub()

this.subscriber.on('message', this._handlePubSubMessage.bind(this))

var oldPublish = Core.Persistence.publish

// Log all outgoing to redis server
Core.Persistence.publish = function (channel, data, callback) {
logging.info('#redis.message.outgoing', channel, data)
oldPublish(channel, data, callback)
}
}

Server.prototype._setupSentry = function (configuration) {
var sentryOptions = {
host: hostname,
port: configuration.port
}

if (configuration.sentry) {
_.extend(sentryOptions, configuration.sentry)
}

Stamper.setup(this.sentry.name)

this.sentry.start(sentryOptions)
}

// Process a message from persistence (i.e. subscriber)
Server.prototype._handlePubSubMessage = function (to, data) {
if (this.resources[to]) {
Expand Down

0 comments on commit 9d9c904

Please sign in to comment.