Configuration file.

This commit is contained in:
Julien Fontanet 2013-09-03 13:28:34 +02:00
parent e3519f571e
commit 34a77d4a11
5 changed files with 127 additions and 7 deletions

1
config/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/local.yaml

28
config/local.yaml.dist Normal file
View File

@ -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: ''

View File

@ -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",

View File

@ -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();

View File

@ -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);
};
//////////////////////////////////////////////////////////////////////