Permalink
Browse files

break out database config into separate files

 - shared 'database.js' for tests and server
 - minimizing use of `throw` in anticipation of bigger refactor

Aside: I'd like to move to more node.js idiomatic callback(err,thing)
wherever possible in Cube, probably as a major semver release.
  • Loading branch information...
1 parent f84c9ea commit 29fd685c5e9c7495068cabb5f9524f03d3788f94 @RandomEtc RandomEtc committed Apr 1, 2013
Showing with 65 additions and 45 deletions.
  1. +35 −0 lib/cube/database.js
  2. +12 −27 lib/cube/server.js
  3. +6 −8 test/collector-test.js
  4. +6 −10 test/helpers.js
  5. +6 −0 test/test-config.json
View
@@ -0,0 +1,35 @@
+var mongodb = require("mongodb");
+
+var database = module.exports = {};
+
+// open MongoDB driver given Cube options:
+// {
+// "mongo-host": "",
+// "mongo-port": "",
+// "mongo-database": "",
+// "mongo-username": "",
+// "mongo-password": "",
+// }
+database.open = function(options, callback) {
+ var server_options = { auto_reconnect: true },
+ mongo = new mongodb.Server(options["mongo-host"], options["mongo-port"], server_options),
+ db_options = { safe: false },
+ db = new mongodb.Db(options["mongo-database"], mongo, db_options);
+ db.open(function(error) {
+ if (error) {
+ return callback(error);
+ }
+ if ("mongo-username" in options) {
+ db.authenticate(options["mongo-username"], options["mongo-password"], function(error, success) {
+ if (error) {
+ return callback(error);
+ } else if (!success) {
+ return callback(new Error("authentication failed"));
+ }
+ return callback(null, db);
+ });
+ } else {
+ return callback(null, db);
+ }
+ });
+};
View
@@ -5,13 +5,7 @@ var util = require("util"),
websocket = require("websocket"),
websprocket = require("websocket-server"),
static = require("node-static"),
- mongodb = require("mongodb");
-
-// Don't crash on errors.
-process.on("uncaughtException", function(error) {
- util.log("uncaught exception: " + error);
- util.log(error.stack);
-});
+ database = require('./database');
// And then this happened:
websprocket.Connection = require("../../node_modules/websocket-server/lib/ws/connection");
@@ -29,19 +23,20 @@ var wsOptions = {
closeTimeout: 5000
};
-// MongoDB driver configuration.
-var server_options = {auto_reconnect: true},
- db_options = {safe: false};
-
module.exports = function(options) {
+
+ // Don't crash on errors.
+ process.on("uncaughtException", function(error) {
+ util.log("uncaught exception: " + error);
+ util.log(error.stack);
+ });
+
var server = {},
primary = http.createServer(),
secondary = websprocket.createServer(),
file = new static.Server("static"),
meta,
endpoints = {ws: [], http: []},
- mongo = new mongodb.Server(options["mongo-host"], options["mongo-port"], server_options),
- db = new mongodb.Db(options["mongo-database"], mongo, db_options),
id = 0;
secondary.server = primary;
@@ -73,9 +68,9 @@ module.exports = function(options) {
for (var i = -1, n = endpoints.ws.length, e; ++i < n;) {
if ((e = endpoints.ws[i]).match(request.url)) {
- function callback(response) {
+ var callback = function(response) {
connection.sendUTF(JSON.stringify(response));
- }
+ };
callback.id = ++id;
@@ -147,18 +142,8 @@ module.exports = function(options) {
server.start = function() {
// Connect to mongodb.
util.log("starting mongodb client");
- db.open(function(error) {
+ database.open(options, function (error, db) {
if (error) throw error;
- if (options["mongo-username"] == null) return ready();
- db.authenticate(options["mongo-username"], options["mongo-password"], function(error, success) {
- if (error) throw error;
- if (!success) throw new Error("authentication failed");
- ready();
- });
- });
-
- // Start the server!
- function ready() {
server.register(db, endpoints);
meta = require("./event").putter(db);
util.log("starting http server on port " + options["http-port"]);
@@ -171,7 +156,7 @@ module.exports = function(options) {
});
udp.bind(options["udp-port"]);
}
- }
+ });
};
return server;
View
@@ -5,12 +5,10 @@ var vows = require("vows"),
var suite = vows.describe("collector");
-var port = ++test.port, server = cube.server({
- "mongo-host": "localhost",
- "mongo-port": 27017,
- "mongo-database": "cube_test",
- "http-port": port
-});
+var server = cube.server(test.config),
+ port = test.config["http-port"];
+
+console.log('collector port %s', port);
server.register = cube.collector.register;
@@ -30,7 +28,7 @@ suite.addBatch(test.batch({
"POST /event/put with a JSON object": {
topic: test.request({method: "POST", port: port, path: "/1.0/event/put"}, JSON.stringify({
type: "test",
- time: new Date,
+ time: new Date(),
data: {
foo: "bar"
}
@@ -46,7 +44,7 @@ suite.addBatch(test.batch({
"POST /event/put with a JSON array": {
topic: test.request({method: "POST", port: port, path: "/1.0/event/put"}, JSON.stringify([{
type: "test",
- time: new Date,
+ time: new Date(),
data: {
foo: "bar"
}
View
@@ -1,17 +1,15 @@
-var mongodb = require("mongodb"),
+var database = require("../lib/cube/database"),
util = require("util"),
http = require("http");
-exports.port = 1083;
+var config = exports.config = require('./test-config');
exports.batch = function(batch) {
return {
"": {
topic: function() {
- var client = new mongodb.Server("localhost", 27017),
- db = new mongodb.Db("cube_test", client, { safe: false }),
- cb = this.callback;
- db.open(function(error) {
+ var cb = this.callback;
+ database.open(config, function(error, db) {
if (error) {
return cb(error);
}
@@ -20,16 +18,14 @@ exports.batch = function(batch) {
db.dropCollection("test_metrics", collectionReady);
function collectionReady() {
if (!--collectionsRemaining) {
- cb(null, {client: client, db: db});
+ cb(null, {db: db});
}
}
});
},
"": batch,
teardown: function(test) {
- if (test.client.isConnected()) {
- test.client.close();
- }
+ test.db.close();
}
}
};
View
@@ -0,0 +1,6 @@
+{
+ "mongo-host": "localhost",
+ "mongo-port": 27017,
+ "mongo-database": "cube_test",
+ "http-port": 1083
+}

0 comments on commit 29fd685

Please sign in to comment.