Commits

Daniel Foote committed 0d8686c

Very basic node JS interface. Needs cleanup.

Comments (0)

Files changed (3)

configs/test.json

+{
+	"services": {
+		"parameters": {
+			"foo": "bar",
+			"provider": "paasmaker.service.parameters"
+		}
+	},
+	"application": {
+		"name": "test",
+		"version": 1,
+		"workspace": "Test",
+		"workspace_stub": "test"
+	},
+	"port": 9000
+}

paasmaker-interface.js

+
+var fs = require('fs');
+
+// NOTE: We're using sync operators in this file for
+// simplicity.
+
+// Read environment variables.
+function loadFromEnvironment() {
+	exports.services = JSON.parse(process.env.PM_SERVICES);
+	exports.metadata = JSON.parse(process.env.PM_METADATA);
+	exports.metadata['port'] = parseInt(process.env.PM_PORT, 10);
+}
+
+function loadFromFiles(files) {
+	if(files) {
+		for(var i = 0; i < files.length; i++) {
+			var filename = files[i];
+			if(fs.existsSync(filename)) {
+				var contents = fs.readFileSync(filename);
+				var parsed = JSON.parse(contents);
+
+				if(parsed.services && parsed.application) {
+					exports.services = parsed.services;
+					exports.metadata = parsed.application;
+
+					if(parsed.port) {
+						parsed.application['port'] = parsed.port;
+					}
+
+					// TODO: Validate.
+
+					// Stop processing.
+					return;
+				}
+			}
+		}
+
+		throw new Error("Unable to find a configuration file to load.");
+	} else {
+		throw new Error("No files supplied, and not running on Paasmaker.");
+	}
+}
+
+exports.load = function(files) {
+	exports.services = {};
+	exports.metadata = {};
+
+	exports.isOnPaasmaker = !!process.env.PM_SERVICES && !!process.env.PM_METADATA;
+
+	if(exports.isOnPaasmaker) {
+		loadFromEnvironment();
+	} else {
+		loadFromFiles(files);
+	}
+}
+// Test for the interface.
+
+var assert = require('assert');
+var pminterface = require('./paasmaker-interface.js');
+
+console.log("Testing no environment variables and no overrides.")
+
+// First run - try to call load. It will fail because it's not running on
+// Paasmaker and there are no files to load.
+assert.throws(
+	function() {
+		pminterface.load();
+	},
+	Error,
+	/No files/
+);
+
+// Now try loading from environment variables.
+console.log("Testing loading from environment variables.");
+
+process.env.PM_SERVICES = JSON.stringify({'variables': {'one': 'two'}});
+process.env.PM_METADATA = JSON.stringify({
+	'application': {
+		'name': 'test',
+		'version': 1,
+		'workspace': 'Test',
+		'workspace_stub': 'test'
+	},
+	'node': {'one': 'two'},
+	'workspace': {'three': 'four'}
+});
+process.env.PM_PORT = "42600";
+
+pminterface = require('./paasmaker-interface.js');
+
+pminterface.load();
+
+assert.ok(pminterface.metadata);
+assert.ok(pminterface.services);
+assert.ok(pminterface.services.variables);
+assert.strictEqual(pminterface.metadata.port, 42600);
+
+delete process.env['PM_SERVICES']
+delete process.env['PM_METADATA']
+delete process.env['PM_PORT']
+
+// Now try to load from a configuration file.
+console.log("Testing loading from configuration file.");
+
+pminterface = require('./paasmaker-interface.js');
+
+pminterface.load(['configs/noexist.json', 'configs/test.json']);
+
+assert.strictEqual(pminterface.metadata.port, 9000);
+assert.ok(pminterface.metadata);
+assert.equal(pminterface.metadata.name, "test");
+assert.ok(pminterface.services.parameters);
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.