mirror of
https://github.com/coderaiser/cloudcmd.git
synced 2026-01-23 02:35:49 +00:00
feature(cloudcmd) add middleware
This commit is contained in:
parent
c07101fb4b
commit
d8040d4a19
4 changed files with 196 additions and 209 deletions
|
|
@ -19,7 +19,7 @@
|
|||
port = argvLast - 0;
|
||||
|
||||
if (argvLength === 2)
|
||||
start();
|
||||
start(true);
|
||||
else
|
||||
if (!isPort(argv))
|
||||
help();
|
||||
|
|
@ -27,7 +27,7 @@
|
|||
if (isNaN(port))
|
||||
throw('Error: port should be a number.');
|
||||
else
|
||||
start({
|
||||
start(true, {
|
||||
port: port
|
||||
});
|
||||
|
||||
|
|
@ -37,9 +37,7 @@
|
|||
Util.log('Cloud Commander testing mode');
|
||||
Util.log('argv: ', argv);
|
||||
|
||||
start({
|
||||
server: false
|
||||
});
|
||||
start();
|
||||
break;
|
||||
|
||||
case '-v':
|
||||
|
|
@ -67,10 +65,14 @@
|
|||
console.log('v' + Info.version);
|
||||
}
|
||||
|
||||
function start(config) {
|
||||
var cloudcmd = require('..');
|
||||
function start(isServer, config) {
|
||||
var SERVER = '../lib/server',
|
||||
CLOUDCMD = '..';
|
||||
|
||||
cloudcmd(config);
|
||||
if (isServer)
|
||||
require(SERVER)(config);
|
||||
else
|
||||
require(CLOUDCMD)(config);
|
||||
}
|
||||
|
||||
function isPort(argv) {
|
||||
|
|
@ -108,7 +110,7 @@
|
|||
function repl() {
|
||||
console.log('REPL mode enabled (telnet localhost 1337)');
|
||||
require(DIR_LIB + '/server/repl');
|
||||
start();
|
||||
start(true);
|
||||
}
|
||||
|
||||
})();
|
||||
|
|
|
|||
|
|
@ -2,14 +2,42 @@
|
|||
'use strict';
|
||||
|
||||
var DIR = __dirname + '/',
|
||||
DIR_ROOT = DIR + '../',
|
||||
DIR_SERVER = DIR + 'server/',
|
||||
|
||||
server = require(DIR + 'server'),
|
||||
config = require(DIR_SERVER + 'config');
|
||||
Util = require(DIR + 'util'),
|
||||
|
||||
config = require(DIR_SERVER + 'config'),
|
||||
minify = require(DIR_SERVER + 'minify'),
|
||||
rest = require(DIR_SERVER + 'rest'),
|
||||
route = require(DIR_SERVER + 'route'),
|
||||
join = require(DIR_SERVER + 'join'),
|
||||
ponse = require(DIR_SERVER + 'ponse'),
|
||||
tryRequire = require(DIR_SERVER + 'tryRequire'),
|
||||
|
||||
WIN = process.platform === 'WIN32',
|
||||
terminal = !WIN ? require(DIR_SERVER + 'terminal') : function() {},
|
||||
|
||||
webconsole,
|
||||
|
||||
emptyFunc = function(req, res, next) {
|
||||
Util.exec(next);
|
||||
};
|
||||
|
||||
emptyFunc.middle = function() {
|
||||
return emptyFunc;
|
||||
};
|
||||
|
||||
webconsole = tryRequire('console-io', function(error) {
|
||||
if (error)
|
||||
Util.log(error.message);
|
||||
}) || emptyFunc;
|
||||
|
||||
|
||||
module.exports = function(params) {
|
||||
var keys;
|
||||
var keys,
|
||||
funcs = cloudcmd(),
|
||||
middle = respond.bind(null, funcs);
|
||||
|
||||
if (params) {
|
||||
keys = Object.keys(params);
|
||||
|
|
@ -19,6 +47,59 @@
|
|||
});
|
||||
}
|
||||
|
||||
server();
|
||||
return middle;
|
||||
};
|
||||
|
||||
module.exports.listen = function(socket) {
|
||||
Util.checkArgs(arguments, ['socket']);
|
||||
|
||||
webconsole({
|
||||
socket: socket
|
||||
});
|
||||
|
||||
terminal(socket);
|
||||
};
|
||||
|
||||
function cloudcmd() {
|
||||
var isOption = function(name) {
|
||||
return config(name);
|
||||
},
|
||||
|
||||
isMinify = isOption.bind(null, 'minify'),
|
||||
isOnline = isOption.bind(null, 'online'),
|
||||
isCache = isOption.bind(null, 'cache'),
|
||||
|
||||
ponseStatic = ponse.static(DIR_ROOT, {
|
||||
cache: isCache
|
||||
}),
|
||||
|
||||
funcs = [
|
||||
rest,
|
||||
route,
|
||||
|
||||
join({
|
||||
minify: isMinify
|
||||
}),
|
||||
|
||||
webconsole.middle('/console', isMinify, isOnline),
|
||||
|
||||
minify({
|
||||
dir : DIR,
|
||||
log : true,
|
||||
is : isMinify
|
||||
}),
|
||||
|
||||
ponseStatic
|
||||
];
|
||||
|
||||
return funcs;
|
||||
}
|
||||
|
||||
function respond(funcs, req, res) {
|
||||
funcs = funcs.map(function(func) {
|
||||
return Util.exec.with(func, req, res);
|
||||
});
|
||||
|
||||
Util.exec.series(funcs);
|
||||
}
|
||||
})();
|
||||
|
|
|
|||
139
lib/server.js
139
lib/server.js
|
|
@ -1,161 +1,66 @@
|
|||
(function() {
|
||||
'use strict';
|
||||
|
||||
var DIR = __dirname + '/../',
|
||||
DIR_LIB = DIR + 'lib/',
|
||||
var DIR_LIB = './',
|
||||
DIR_SERVER = DIR_LIB + 'server/',
|
||||
|
||||
WIN = process.platform === 'WIN32',
|
||||
|
||||
http = require('http'),
|
||||
|
||||
Util = require(DIR_LIB + 'util'),
|
||||
CloudFunc = require(DIR_LIB + 'cloudfunc'),
|
||||
middleware = require(DIR_LIB + 'cloudcmd'),
|
||||
|
||||
minify = require(DIR_SERVER + 'minify'),
|
||||
|
||||
terminal = !WIN ? require(DIR_SERVER + 'terminal') : function() {},
|
||||
|
||||
rest = require(DIR_SERVER + 'rest'),
|
||||
route = require(DIR_SERVER + 'route'),
|
||||
|
||||
config = require(DIR_SERVER + 'config'),
|
||||
|
||||
join = require(DIR_SERVER + 'join'),
|
||||
ponse = require(DIR_SERVER + 'ponse'),
|
||||
config = require(DIR_SERVER + 'config'),
|
||||
express = require(DIR_SERVER + 'express'),
|
||||
tryRequire = require(DIR_SERVER + 'tryRequire'),
|
||||
|
||||
io = tryRequire('socket.io'),
|
||||
emptyFunc = function(req, res, next) {
|
||||
Util.exec(next);
|
||||
},
|
||||
tryRequire = require(DIR_SERVER + 'tryRequire'),
|
||||
|
||||
webconsole, expressApp;
|
||||
|
||||
emptyFunc.middle = function() {
|
||||
return emptyFunc;
|
||||
};
|
||||
|
||||
webconsole = tryRequire('console-io', function(error) {
|
||||
if (error)
|
||||
Util.log(error.message);
|
||||
}) || emptyFunc;
|
||||
io = tryRequire('socket.io');
|
||||
|
||||
/**
|
||||
* start server function
|
||||
*
|
||||
*/
|
||||
module.exports = function() {
|
||||
var port, ip,
|
||||
HTTP = 'http://';
|
||||
module.exports = function(options) {
|
||||
var server, app, socket,
|
||||
|
||||
middle = middleware(options),
|
||||
|
||||
port = process.env.PORT || /* c9 */
|
||||
process.env.VCAP_APP_PORT || /* cloudfoundry */
|
||||
config('port'),
|
||||
|
||||
ip = process.env.IP || /* c9 */
|
||||
config('ip') ||
|
||||
'0.0.0.0';
|
||||
config('ip') ||
|
||||
'0.0.0.0',
|
||||
|
||||
if (config('server'))
|
||||
createServer(port, ip, HTTP);
|
||||
};
|
||||
|
||||
function createServer(port, ip, protocol, callback) {
|
||||
var server, app, respondApp,
|
||||
|
||||
isOption = function(name) {
|
||||
return config(name);
|
||||
},
|
||||
|
||||
isMinify = isOption.bind(null, 'minify'),
|
||||
isOnline = isOption.bind(null, 'online'),
|
||||
isCache = isOption.bind(null, 'cache'),
|
||||
|
||||
ponseStatic = ponse.static(DIR, {
|
||||
cache: isCache
|
||||
}),
|
||||
|
||||
funcs = [
|
||||
rest,
|
||||
route,
|
||||
|
||||
join({
|
||||
minify: isMinify
|
||||
}),
|
||||
|
||||
webconsole.middle('/console', isMinify, isOnline),
|
||||
|
||||
minify({
|
||||
dir : DIR,
|
||||
log : true,
|
||||
is : isMinify
|
||||
})
|
||||
];
|
||||
|
||||
expressApp = express.getApp(funcs, {
|
||||
expressApp = express.getApp([middle], {
|
||||
auth : config.auth,
|
||||
username: config.username,
|
||||
password: config.password
|
||||
});
|
||||
|
||||
if (expressApp) {
|
||||
if (expressApp)
|
||||
app = expressApp;
|
||||
app.use(ponseStatic);
|
||||
} else {
|
||||
funcs.push(ponseStatic);
|
||||
|
||||
respondApp = Util.exec.with(respond, funcs);
|
||||
app = respondApp;
|
||||
}
|
||||
else
|
||||
app = middleware;
|
||||
|
||||
server = http.createServer(app);
|
||||
|
||||
server.on('error', function(error) {
|
||||
Util.log(error);
|
||||
Util.exec(callback, error);
|
||||
Util.log(error.message);
|
||||
});
|
||||
|
||||
server.listen(port, ip);
|
||||
|
||||
logServer(port, ip, protocol);
|
||||
addSockets(server);
|
||||
}
|
||||
|
||||
function logServer(port, ip, http) {
|
||||
Util.log('url:', http + ip + ':' + port);
|
||||
}
|
||||
|
||||
function addSockets(server) {
|
||||
var socket, msg,
|
||||
status = 'off';
|
||||
|
||||
if (io && config('socket')) {
|
||||
if (io) {
|
||||
socket = io.listen(server);
|
||||
|
||||
if (socket) {
|
||||
status = 'on';
|
||||
|
||||
webconsole({
|
||||
socket: socket
|
||||
});
|
||||
|
||||
terminal(socket);
|
||||
}
|
||||
middleware.listen(socket);
|
||||
}
|
||||
|
||||
msg = CloudFunc.formatMsg('sockets', '', status);
|
||||
|
||||
Util.log(msg);
|
||||
}
|
||||
logServer(port, ip);
|
||||
};
|
||||
|
||||
function respond(funcs, req, res) {
|
||||
funcs = funcs.map(function(func) {
|
||||
return Util.exec.with(func, req, res);
|
||||
});
|
||||
|
||||
Util.exec.series(funcs);
|
||||
function logServer(port, ip) {
|
||||
Util.log('url: http://' + ip + ':' + port);
|
||||
}
|
||||
|
||||
})();
|
||||
|
|
|
|||
|
|
@ -75,40 +75,39 @@
|
|||
}
|
||||
|
||||
function readFiles(callback) {
|
||||
var filesList, paths = {};
|
||||
var filesList,
|
||||
paths = {},
|
||||
|
||||
lengthTmpl = Object.keys(Template).length,
|
||||
lenthPath = TMPL_PATH.length,
|
||||
isRead = lengthTmpl === lenthPath;
|
||||
|
||||
filesList = TMPL_PATH.map(function(name) {
|
||||
var path = DIR_FS + name + '.html';
|
||||
|
||||
paths[path] = name;
|
||||
|
||||
return path;
|
||||
});
|
||||
Util.checkArgs(arguments, ['callback']);
|
||||
|
||||
files.read(filesList, 'utf8', function(error, files) {
|
||||
var status, msg, names;
|
||||
|
||||
if (error) {
|
||||
Util.log(error);
|
||||
} else {
|
||||
status = 'ok';
|
||||
|
||||
Object.keys(files).forEach(function(path) {
|
||||
var name = paths[path];
|
||||
|
||||
Template[name] = files[path];
|
||||
});
|
||||
|
||||
names = TMPL_PATH.map(function(item) {
|
||||
return item + '.html';
|
||||
});
|
||||
|
||||
msg = CloudFunc.formatMsg('read', names, status);
|
||||
Util.log(msg);
|
||||
}
|
||||
|
||||
if (isRead) {
|
||||
callback();
|
||||
});
|
||||
} else {
|
||||
filesList = TMPL_PATH.map(function(name) {
|
||||
var path = DIR_FS + name + '.html';
|
||||
|
||||
paths[path] = name;
|
||||
|
||||
return path;
|
||||
});
|
||||
|
||||
files.read(filesList, 'utf8', function(error, files) {
|
||||
if (error)
|
||||
Util.log(error.message);
|
||||
else
|
||||
Object.keys(files).forEach(function(path) {
|
||||
var name = paths[path];
|
||||
|
||||
Template[name] = files[path];
|
||||
});
|
||||
|
||||
callback();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -117,59 +116,59 @@
|
|||
function route(request, response, callback) {
|
||||
var name, p, isAuth, isFS, path;
|
||||
|
||||
if (request && response) {
|
||||
name = ponse.getPathName(request);
|
||||
isAuth = Util.strCmp(name, ['/auth', '/auth/github']);
|
||||
isFS = Util.strCmp(name, '/') || Util.isContainStrAtBegin(name, FS);
|
||||
Util.checkArgs(arguments, ['req', 'res']);
|
||||
|
||||
name = ponse.getPathName(request);
|
||||
isAuth = Util.strCmp(name, ['/auth', '/auth/github']);
|
||||
isFS = Util.strCmp(name, '/') || Util.isContainStrAtBegin(name, FS);
|
||||
|
||||
p = {
|
||||
request : request,
|
||||
response : response,
|
||||
gzip : true,
|
||||
name : name
|
||||
};
|
||||
|
||||
if (!isAuth && !isFS)
|
||||
Util.exec(callback);
|
||||
else if (isAuth) {
|
||||
Util.log('* Routing' + '-> ' + name);
|
||||
|
||||
p = {
|
||||
request : request,
|
||||
response : response,
|
||||
gzip : true,
|
||||
name : name
|
||||
};
|
||||
p.name = DIR_HTML + name + '.html';
|
||||
ponse.sendFile(p);
|
||||
} else if (isFS) {
|
||||
name = Util.rmStrOnce(name, CloudFunc.FS) || '/';
|
||||
path = mellow.convertPath(name);
|
||||
|
||||
if (!isAuth && !isFS)
|
||||
Util.exec(callback);
|
||||
else if (isAuth) {
|
||||
Util.log('* Routing' + '-> ' + name);
|
||||
mellow.read(path, function(error, dir) {
|
||||
if (dir)
|
||||
dir.path = format.addSlashToEnd(name);
|
||||
|
||||
p.name = DIR_HTML + name + '.html';
|
||||
ponse.sendFile(p);
|
||||
} else if (isFS) {
|
||||
name = Util.rmStrOnce(name, CloudFunc.FS) || '/';
|
||||
path = mellow.convertPath(name);
|
||||
|
||||
mellow.read(path, function(error, dir) {
|
||||
if (dir)
|
||||
dir.path = format.addSlashToEnd(name);
|
||||
|
||||
if (error)
|
||||
if (error.code !== 'ENOTDIR')
|
||||
if (error)
|
||||
if (error.code !== 'ENOTDIR')
|
||||
ponse.sendError(error, p);
|
||||
else
|
||||
fs.realpath(path, function(error, pathReal) {
|
||||
if (!error)
|
||||
p.name = pathReal;
|
||||
else
|
||||
p.name = path;
|
||||
|
||||
p.gzip = false;
|
||||
ponse.sendFile(p);
|
||||
});
|
||||
else
|
||||
buildIndex(dir, function(error, data) {
|
||||
var NOT_LOG = true;
|
||||
|
||||
p.name = PATH_INDEX;
|
||||
|
||||
if (error)
|
||||
ponse.sendError(error, p);
|
||||
else
|
||||
fs.realpath(path, function(error, pathReal) {
|
||||
if (!error)
|
||||
p.name = pathReal;
|
||||
else
|
||||
p.name = path;
|
||||
|
||||
p.gzip = false;
|
||||
ponse.sendFile(p);
|
||||
});
|
||||
else
|
||||
buildIndex(dir, function(error, data) {
|
||||
var NOT_LOG = true;
|
||||
|
||||
p.name = PATH_INDEX;
|
||||
|
||||
if (error)
|
||||
ponse.sendError(error, p);
|
||||
else
|
||||
ponse.send(data, p, NOT_LOG);
|
||||
});
|
||||
});
|
||||
}
|
||||
ponse.send(data, p, NOT_LOG);
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue