feature(config) add

This commit is contained in:
coderaiser 2014-09-16 04:51:22 -04:00
parent 7d93c9930c
commit cd296f84be
5 changed files with 81 additions and 115 deletions

View file

@ -1,17 +1,7 @@
(function() {
'use strict';
if (!global.cloudcmd)
return console.log(
'# server.js' + '\n' +
'# -----------' + '\n' +
'# Module is part of Cloud Commander,' + '\n' +
'# easy to use web server.' + '\n' +
'# http://cloudcmd.io' + '\n');
var main = global.cloudcmd.main,
DIR = __dirname + '/../',
var DIR = __dirname + '/../',
DIR_LIB = DIR + 'lib/',
DIR_SERVER = DIR_LIB + 'server/',
@ -31,6 +21,8 @@
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'),
express = require(DIR_SERVER + 'express'),
@ -58,35 +50,31 @@
*/
module.exports = function() {
var port, ip,
HTTP = 'http://',
config = main.config;
HTTP = 'http://';
port = process.env.PORT || /* c9 */
process.env.VCAP_APP_PORT || /* cloudfoundry */
config.port,
config('port'),
ip = process.env.IP || /* c9 */
config.ip ||
config('ip') ||
'0.0.0.0';
if (config.server)
if (config('server'))
createServer(port, ip, HTTP);
};
function createServer(port, ip, protocol, callback) {
var server, app, respondApp,
config = main.config,
middle = controller.middle(DIR),
isOption = function(name) {
return function() {
return main.config[name];
};
return config(name);
},
isMinify = isOption('minify'),
isOnline = isOption('online'),
isMinify = isOption.bind(null, 'minify'),
isOnline = isOption.bind(null, 'online'),
funcs = [
rest,
@ -140,10 +128,9 @@
function addSockets(server) {
var socket, msg,
config = main.config,
status = 'off';
if (config.socket && io) {
if (io && config('socket')) {
socket = io.listen(server);
if (socket) {
@ -177,8 +164,7 @@
* @param res - ответ сервера (Response)
*/
function controller(dir, req, res) {
var config = main.config,
parsedUrl = URL.parse(req.url),
var parsedUrl = URL.parse(req.url),
query = parsedUrl.search || '',
name = ponse.getPathName(req);
@ -189,7 +175,7 @@
ponse.sendFile({
name : name,
cache : config.cache,
cache : config('cache'),
gzip : true,
request : req,
response : res

40
lib/server/config.js Normal file
View file

@ -0,0 +1,40 @@
(function() {
var DIR_SERVER = __dirname + '/',
DIR_LIB = DIR_SERVER + '../',
DIR = DIR_SERVER + '../../',
fs = require('fs'),
Util = require(DIR_LIB + 'util'),
tryRequire = require(DIR_SERVER + 'tryRequire'),
ConfigPath = DIR + 'json/config.json',
config = tryRequire(ConfigPath) || {};
module.exports = function(key, value) {
var result;
if (value === undefined)
result = config[key];
else
config[key] = value;
return result;
};
module.exports.save = function(callback) {
var data = Util.stringifyJSON(config);
Util.checkArgs(arguments, ['callback']);
if (data)
fs.writeFile(ConfigPath, data, callback);
else
callback({
message: 'Error: config is empty!'
});
};
})();

View file

@ -1,21 +1,15 @@
/* RESTful module */
(function() {
'use strict';
if (!global.cloudcmd)
return console.log(
'# rest.js' + '\n' +
'# -----------' + '\n' +
'# Module is part of Cloud Commander,' + '\n' +
'# used for work with REST API.' + '\n' +
'# If you wont to see at work set rest: true' + '\n' +
'# and apiURL in config.json' + '\n' +
'# http://cloudcmd.io' + '\n');
/*
'# rest.js' + '\n' +
'# -----------' + '\n' +
'# Module is part of Cloud Commander,' + '\n' +
'# used for work with REST API.' + '\n' +
'# http://cloudcmd.io' + '\n');
*/
var main = global.cloudcmd.main,
DIR = './',
var DIR = './',
DIR_LIB = DIR + '../',
DIR_ROOT = __dirname + '/' + DIR_LIB + '../',
DIR_JSON = DIR_ROOT + 'json/',
@ -36,6 +30,7 @@
mellow = require(DIR + 'mellow'),
ponse = require(DIR + 'ponse'),
pipe = require(DIR + 'pipe'),
config = require(DIR + 'config'),
isWin32 = process.platform === 'win32',
@ -270,7 +265,7 @@
* @param pParams {command, method, body, requrest, response}
*/
function onPUT(name, body, callback) {
var cmd, files, json, config, data, from, to, error;
var cmd, files, json, data, from, to, error;
Util.checkArgs(arguments, ['name', 'body', 'callback']);
@ -386,7 +381,6 @@
case 'config':
var passwd = files && files.password,
sha = crypto.createHash('sha1');
config = main.config;
if (passwd) {
sha.update(passwd);
@ -395,12 +389,12 @@
}
Object.keys(files).forEach(function(name) {
config[name] = files[name];
config(name, files[name]);
});
json = Util.stringifyJSON(config) + '\n';
fs.writeFile(DIR_JSON + 'config.json', json, function(error) {
config.save(function(error) {
data = formatMsg('config', name);
callback(error, data);
});

View file

@ -9,18 +9,16 @@
fs = require('fs'),
main = require(DIR_SERVER + 'main'),
mellow = require(DIR_SERVER + 'mellow'),
ponse = require(DIR_SERVER + 'ponse'),
files = require(DIR_SERVER + 'files'),
minify = require(DIR_SERVER + 'minify'),
config = require(DIR_SERVER + 'config'),
Util = require(DIR_LIB + 'util'),
CloudFunc = require(DIR_LIB + 'cloudfunc'),
format = require(DIR_LIB + 'format'),
Config = main.config,
PATH_INDEX = DIR_FS + 'index.html',
TMPL_PATH = [
@ -51,7 +49,7 @@
data = options.data,
panel = options.panel;
if (!Config.showKeysPanel) {
if (!config('showKeysPanel')) {
keysPanel = '<div class="keyspanel';
data = data.replace(keysPanel + '"', keysPanel +' hidden"');
}
@ -139,7 +137,7 @@
p.name = DIR_HTML + name + '.html';
ponse.sendFile(p);
} else if (isFS) {
name = Util.rmStrOnce(name, CloudFunc.FS) || main.SLASH;
name = Util.rmStrOnce(name, CloudFunc.FS) || '/';
path = mellow.convertPath(name);
mellow.read(path, function(error, dir) {
@ -176,7 +174,7 @@
}
function buildIndex(json, callback) {
var isMinify = Config.minify;
var isMinify = config('minify');
Util.exec.if(!isMinify, function(error, name) {
fs.readFile(name || PATH_INDEX, 'utf8', function(error, template) {