From 34a77d4a11f369b5049685fcb8f8a07d8b41673a Mon Sep 17 00:00:00 2001 From: Julien Fontanet Date: Tue, 3 Sep 2013 13:28:34 +0200 Subject: [PATCH] Configuration file. --- config/.gitignore | 1 + config/local.yaml.dist | 28 ++++++++++++ package.json | 1 + src/main.js | 100 ++++++++++++++++++++++++++++++++++++++--- src/xo.js | 4 +- 5 files changed, 127 insertions(+), 7 deletions(-) create mode 100644 config/.gitignore create mode 100644 config/local.yaml.dist diff --git a/config/.gitignore b/config/.gitignore new file mode 100644 index 0000000..1ef5f53 --- /dev/null +++ b/config/.gitignore @@ -0,0 +1 @@ +/local.yaml diff --git a/config/local.yaml.dist b/config/local.yaml.dist new file mode 100644 index 0000000..0d8b542 --- /dev/null +++ b/config/local.yaml.dist @@ -0,0 +1,28 @@ +# Configuration of XO-Server's web server. +http: + # Port on which the server is listening on. + # + # Default: 80 + #port: 8080 + + # Address on which the server is listening on. + # + # Sets it to '*' to listen on all addresses. + # + # Default: localhost + #host: '*' + +# For now, XO-Server does not have any persistence, therefore +# users should be recreated each time. +# +# So, temporarily, users can be created here. +users: + - email: 'admin@admin.com' + password: 'admin' + permission: 'admin' + +# Same thing for servers. +servers: + - host: '' + username: '' + password: '' diff --git a/package.json b/package.json index fb957b8..68c8e12 100644 --- a/package.json +++ b/package.json @@ -16,6 +16,7 @@ "connect": ">=2.8.4", "extendable": ">=0.0.3", "hashy": ">=0.1.0", + "js-yaml": ">=2.1.0", "q": ">=0.9.6", "sync": ">=0.2.2", "underscore": ">=1.4.4", diff --git a/src/main.js b/src/main.js index 8ab67c3..c705fe6 100644 --- a/src/main.js +++ b/src/main.js @@ -12,10 +12,7 @@ var xo = require('./xo')(); var Api = require('./api'); var api = new Api(xo); -// @todo Port should be configurable. -var http_serv = require('http').createServer().listen(8080).on('listening', function () { - console.log('XO-Server Web server is listening on port '+ 8080 +'.'); -}); +var http_serv; ////////////////////////////////////////////////////////////////////// @@ -266,4 +263,97 @@ xo.on('started', function () { ////////////////////////////////////////////////////////////////////// -xo.start(); +var cfg = { + 'data': {}, + + 'get': function (path) { + /* jshint noempty: false */ + + if (!_.isArray(path)) + { + path = Array.prototype.slice.call(arguments); + } + + var current = this.data; + for ( + var i = 0, n = path.length; + (i < n) && (undefined !== (current = current[path[i]])); + ++i + ) + {} + + if (i < n) + { + return undefined; + } + + return current; + }, + + 'merge': function (data) { + var helper = function (target, source) { + if (null === source) // Special case. + { + return target; + } + + if (!_.isObject(target) || !_.isObject(source)) + { + return source; + } + + if (_.isArray(target) && _.isArray(source)) + { + target.push.apply(target, source); + return target; + } + + for (var prop in source) + { + target[prop] = helper(target[prop], source[prop]); + } + return target; + }; + + helper(this.data, data); + return this; + }, +}; + +// Defaults values. +cfg.merge({ + 'http': { + 'port': 80, + 'host': 'localhost', + }, + 'users': [], + 'servers': [], +}); + +Q.ninvoke(require('fs'), 'readFile', __dirname +'/../config/local.yaml', {'encoding': 'utf8'}).then(function (data) { + data = require('js-yaml').safeLoad(data); + cfg.merge(data); +}).fail(function (e) { + console.error('[ERROR] Reading config file: '+ e); +}).then(function () { + var users = xo.users; + cfg.get('users').forEach(function (user) { + if (user.password) + { + users.create(user.email, user.password, user.permission).done(); + } + else + { + users.add(user).done(); + } + }); + + xo.servers.add(cfg.get('servers')).done(); + + var port = cfg.get('http', 'port'); + http_serv = require('http').createServer().listen(port).on('listening', function () { + console.log('XO-Server Web server is listening on port '+ port +'.'); + }); + + xo.start(cfg); +}).done(); diff --git a/src/xo.js b/src/xo.js index 17f8014..f983791 100644 --- a/src/xo.js +++ b/src/xo.js @@ -590,7 +590,7 @@ function Xo() } require('util').inherits(Xo, require('events').EventEmitter); -Xo.prototype.start = function () { +Xo.prototype.start = function (options) { var xo = this; @@ -598,7 +598,7 @@ Xo.prototype.start = function () { //-------------------------------------- - xo.emit('started'); + xo.emit('started', options); }; //////////////////////////////////////////////////////////////////////