Permalink
Browse files

refactor config.js

  • Loading branch information...
1 parent 0f5aca8 commit bf2b19679814d8bf53a357083c06825a5c4123c2 @chjj committed Jun 24, 2012
Showing with 79 additions and 110 deletions.
  1. +75 −106 lib/config.js
  2. +4 −4 lib/tty.js
View
@@ -8,40 +8,9 @@ var path = require('path')
, logger = require('./logger');
/**
- * Default Config
+ * Options
*/
-var schema = {
- users: {},
- https: {
- key: null,
- cert: null
- },
- port: 8080,
- // hostname: '0.0.0.0',
- // shell: 'sh',
- // shellArgs: ['arg1', 'arg2'],
- // static: './static',
- // limitGlobal: 10000,
- // limitPerUser: 1000,
- // syncSession: true,
- // sessionTimeout: 10 * 60 * 1000,
- // log: true,
- // cwd: '.',
- term: {
- // termName: 'xterm',
- // geometry: [80, 30],
- // visualBell: false,
- // popOnBell: false,
- // cursorBlink: true,
- // scrollback: 1000,
- // screenKeys: false,
- // colors: [],
- // programFeatures: false,
- // debug: false
- }
-};
-
var options;
/**
@@ -59,7 +28,7 @@ function readConfig(file) {
dir = path.dirname(file);
json = options.config;
} else {
- dir = path.join(home, '.tty.js');
+ dir = process.env.TTYJS_PATH || path.join(home, '.tty.js');
json = path.join(dir, 'config.json');
}
@@ -69,19 +38,12 @@ function readConfig(file) {
dir = home;
}
- // read conf
conf = JSON.parse(fs.readFileSync(json, 'utf8'));
-
- // ensure schema
- ensure(conf, schema);
} else {
if (!exists(dir)) {
fs.mkdirSync(dir, 0700);
}
- // ensure schema
- ensure(conf, schema);
-
fs.writeFileSync(json, JSON.stringify(conf, null, 2));
fs.chmodSync(json, 0600);
}
@@ -109,84 +71,88 @@ function checkConfig(conf) {
merge(conf, readConfig(file));
}
- if (conf.__check) return conf;
-
// flag
+ if (conf.__check) return conf;
conf.__check = true;
- // maybe remove?
- ensure(conf, schema);
+ // merge options
+ merge(conf, options.conf);
- if (!conf.dir) {
- conf.dir = process.env.HOME + '/.tty.js';
- //conf.dir = process.cwd();
- }
+ // directory and config file
+ conf.dir = conf.dir || '';
+ conf.json = conf.json || '';
- if (!conf.json) {
- conf.json = process.env.HOME + '/.tty.js/config.json';
- //conf.json = process.cwd() + '/config.json';
+ // users
+ conf.users = conf.users || {};
+ if (conf.auth && conf.auth.username && !conf.auth.disabled) {
+ conf.users[conf.auth.username] = conf.auth.password;
}
- var dir = conf.dir
- , json = conf.json;
-
- // merge options
- merge(conf, options);
+ // https
+ conf.https = conf.https || conf.ssl || conf.tls || {};
+ conf.https = {
+ key: tryRead(conf.dir, conf.https.key || 'server.key') || conf.https.key,
+ cert: tryRead(conf.dir, conf.https.cert || 'server.crt') || conf.https.cert
+ };
- // check legacy features
- checkLegacy(conf);
+ // port
+ conf.port = conf.port || 8080;
- // key and cert
- conf.https = conf.https || {
- key: tryRead(dir, 'server.key'),
- cert: tryRead(dir, 'server.crt')
- };
+ // hostname
+ conf.hostname; // '0.0.0.0'
// shell, process name
if (conf.shell && ~conf.shell.indexOf('/')) {
- conf.shell = path.resolve(dir, conf.shell);
+ conf.shell = path.resolve(conf.dir, conf.shell);
}
-
- // static directory
- conf.static = conf.static || tryResolve(dir, 'static');
-
- // Path to shell, or the process to execute in the terminal.
conf.shell = conf.shell || process.env.SHELL || 'sh';
- // Arguments to shell, if they exist
+ // arguments to shell, if they exist
conf.shellArgs = conf.shellArgs || [];
- // $TERM
- conf.term.termName = conf.termName || conf.term.termName;
- if (!conf.term.termName) {
- // tput -Txterm-256color longname
- conf.term.termName =
- exists('/usr/share/terminfo/x/xterm+256color')
- || exists('/usr/share/terminfo/x/xterm-256color')
- ? 'xterm-256color'
- : 'xterm';
- }
- conf.termName = conf.term.termName;
+ // static directory
+ conf.static = tryResolve(conf.dir, conf.static || 'static');
// limits
conf.limitPerUser = conf.limitPerUser || Infinity;
conf.limitGlobal = conf.limitGlobal || Infinity;
+ // sync session
+ conf.syncSession; // false
+
// session timeout
if (typeof conf.sessionTimeout !== 'number') {
conf.sessionTimeout = 10 * 60 * 1000;
}
- // users
- if (conf.users && !Object.keys(conf.users).length) {
- delete conf.users;
- }
+ // log
+ conf.log; // true
// cwd
if (conf.cwd) {
- conf.cwd = path.resolve(dir, conf.cwd);
+ conf.cwd = path.resolve(conf.dir, conf.cwd);
}
+ // term
+ conf.term = conf.term || {};
+
+ conf.termName = conf.termName || conf.term.termName || terminfo();
+ conf.term.termName = conf.termName;
+
+ conf.term.termName; // 'xterm'
+ conf.term.geometry; // [80, 30]
+ conf.term.visualBell; // false
+ conf.term.popOnBell; // false
+ conf.term.cursorBlink; // true
+ conf.term.scrollback; // 1000
+ conf.term.screenKeys; // false
+ conf.term.colors; // []
+ conf.term.programFeatures; // false
+ conf.term.debug; // false
+
+ // check legacy features
+ checkLegacy(conf);
+
return conf;
}
@@ -198,17 +164,7 @@ function checkLegacy(conf) {
var out = [];
if (conf.auth) {
- if (conf.auth && conf.auth.username && !conf.auth.disabled) {
- conf.users[conf.auth.username] = conf.auth.password;
- }
- logger('error', '`auth` is deprecated, please use `users` instead.');
- }
-
- if (conf.https && conf.https.key) {
- conf.https = {
- key: tryRead(conf.dir, conf.https.key),
- cert: tryRead(conf.dir, conf.https.cert)
- };
+ logger.error('`auth` is deprecated, please use `users` instead.');
}
if (conf.userScript) {
@@ -229,10 +185,6 @@ function checkLegacy(conf) {
+ '`user.css` in `~/.tty.js/static/user.css` instead.');
}
- if (conf.static) {
- conf.static = tryResolve(conf.dir, conf.static);
- }
-
if (conf.hooks) {
out.push(''
+ '`hooks` is deprecated, please programmatically '
@@ -241,14 +193,31 @@ function checkLegacy(conf) {
if (out.length) {
out.forEach(function(out) {
- logger('error', out);
+ logger.error(out);
});
- logger('error', 'Exiting.');
+ logger.error('Exiting.');
process.exit(1);
}
}
/**
+ * Terminfo
+ */
+
+function terminfo() {
+ // tput -Txterm-256color longname
+ var terminfo = exists('/usr/share/terminfo/x/xterm+256color')
+ || exists('/usr/share/terminfo/x/xterm-256color');
+
+ // Default $TERM
+ var TERM = terminfo
+ ? 'xterm-256color'
+ : 'xterm';
+
+ return TERM;
+}
+
+/**
* Daemonize
*/
@@ -269,6 +238,7 @@ function daemonize() {
process.exit(code || 0);
});
+ // kill current stack
process.once('uncaughtException', function() {});
throw 'stop';
}
@@ -302,7 +272,7 @@ function help() {
function parseArg() {
var argv = process.argv.slice()
- , opt = {}
+ , opt = { conf: {} }
, arg;
function getarg() {
@@ -338,7 +308,7 @@ function parseArg() {
switch (arg) {
case '-p':
case '--port':
- opt.port = +argv.shift();
+ opt.conf.port = +argv.shift();
break;
case '-c':
case '--config':
@@ -458,7 +428,6 @@ function clone(obj) {
exports.readConfig = readConfig;
exports.checkConfig = checkConfig;
-exports.schema = clone(schema);
exports.xresources = readResources();
exports.helpers = {
View
@@ -203,7 +203,7 @@ Server.prototype._basicAuth = function() {
var self = this;
var conf = this.conf;
- if (!conf.users) {
+ if (!Object.keys(conf.users).length) {
return function(req, res, next) {
next();
};
@@ -322,8 +322,8 @@ Session.prototype.uid = function() {
if (this.server.conf.syncSession) {
var req = this.req;
return req.address.address
- + ':' + req.address.port
- + ':' + req.headers['user-agent'];
+ + '|' + req.address.port
+ + '|' + req.headers['user-agent'];
}
return Session.uid++ + '';
};
@@ -355,7 +355,7 @@ Session.prototype.warning = function() {
Session.prototype._log = function(level, args) {
if (typeof args[0] !== 'string') args.unshift('');
- var id = this.id.split(':')[0];
+ var id = this.id.split('|')[0];
args[0] = '\x1b[1m' + id + '\x1b[m ' + args[0];
return this.server._log(level, args);
};

0 comments on commit bf2b196

Please sign in to comment.