Permalink
Browse files

Move service bus to a module

  • Loading branch information...
robjoh committed Aug 17, 2014
1 parent 6a41227 commit 1ebd6fbf376cac48740692c0836c8e5e10b3eecb
Showing with 68 additions and 41 deletions.
  1. +9 −41 server.js
  2. +59 −0 service-bus.js
View
@@ -2,53 +2,21 @@
var namespace = 'azure-service-bus-nodejs',
accessKey = '[Access key for this namespace]',
azure = require('azure'),
serviceBus = require('./service-bus.js'),
http = require('http');
var client = azure.createServiceBusService(namespace, accessKey);
var client = serviceBus.createClient(namespace, accessKey);
var server = http.createServer(function(httpReq, httpResp) {
if (httpReq.url === '/') {
getTopics(writeResult);
client.getTopics(writeResult);
} else {
getSubscriptions(httpReq.url.slice(1), writeResult);
}
function getTopics(callback) {
client.listTopics(function(error, result, response) {
if (error) {
callback(error)
return;
}
var topics = result.map(function(topic) {
return {
name: topic.TopicName,
totalSubscriptions: topic.SubscriptionCount,
totalSize: topic.SizeInBytes
};
});
callback(null, topics);
});
}
function getSubscriptions(topic, callback) {
client.listSubscriptions(topic, function(error, result, response) {
if (error) {
callback(error);
return;
}
var subscriptions = result.map(function(subscription) {
return {
name: subscription.SubscriptionName,
totalMessages: subscription.MessageCount
};
});
callback(null, subscriptions);
});
var parts = httpReq.url.split('/');
if (parts.length === 2) {
client.getSubscriptions(parts[1], writeResult);
} else {
client.getSubscription(parts[1], parts[2], writeResult);
}
}
function writeResult(error, result) {
View
@@ -0,0 +1,59 @@
"use strict"
var azure = require('azure');
function mapTopic(topic) {
return {
name: topic.TopicName,
totalSubscriptions: topic.SubscriptionCount,
totalSize: topic.SizeInBytes
};
}
function mapSubscription(subscription) {
return {
name: subscription.SubscriptionName,
totalMessages: subscription.MessageCount
};
}
function createClient(namespace, accessKey) {
var client = azure.createServiceBusService(namespace, accessKey);
return {
getTopics: function (callback) {
client.listTopics(function(error, result, response) {
if (error) {
callback(error)
return;
}
callback(null, result.map(mapTopic));
});
},
getSubscriptions: function (topic, callback) {
client.listSubscriptions(topic, function(error, result, response) {
if (error) {
callback(error);
return;
}
callback(null, result.map(mapSubscription));
});
},
getSubscription: function (topic, subscription, callback) {
client.getSubscription(topic, subscription, function(error, result, response) {
if (error) {
callback(error);
return;
}
callback(null, mapSubscription(result));
});
}
};
}
module.exports.createClient = createClient;

0 comments on commit 1ebd6fb

Please sign in to comment.