Created
May 22, 2012 04:30
-
-
Save tjstebbing/2766573 to your computer and use it in GitHub Desktop.
services concept for node servers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Service = require('services'); | |
var express = require('express'); | |
var log = require('clog'); | |
var mongodb = require('mongodb'); | |
var amqp = require('amqp'); | |
logService = function(settings, services, cb) { | |
if(settings.debug) { | |
log.configure({'log level' : 5}); | |
} else { | |
log.configure({'log level' : 1}); | |
} | |
services.log = log; | |
cb(true); | |
}; | |
mongoService = function(settings, services, cb) { | |
var mongo = new mongodb.Db(settings.mongo.db, dbServer, {}); | |
mongo.open(function(err, mongo) { | |
if(err) { | |
services.log.error("Could not connect to mongo."); | |
services.log.error(err); | |
cb(false); | |
} else { | |
services.mongo = mongo; | |
cb(true); | |
} | |
}); | |
}; | |
rabbitService = function(settings, services, cb) { | |
var mq = amqp.createConnection({ host: settings.rabbit.ip }); | |
mq.on('ready', function() { | |
services.mq = mq; | |
cb(true); | |
}); | |
//pass on a closed signal and reconnect | |
mq.on('closed', function() { | |
services.emit('mq:closed'); | |
//do stuff | |
}); | |
}; | |
myWebService = function(settings, services, cb) { | |
services.app = express.createServer(); | |
//app logic here! | |
services.on('mq:closed', function() { | |
//perhaps our app cares about mq being closed? | |
}); | |
services.app.listen(settings.port); | |
}; | |
var services = Services(); | |
//services are registered in dependancy order | |
services.addService(logService); | |
services.addService(mongoService); | |
services.addService(rabbitService); | |
services.addService(myWebService); | |
services.start(settings, function(res) { /* handle errors */ }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment