mirror of
https://github.com/coderaiser/cloudcmd.git
synced 2026-01-23 02:35:49 +00:00
feature(validate) add
This commit is contained in:
parent
74a1da51ff
commit
b9eab52f8b
4 changed files with 82 additions and 93 deletions
|
|
@ -10,7 +10,6 @@
|
|||
|
||||
exit = require(DIR_SERVER + 'exit'),
|
||||
config = require(DIR_SERVER + 'config'),
|
||||
createPass = require(DIR_SERVER + 'password'),
|
||||
|
||||
argv = process.argv,
|
||||
|
||||
|
|
@ -70,22 +69,23 @@
|
|||
checkUpdate();
|
||||
|
||||
port(args.port);
|
||||
password(args.password);
|
||||
|
||||
config('auth', args.auth);
|
||||
config('online', args.online);
|
||||
config('minify', args.minify);
|
||||
config('username', args.username);
|
||||
config('progressOfCopying', args['progress-of-copying']);
|
||||
root(args.root);
|
||||
editor(args.editor);
|
||||
|
||||
readConfig(args.config);
|
||||
|
||||
if (args.save)
|
||||
config.save(start);
|
||||
else
|
||||
start();
|
||||
start({
|
||||
root: args.root,
|
||||
editor: args.editor,
|
||||
password: args.password,
|
||||
});
|
||||
}
|
||||
|
||||
function version() {
|
||||
|
|
@ -99,17 +99,6 @@
|
|||
require(SERVER)(config);
|
||||
}
|
||||
|
||||
function password(pass) {
|
||||
var algo, hash;
|
||||
|
||||
if (pass) {
|
||||
algo = config('algo');
|
||||
hash = createPass(algo, pass);
|
||||
|
||||
config('password', hash);
|
||||
}
|
||||
}
|
||||
|
||||
function port(arg) {
|
||||
var number = parseInt(arg, 10);
|
||||
|
||||
|
|
@ -154,29 +143,6 @@
|
|||
console.log('\nGeneral help using Cloud Commander: <%s>', url);
|
||||
}
|
||||
|
||||
function root(dir) {
|
||||
var fs;
|
||||
|
||||
config('root', dir);
|
||||
|
||||
if (dir !== '/') {
|
||||
fs = require('fs');
|
||||
fs.stat(dir, function(error) {
|
||||
if (error)
|
||||
exit('cloudcmd --root: %s', error.message);
|
||||
else
|
||||
console.log('root:', dir);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function editor(name) {
|
||||
var reg = /^(dword|edward)$/;
|
||||
|
||||
if (!reg.test(name))
|
||||
exit('cloudcmd --editor: could be "dword" or "edward" only');
|
||||
}
|
||||
|
||||
function repl() {
|
||||
console.log('REPL mode enabled (telnet localhost 1337)');
|
||||
require(DIR_LIB + '/server/repl');
|
||||
|
|
|
|||
|
|
@ -12,6 +12,8 @@
|
|||
config = require(DIR_SERVER + 'config'),
|
||||
rest = require(DIR_SERVER + 'rest'),
|
||||
route = require(DIR_SERVER + 'route'),
|
||||
validate = require(DIR_SERVER + 'validate'),
|
||||
createPass = require(DIR_SERVER + 'password'),
|
||||
|
||||
join = require('join-io'),
|
||||
ponse = require('ponse'),
|
||||
|
|
@ -35,34 +37,36 @@
|
|||
},
|
||||
|
||||
module.exports = function(params) {
|
||||
var keys,
|
||||
p = params || {},
|
||||
prefix = p.prefix || '';
|
||||
var p = params || {},
|
||||
options = p.config || {},
|
||||
prefix = p.prefix || '',
|
||||
keys = Object.keys(options);
|
||||
|
||||
if (params) {
|
||||
keys = Object.keys(params);
|
||||
keys.forEach(function(name) {
|
||||
var value = options[name];
|
||||
|
||||
keys.forEach(function(name) {
|
||||
config(name, params[name]);
|
||||
});
|
||||
}
|
||||
switch(name) {
|
||||
case 'root':
|
||||
validate.root(value);
|
||||
break;
|
||||
case 'editor':
|
||||
validate.editor(value);
|
||||
break;
|
||||
case 'password':
|
||||
value = createPass(config('algo'), value);
|
||||
break;
|
||||
}
|
||||
|
||||
config(name, value);
|
||||
});
|
||||
|
||||
if (p.socket)
|
||||
module.exports.listen(p.socket);
|
||||
listen(p.socket);
|
||||
|
||||
return cloudcmd(prefix);
|
||||
};
|
||||
|
||||
module.exports.middle = function(params) {
|
||||
var p = params || {},
|
||||
prefix = p.prefix || '/cloudcmd',
|
||||
funcs = cloudcmd(),
|
||||
middle = respond.bind(null, prefix, funcs);
|
||||
|
||||
return middle;
|
||||
};
|
||||
|
||||
module.exports.listen = function(socket) {
|
||||
function listen(socket) {
|
||||
var size = cloudfunc.MAX_SIZE;
|
||||
|
||||
Util.check(arguments, ['socket']);
|
||||
|
|
@ -86,7 +90,7 @@
|
|||
spero.listen(socket, {
|
||||
root: root
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
function cloudcmd(prefix) {
|
||||
var isOption = function(name) {
|
||||
|
|
@ -178,12 +182,4 @@
|
|||
next();
|
||||
};
|
||||
}
|
||||
|
||||
function respond(prefix, funcs, req, res) {
|
||||
funcs = funcs.map(function(func) {
|
||||
return Util.exec.with(func, req, res);
|
||||
});
|
||||
|
||||
Util.exec.series(funcs);
|
||||
}
|
||||
})();
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
|
||||
http = require('http'),
|
||||
|
||||
middleware = require(DIR_LIB + 'cloudcmd'),
|
||||
cloudcmd = require(DIR_LIB + 'cloudcmd'),
|
||||
exit = require(DIR_SERVER + 'exit'),
|
||||
config = require(DIR_SERVER + 'config'),
|
||||
express = require(DIR_SERVER + 'express'),
|
||||
|
|
@ -17,37 +17,33 @@
|
|||
*
|
||||
*/
|
||||
module.exports = function(options) {
|
||||
var server, app, socket,
|
||||
var server,
|
||||
|
||||
port = process.env.PORT || /* c9 */
|
||||
process.env.VCAP_APP_PORT || /* cloudfoundry */
|
||||
config('port'),
|
||||
|
||||
ip = process.env.IP || /* c9 */
|
||||
config('ip') ||
|
||||
'0.0.0.0',
|
||||
|
||||
app = express.getApp({
|
||||
auth : config('auth'),
|
||||
username: config('username'),
|
||||
password: config('password')
|
||||
});
|
||||
|
||||
middle = middleware(options),
|
||||
server = http.createServer(app);
|
||||
|
||||
port = process.env.PORT || /* c9 */
|
||||
process.env.VCAP_APP_PORT || /* cloudfoundry */
|
||||
config('port'),
|
||||
|
||||
ip = process.env.IP || /* c9 */
|
||||
config('ip') ||
|
||||
'0.0.0.0',
|
||||
|
||||
expressApp = express.getApp(middle, {
|
||||
auth : config('auth'),
|
||||
username: config('username'),
|
||||
password: config('password')
|
||||
});
|
||||
|
||||
if (expressApp)
|
||||
app = expressApp;
|
||||
else
|
||||
app = middle;
|
||||
|
||||
server = http.createServer(app);
|
||||
socket = io.listen(server);
|
||||
app.use(cloudcmd({
|
||||
config: options,
|
||||
socket: io.listen(server)
|
||||
}));
|
||||
|
||||
server.on('error', function(error) {
|
||||
exit('cloudcmd --port: %s', error.message);
|
||||
});
|
||||
|
||||
middleware.listen(socket);
|
||||
server.listen(port, ip);
|
||||
|
||||
console.log('url: http://%s:%d', config('ip') || 'localhost', port);
|
||||
|
|
|
|||
31
lib/server/validate.js
Normal file
31
lib/server/validate.js
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
(function() {
|
||||
'use strict';
|
||||
|
||||
var DIR = './',
|
||||
exit = require(DIR + 'exit');
|
||||
|
||||
module.exports.root = root;
|
||||
module.exports.editor = editor;
|
||||
|
||||
function root(dir) {
|
||||
var fs;
|
||||
|
||||
if (dir !== '/') {
|
||||
fs = require('fs');
|
||||
fs.stat(dir, function(error) {
|
||||
if (error)
|
||||
exit('cloudcmd --root: %s', error.message);
|
||||
else
|
||||
console.log('root:', dir);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function editor(name) {
|
||||
var reg = /^(dword|edward)$/;
|
||||
|
||||
if (!reg.test(name))
|
||||
exit('cloudcmd --editor: could be "dword" or "edward" only');
|
||||
}
|
||||
|
||||
})();
|
||||
Loading…
Add table
Add a link
Reference in a new issue